<?php
 namespace Module;
 use LayoutMenuItem;
 use TCMS\Module;
 use Route;
 use LayoutData;
 class AdminUrlDefault extends Module {
 protected function initialize() {
 }
 protected function registerRoutes() {
 Route::get('/url', 'UrlController', 'index')->name('index');
 Route::get('/url/update/{id}', 'UrlController', 'update')->name('update');
 Route::post('/url/update/{id}', 'UrlController', 'postUpdate');
 Route::post('/url/delete', 'UrlController', 'postDelete')->name('delete');
 }
 public function registerComponent() {
 \Model::add('UrlModel');
 }
 public function boot() {
 LayoutData::addMenuItem('url', route('index'), 'Url', 'primary', function(LayoutMenuItem $menuItem) {
 $menuItem->icon = 'fa fa-link';
 }
);
 \Event::listen('add_url', function($p) {
 $model = $p[0];
 if($model instanceof \TCMS\Model) {
 $type = $p[1];
 $urlModel = new \Model\UrlModel();
 $urlModel->name = $model->title ? $model->title : $model->name;
 $urlModel->url = $model->alias ? $model->alias : $model->url;
 $urlModel->type = $type;
 $urlModel->src_id = $model->id;
 $urlModel->insert();
 }
 }
);
 \Event::listen('update_url', function($p) {
 $model = $p[0];
 if($model instanceof \TCMS\Model) {
 $type = $p[1];
 $urlModel = new \Model\UrlModel();
 $urlModel->name = $model->title ? $model->title : $model->name;
 $urlModel->url = $model->alias ? $model->alias : $model->url;
 $urlModel->type = $type;
 $urlModel->src_id = $model->id;
 $url = $urlModel->where('type', $urlModel->type)->where('src_id', $urlModel->src_id)->first();
 if($url) {
 $urlModel->where('id', $url->id)->update();
 }
 else {
 $urlModel->insert();
 }
 }
 }
);
 \Event::listen('delete_url', function($p) {
 $id = $p[0];
 $type = $p[1];
 $urlModel = new \Model\UrlModel();
 $urlModel->where('type', $type)->where('src_id', $id)->delete();
 }
);
 }
 public function middleware(string $routeName, $auth) {
 }
 }
