From 6f0822792835ae1d6a928380ea2928e121136364 Mon Sep 17 00:00:00 2001 From: hartois Date: Wed, 13 Jul 2016 07:39:40 +0300 Subject: [PATCH 1/2] Modules getRelatedFiles: FIX designer project path --- dvelum/app/Backend/Modules/Controller.php | 1112 +++++++++++---------- 1 file changed, 559 insertions(+), 553 deletions(-) diff --git a/dvelum/app/Backend/Modules/Controller.php b/dvelum/app/Backend/Modules/Controller.php index c8cdd8d2..2318cb44 100644 --- a/dvelum/app/Backend/Modules/Controller.php +++ b/dvelum/app/Backend/Modules/Controller.php @@ -8,17 +8,17 @@ public function indexAction() { $res = Resource::getInstance(); $res->addJs('/js/app/system/FilesystemWindow.js' , 1); - $res->addJs('/js/app/system/ImageField.js' , 1); - $res->addJs('/js/app/system/IconField.js' , 1); - $res->addJs('/js/app/system/HistoryPanel.js' , 1); - $res->addJs('/js/app/system/EditWindow.js' , 1); - $res->addJs('/js/app/system/crud/modules.js' , 2); - $res->addJs('/js/app/system/Modules.js' , 3); - $this->_resource->addInlineJs(' - var canEdit = '.((integer)$this->_user->canEdit($this->_module)).'; - var canDelete = '.((integer)$this->_user->canDelete($this->_module)).'; + $res->addJs('/js/app/system/ImageField.js' , 1); + $res->addJs('/js/app/system/IconField.js' , 1); + $res->addJs('/js/app/system/HistoryPanel.js' , 1); + $res->addJs('/js/app/system/EditWindow.js' , 1); + $res->addJs('/js/app/system/crud/modules.js' , 2); + $res->addJs('/js/app/system/Modules.js' , 3); + $this->_resource->addInlineJs(' + var canEdit = '.((integer)$this->_user->canEdit($this->_module)).'; + var canDelete = '.((integer)$this->_user->canDelete($this->_module)).'; '); - parent::indexAction(); + parent::indexAction(); } /** @@ -26,178 +26,178 @@ public function indexAction() */ public function listAction() { - $type = Request::post('type' , Filter::FILTER_STRING, false); - - if(!$type) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - switch($type){ - case 'backend': - $this->listBackend(); - break; - case 'frontend': - $this->listFrontend(); - break; - default: - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } + $type = Request::post('type' , Filter::FILTER_STRING, false); + + if(!$type) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + switch($type){ + case 'backend': + $this->listBackend(); + break; + case 'frontend': + $this->listFrontend(); + break; + default: + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } + } + /** + * Get list of backend modules + */ + public function listBackend() + { + $manager = new Modules_Manager(); + $data = $manager->getList(); + + foreach ($data as $k=>&$item) + { + $item['related_files']= ''; + + if(empty($item['dist'])){ + $relatedFiles = $this->getRelatedFiles($item); + if(!empty($relatedFiles)){ + $item['related_files'] = implode('
' , $relatedFiles); + } + } + + $item['iconUrl'] = $this->_configMain->get('wwwroot').$item['icon']; + } + + Response::jsonSuccess(array_values($data)); + } + + /** + * Get list of frontend modules + */ + public function listFrontend() + { + $manager = new Modules_Manager_Frontend(); + $data = $manager->getList(); + Response::jsonSuccess(array_values($data)); } - /** - * Get list of backend modules - */ - public function listBackend() - { - $manager = new Modules_Manager(); - $data = $manager->getList(); - - foreach ($data as $k=>&$item) - { - $item['related_files']= ''; - - if(empty($item['dist'])){ - $relatedFiles = $this->getRelatedFiles($item); - if(!empty($relatedFiles)){ - $item['related_files'] = implode('
' , $relatedFiles); - } - } - - $item['iconUrl'] = $this->_configMain->get('wwwroot').$item['icon']; - } - - Response::jsonSuccess(array_values($data)); - } - - /** - * Get list of frontend modules - */ - public function listFrontend() - { - $manager = new Modules_Manager_Frontend(); - $data = $manager->getList(); - Response::jsonSuccess(array_values($data)); - } /** * Update modules list */ public function updateAction() { - $this->_checkCanEdit(); - $type = Request::post('type' , Filter::FILTER_STRING, false); - - if(!$type) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - switch($type){ - case 'backend': - $this->updateBackendRecord(); - break; - case 'frontend': - $this->updateFrontendRecord(); - break; - default: - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } + $this->_checkCanEdit(); + $type = Request::post('type' , Filter::FILTER_STRING, false); + + if(!$type) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + switch($type){ + case 'backend': + $this->updateBackendRecord(); + break; + case 'frontend': + $this->updateFrontendRecord(); + break; + default: + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } } - /** - * Update frontend module record - */ - protected function updateFrontendRecord() - { - $id = Request::post('id' , Filter::FILTER_STRING , false); - - $acceptedFields = array( - 'code' => Filter::FILTER_STRING , - 'title' => Filter::FILTER_STRING , - 'class'=> Filter::FILTER_STRING, - ); - - $data = array(); - $errors = array(); - foreach($acceptedFields as $name => $type){ - $data[$name] = Request::post($name , $type , false); - if(empty($data[$name])) - $errors[$name] = $this->_lang->get('CANT_BE_EMPTY'); - } - - if(!empty($errors)) - Response::jsonError($this->_lang->get('FILL_FORM') , $errors); - - $manager = new Modules_Manager_Frontend(); - - if(empty($id) && $manager->isValidModule($data['code'])){ - Response::jsonError($this->_lang->get('INVALID_VALUE'),array('code'=>$this->_lang->get('SB_UNIQUE'))); - } - - if(empty($id)){ - $id = $data['code']; - } - - if(!$manager->updateModule($id , $data)){ - Response::jsonError($this->_lang->get('CANT_WRITE_FS')); - } - - Response::jsonSuccess(array('id'=>$data['code'])); - } - - /** - * Update module record - */ - protected function updateBackendRecord() - { - - $manager = new Modules_Manager(); - - $id = Request::post('id' , Filter::FILTER_STRING , false); - $controller = Request::post('class' , Filter::FILTER_STRING , false); - - if(!$id){ - if(!$controller){ - Response::jsonError($this->_lang->get('INVALID_VALUE'),array('class'=>$this->_lang->get('CANT_BE_EMPTY'))); - }else{ - $moduleName = str_replace(array('Backend_','_Controller'),'', $controller); - $list = $manager->getRegisteredModules(); - if(in_array($moduleName, $list , true)){ - Response::jsonError($this->_lang->get('INVALID_VALUE'),array('class'=>$this->_lang->get('SB_UNIQUE'))); - } - } - } - - $acceptedFields = array( - //'id'=> Filter::FILTER_STRING , - 'dev' => Filter::FILTER_BOOLEAN , - 'active' => Filter::FILTER_BOOLEAN , - 'title'=> Filter::FILTER_STRING , - 'class'=> Filter::FILTER_STRING , - 'designer'=> Filter::FILTER_STRING, - 'in_menu'=> Filter::FILTER_BOOLEAN , - 'icon'=> Filter::FILTER_STRING - ); - - foreach($acceptedFields as $name => $type){ + /** + * Update frontend module record + */ + protected function updateFrontendRecord() + { + $id = Request::post('id' , Filter::FILTER_STRING , false); + + $acceptedFields = array( + 'code' => Filter::FILTER_STRING , + 'title' => Filter::FILTER_STRING , + 'class'=> Filter::FILTER_STRING, + ); + + $data = array(); + $errors = array(); + foreach($acceptedFields as $name => $type){ + $data[$name] = Request::post($name , $type , false); + if(empty($data[$name])) + $errors[$name] = $this->_lang->get('CANT_BE_EMPTY'); + } + + if(!empty($errors)) + Response::jsonError($this->_lang->get('FILL_FORM') , $errors); + + $manager = new Modules_Manager_Frontend(); + + if(empty($id) && $manager->isValidModule($data['code'])){ + Response::jsonError($this->_lang->get('INVALID_VALUE'),array('code'=>$this->_lang->get('SB_UNIQUE'))); + } + + if(empty($id)){ + $id = $data['code']; + } + + if(!$manager->updateModule($id , $data)){ + Response::jsonError($this->_lang->get('CANT_WRITE_FS')); + } + + Response::jsonSuccess(array('id'=>$data['code'])); + } + + /** + * Update module record + */ + protected function updateBackendRecord() + { + + $manager = new Modules_Manager(); + + $id = Request::post('id' , Filter::FILTER_STRING , false); + $controller = Request::post('class' , Filter::FILTER_STRING , false); + + if(!$id){ + if(!$controller){ + Response::jsonError($this->_lang->get('INVALID_VALUE'),array('class'=>$this->_lang->get('CANT_BE_EMPTY'))); + }else{ + $moduleName = str_replace(array('Backend_','_Controller'),'', $controller); + $list = $manager->getRegisteredModules(); + if(in_array($moduleName, $list , true)){ + Response::jsonError($this->_lang->get('INVALID_VALUE'),array('class'=>$this->_lang->get('SB_UNIQUE'))); + } + } + } + + $acceptedFields = array( + //'id'=> Filter::FILTER_STRING , + 'dev' => Filter::FILTER_BOOLEAN , + 'active' => Filter::FILTER_BOOLEAN , + 'title'=> Filter::FILTER_STRING , + 'class'=> Filter::FILTER_STRING , + 'designer'=> Filter::FILTER_STRING, + 'in_menu'=> Filter::FILTER_BOOLEAN , + 'icon'=> Filter::FILTER_STRING + ); + + foreach($acceptedFields as $name => $type){ if($type === Filter::FILTER_BOOLEAN) - $data[$name] = Request::post($name , $type , false); + $data[$name] = Request::post($name , $type , false); else $data[$name] = Request::post($name , $type , null); - } + } - if($id) { - if (!$manager->isValidModule($id)) { - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } - }else{ - $id = $moduleName; - } + if($id) { + if (!$manager->isValidModule($id)) { + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } + }else{ + $id = $moduleName; + } - $data['id'] = $id; + $data['id'] = $id; - if($manager->updateModule($id , $data)){ - Response::jsonSuccess(array('id'=>$id)); - }else{ - Response::jsonError($this->_lang->get('CANT_WRITE_FS')); - } - } + if($manager->updateModule($id , $data)){ + Response::jsonSuccess(array('id'=>$id)); + }else{ + Response::jsonError($this->_lang->get('CANT_WRITE_FS')); + } + } /** * Get list of available controllers @@ -206,411 +206,417 @@ public function controllersAction() { $type = Request::post('type' , Filter::FILTER_STRING , false); - if(!$type) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - switch($type){ - case 'backend': - $manager = new Modules_Manager(); - Response::jsonSuccess($manager->getAvailableControllers()); - break; - case 'frontend': - $manager = new Modules_Manager_Frontend(); - Response::jsonSuccess($manager->getControllers());; - break; - default: - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } + if(!$type) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + switch($type){ + case 'backend': + $manager = new Modules_Manager(); + Response::jsonSuccess($manager->getAvailableControllers()); + break; + case 'frontend': + $manager = new Modules_Manager_Frontend(); + Response::jsonSuccess($manager->getControllers());; + break; + default: + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } } - /** - * Get Designer projects tree list - */ - public function fslistAction() - { - $node = Request::post('node', 'string', ''); - $manager = new Designer_Manager($this->_configMain); - Response::jsonArray($manager->getProjectsList($node)); - } - - /** - * Get list of registered Db_Object's - */ - public function objectsAction() - { - $manager = new Db_Object_Manager(); - $list = $manager->getRegisteredObjects(); - $data = array(); - - $systemObjects = $this->_configBackend['system_objects']; - - foreach ($list as $key) - if(!in_array(ucfirst($key), $systemObjects , true) && !class_exists('Backend_'.Utils_String::formatClassName($key).'_Controller')) - $data[]= array('id'=>$key , 'title'=>Db_Object_Config::getInstance($key)->getTitle()); - - Response::jsonSuccess($data); - } - - /** - * Create new module - */ - public function createAction() - { - $this->_checkCanEdit(); - - $object = Request::post('object', 'string', false); - - if(!$object) - Response::jsonError($this->_lang->WRONG_REQUEST); - - $object = Utils_String::formatClassName($object); - - $class = 'Backend_' . $object . '_Controller'; - - if(class_exists($class)) - Response::jsonError($this->_lang->FILL_FORM , array('id'=>'name','msg'=>$this->_lang->SB_UNIQUE)); - - $designerConfig = Config::storage()->get('designer.php'); - - $projectRelativePath = '/' . strtolower($object) . '.designer.dat'; - $projectFile = Config::storage()->getWrite() . $designerConfig->get('configs') . strtolower($object) . '.designer.dat'; - - if(file_exists($projectFile)) - Response::jsonError($this->_lang->FILE_EXISTS . '(' . $projectFile . ')'); - - $objectConfig = Db_Object_Config::getInstance($object); - - // Check ACL permissions - $acl = $objectConfig->getAcl(); - if($acl){ - if(!$acl->can(Db_Object_Acl::ACCESS_CREATE , $object) || !$acl->can(Db_Object_Acl::ACCESS_VIEW , $object)){ - Response::jsonError($this->_lang->get('ACL_ACCESS_DENIED')); - } - } - - $manager = new Db_Object_Manager(); - - if(!$manager->objectExists($object)) - Response::jsonError($this->_lang->FILL_FORM , array('id'=>'object','msg'=>$this->_lang->INVALID_VALUE)); - - $codeGenadApter = $this->_configBackend->get('modules_generator'); - - $codeGen = new $codeGenadApter(); - try{ - if($objectConfig->isRevControl()) - $codeGen->createVcModule($object, $projectFile); - else - $codeGen->createModule($object, $projectFile); - - }catch (Exception $e){ - Response::jsonError($e->getMessage()); - } - - $userInfo = User::getInstance()->getInfo(); - $per = Model::factory('Permissions'); - - if(!$per->setGroupPermissions($userInfo['group_id'], $object , 1 , 1 , 1 , 1)) - Response::jsonError($this->_lang->CANT_EXEC); - - $modulesManager = new Modules_Manager(); - $modulesManager->addModule($object , array( - 'class'=>$class, - 'id'=>$object , - 'active'=>true, - 'dev'=>false, - 'title'=>$objectConfig->getTitle(), - 'designer'=> $projectRelativePath, - 'icon'=>'i/system/icons/default.png', - 'in_menu'=>true - )); - - Response::jsonSuccess( - array( - 'class'=>$class, - 'name'=>$object , - 'active'=>true, - 'dev'=>false, - 'title'=>$objectConfig->getTitle(), - 'designer'=> $projectFile - ) - ); - } - - /** - * Get module data - */ - public function loadDataAction() - { - $id = Request::post('id' , Filter::FILTER_STRING , false); - $type = Request::post('type' , Filter::FILTER_STRING , false); - - if(!$id || !$type) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - switch($type){ - case 'backend': - $manager = new Modules_Manager(); - Response::jsonSuccess($manager->getModuleConfig($id)); - break; - case 'frontend': - $manager = new Modules_Manager_Frontend(); - $data = $manager->getModuleConfig($id); - $data['id'] = $data['code']; - Response::jsonSuccess($data); - break; - default: - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } - } - - public function deleteAction() - { - $this->_checkCanDelete(); - - $type = Request::post('type' , Filter::FILTER_STRING , false); - - if(!$type) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - switch($type){ - case 'backend': - $this->deleteBackendModule(); - break; - case 'frontend': - $this->deleteFrontendModule(); - break; - default: - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } - } - - /** - * Delete frontend module - */ - protected function deleteFrontendModule() - { - $manager = new Modules_Manager_Frontend(); - $code = Request::post('code' , Filter::FILTER_STRING , false); - - if(!$code || !$manager->isValidModule($code)){ - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - } - - if($manager->removeModule($code)){ - Response::jsonSuccess(); - }else{ - Response::jsonError($this->_lang->get('CANT_WRITE_FS')); - } - } - - /** - * Get list of module files which can be deleted - * @param array $moduleConfig - * @return array - */ - protected function getRelatedFiles($moduleConfig) - { - $relatedFiles = []; - $classFile = $this->_configMain->get('local_controllers').str_replace('_', '/', $moduleConfig['class']).'.php'; - - if(empty($moduleConfig['dist']) && file_exists($classFile)) - $relatedFiles[] = $classFile; - - if(empty($moduleConfig['dist']) && !empty($moduleConfig['designer'])) - { - $configWrite = Config::storage()->getWrite(); - if(strpos($moduleConfig['designer'],$configWrite)!==false){ - $relatedFiles[] = $moduleConfig['designer']; - if(is_dir($moduleConfig['designer'].'.files')){ - $relatedFiles[]=$moduleConfig['designer'].'.files'; - } - } - } - return $relatedFiles; - } - /** - * Delete module - */ - protected function deleteBackendModule() - { - $this->_checkCanDelete(); - $module = Request::post('id', 'string', false); - $removeRelated = Request::post('delete_related', 'boolean', false); - - $manager = new Modules_Manager(); - $data = $manager->getList(); - if(!$module || !strlen($module) || !$manager->isValidModule($module) || !isset($data[$module])) - Response::jsonError($this->_lang->get('WRONG_REQUEST')); - - $item = $data[$module]; - - $filesToDelete = array(); - - if($removeRelated && empty($item['dist'])){ - $filesToDelete = $this->getRelatedFiles($item); - } - - // check before deleting - if(!empty($filesToDelete)) - { - $err = array(); - foreach ($filesToDelete as $file){ - if(!is_writable($file)) - $err[] = $file; - } - - if(!empty($err)) - Response::jsonError($this->_lang->get('CANT_WRITE_FS') . "\n
" . implode(",\n
", $err)); - } - - $manager->removeModule($module); - - if(!$manager->save()) - Response::jsonError($this->_lang->get('CANT_WRITE_FS') .' ' . $manager->getConfig()->getName()); - - // try to delete related files - if(!empty($filesToDelete)) - { - $err = array(); - foreach ($filesToDelete as $file) - { - if(!file_exists($file)) - continue; - - if(is_dir($file)){ - if(!File::rmdirRecursive($file , true)) - $err[] = $file; - } - else{ - if(!unlink($file)) - $err[] = $file; - } - } - - if(!empty($err)) - Response::jsonError($this->_lang->get('CANT_WRITE_FS') . "\n
".implode(",\n
", $err)); - } - - $this->createClassMap(); - Response::jsonSuccess(); - } - - /** - * Get list of image folders - */ - public function iconDirsAction() - { - $path = Request::post('node', 'string', ''); - $path = str_replace('.','', $path); - - $dirPath = $this->_configMain->get('wwwpath'); - - if(!is_dir($dirPath.$path)) - Response::jsonArray(array()); - - $files = File::scanFiles($dirPath . $path, false, false , File::Dirs_Only); - - if(empty($files)) - Response::jsonArray(array()); - - sort($files); - $list = array(); - - foreach($files as $k=>$fpath) - { - $text = basename($fpath); - - $obj = new stdClass(); - $obj->id = str_replace($dirPath, '', $fpath); - $obj->text = $text; - $obj->url = '/' . $obj->id; - - if(is_dir($fpath)) - { - $obj->expanded = false; - $obj->leaf = false; - } - else - { - $obj->leaf = true; - } - $list[] = $obj; - } - Response::jsonArray($list); - } - - /** - * Get image list - */ - public function iconListAction() - { - $dirPath = $this->_configMain->get('wwwpath'); - $dir = Request::post('dir', 'string', ''); - - if(!is_dir($dirPath . $dir)) - Response::jsonArray(array()); + /** + * Get Designer projects tree list + */ + public function fslistAction() + { + $node = Request::post('node', 'string', ''); + $manager = new Designer_Manager($this->_configMain); + Response::jsonArray($manager->getProjectsList($node)); + } + + /** + * Get list of registered Db_Object's + */ + public function objectsAction() + { + $manager = new Db_Object_Manager(); + $list = $manager->getRegisteredObjects(); + $data = array(); + + $systemObjects = $this->_configBackend['system_objects']; + + foreach ($list as $key) + if(!in_array(ucfirst($key), $systemObjects , true) && !class_exists('Backend_'.Utils_String::formatClassName($key).'_Controller')) + $data[]= array('id'=>$key , 'title'=>Db_Object_Config::getInstance($key)->getTitle()); + + Response::jsonSuccess($data); + } + + /** + * Create new module + */ + public function createAction() + { + $this->_checkCanEdit(); + + $object = Request::post('object', 'string', false); + + if(!$object) + Response::jsonError($this->_lang->WRONG_REQUEST); + + $object = Utils_String::formatClassName($object); + + $class = 'Backend_' . $object . '_Controller'; + + if(class_exists($class)) + Response::jsonError($this->_lang->FILL_FORM , array('id'=>'name','msg'=>$this->_lang->SB_UNIQUE)); + + $designerConfig = Config::storage()->get('designer.php'); + + $projectRelativePath = '/' . strtolower($object) . '.designer.dat'; + $projectFile = Config::storage()->getWrite() . $designerConfig->get('configs') . strtolower($object) . '.designer.dat'; + + if(file_exists($projectFile)) + Response::jsonError($this->_lang->FILE_EXISTS . '(' . $projectFile . ')'); + + $objectConfig = Db_Object_Config::getInstance($object); + + // Check ACL permissions + $acl = $objectConfig->getAcl(); + if($acl){ + if(!$acl->can(Db_Object_Acl::ACCESS_CREATE , $object) || !$acl->can(Db_Object_Acl::ACCESS_VIEW , $object)){ + Response::jsonError($this->_lang->get('ACL_ACCESS_DENIED')); + } + } + + $manager = new Db_Object_Manager(); + + if(!$manager->objectExists($object)) + Response::jsonError($this->_lang->FILL_FORM , array('id'=>'object','msg'=>$this->_lang->INVALID_VALUE)); + + $codeGenadApter = $this->_configBackend->get('modules_generator'); + + $codeGen = new $codeGenadApter(); + try{ + if($objectConfig->isRevControl()) + $codeGen->createVcModule($object, $projectFile); + else + $codeGen->createModule($object, $projectFile); + + }catch (Exception $e){ + Response::jsonError($e->getMessage()); + } + + $userInfo = User::getInstance()->getInfo(); + $per = Model::factory('Permissions'); + + if(!$per->setGroupPermissions($userInfo['group_id'], $object , 1 , 1 , 1 , 1)) + Response::jsonError($this->_lang->CANT_EXEC); + + $modulesManager = new Modules_Manager(); + $modulesManager->addModule($object , array( + 'class'=>$class, + 'id'=>$object , + 'active'=>true, + 'dev'=>false, + 'title'=>$objectConfig->getTitle(), + 'designer'=> $projectRelativePath, + 'icon'=>'i/system/icons/default.png', + 'in_menu'=>true + )); + + Response::jsonSuccess( + array( + 'class'=>$class, + 'name'=>$object , + 'active'=>true, + 'dev'=>false, + 'title'=>$objectConfig->getTitle(), + 'designer'=> $projectFile + ) + ); + } + + /** + * Get module data + */ + public function loadDataAction() + { + $id = Request::post('id' , Filter::FILTER_STRING , false); + $type = Request::post('type' , Filter::FILTER_STRING , false); + + if(!$id || !$type) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + switch($type){ + case 'backend': + $manager = new Modules_Manager(); + Response::jsonSuccess($manager->getModuleConfig($id)); + break; + case 'frontend': + $manager = new Modules_Manager_Frontend(); + $data = $manager->getModuleConfig($id); + $data['id'] = $data['code']; + Response::jsonSuccess($data); + break; + default: + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } + } + + public function deleteAction() + { + $this->_checkCanDelete(); + + $type = Request::post('type' , Filter::FILTER_STRING , false); + + if(!$type) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + switch($type){ + case 'backend': + $this->deleteBackendModule(); + break; + case 'frontend': + $this->deleteFrontendModule(); + break; + default: + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } + } + + /** + * Delete frontend module + */ + protected function deleteFrontendModule() + { + $manager = new Modules_Manager_Frontend(); + $code = Request::post('code' , Filter::FILTER_STRING , false); + + if(!$code || !$manager->isValidModule($code)){ + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + } + + if($manager->removeModule($code)){ + Response::jsonSuccess(); + }else{ + Response::jsonError($this->_lang->get('CANT_WRITE_FS')); + } + } + + /** + * Get list of module files which can be deleted + * @param array $moduleConfig + * @return array + */ + protected function getRelatedFiles($moduleConfig) + { + $relatedFiles = []; + $classFile = $this->_configMain->get('local_controllers').str_replace('_', '/', $moduleConfig['class']).'.php'; + + if(empty($moduleConfig['dist']) && file_exists($classFile)) + $relatedFiles[] = $classFile; + + if(empty($moduleConfig['dist']) && !empty($moduleConfig['designer'])) + { + // local configs path + $configWrite = Config::storage()->getWrite(); + // relative designer projects path + $layoutsPath = Config::storage()->get('designer.php')->get('configs'); + // project path in local configs directory + $projectWrite = $configWrite.$layoutsPath.$moduleConfig['designer']; + + if(file_exists($projectWrite)){ + $relatedFiles[] = $projectWrite; + if(is_dir($projectWrite.'.files')){ + $relatedFiles[]=$projectWrite.'.files'; + } + } + } + return $relatedFiles; + } + /** + * Delete module + */ + protected function deleteBackendModule() + { + $this->_checkCanDelete(); + $module = Request::post('id', 'string', false); + $removeRelated = Request::post('delete_related', 'boolean', false); + + $manager = new Modules_Manager(); + $data = $manager->getList(); + if(!$module || !strlen($module) || !$manager->isValidModule($module) || !isset($data[$module])) + Response::jsonError($this->_lang->get('WRONG_REQUEST')); + + $item = $data[$module]; + + $filesToDelete = array(); + + if($removeRelated && empty($item['dist'])){ + $filesToDelete = $this->getRelatedFiles($item); + } + + // check before deleting + if(!empty($filesToDelete)) + { + $err = array(); + foreach ($filesToDelete as $file){ + if(!is_writable($file)) + $err[] = $file; + } + + if(!empty($err)) + Response::jsonError($this->_lang->get('CANT_WRITE_FS') . "\n
" . implode(",\n
", $err)); + } + + $manager->removeModule($module); + + if(!$manager->save()) + Response::jsonError($this->_lang->get('CANT_WRITE_FS') .' ' . $manager->getConfig()->getName()); + + // try to delete related files + if(!empty($filesToDelete)) + { + $err = array(); + foreach ($filesToDelete as $file) + { + if(!file_exists($file)) + continue; + + if(is_dir($file)){ + if(!File::rmdirRecursive($file , true)) + $err[] = $file; + } + else{ + if(!unlink($file)) + $err[] = $file; + } + } + + if(!empty($err)) + Response::jsonError($this->_lang->get('CANT_WRITE_FS') . "\n
".implode(",\n
", $err)); + } + + $this->createClassMap(); + Response::jsonSuccess(); + } + + /** + * Get list of image folders + */ + public function iconDirsAction() + { + $path = Request::post('node', 'string', ''); + $path = str_replace('.','', $path); + + $dirPath = $this->_configMain->get('wwwpath'); + + if(!is_dir($dirPath.$path)) + Response::jsonArray(array()); + + $files = File::scanFiles($dirPath . $path, false, false , File::Dirs_Only); + + if(empty($files)) + Response::jsonArray(array()); + + sort($files); + $list = array(); + + foreach($files as $k=>$fpath) + { + $text = basename($fpath); + + $obj = new stdClass(); + $obj->id = str_replace($dirPath, '', $fpath); + $obj->text = $text; + $obj->url = '/' . $obj->id; + + if(is_dir($fpath)) + { + $obj->expanded = false; + $obj->leaf = false; + } + else + { + $obj->leaf = true; + } + $list[] = $obj; + } + Response::jsonArray($list); + } + + /** + * Get image list + */ + public function iconListAction() + { + $dirPath = $this->_configMain->get('wwwpath'); + $dir = Request::post('dir', 'string', ''); + + if(!is_dir($dirPath . $dir)) + Response::jsonArray(array()); // windows & linux paths fix $scanPath = str_replace('//','/', $dirPath . $dir); - $files = File::scanFiles($scanPath, array('.jpg','.png','.gif','.jpeg') , false , File::Files_Only); - - if(empty($files)) - Response::jsonArray(array()); - - sort($files); - $list = array(); - - foreach($files as $k=>$fpath) - { - $text = basename($fpath); - $list[] = array( - 'name'=>$text, - 'url'=>str_replace($dirPath , $this->_configMain->get('wwwroot'), $fpath), - 'path'=>str_replace($dirPath, '', $fpath), - ); - } - Response::jsonSuccess($list); - } - - /** - * Rebuild class map - */ - public function rebuildMapAction() - { + $files = File::scanFiles($scanPath, array('.jpg','.png','.gif','.jpeg') , false , File::Files_Only); + + if(empty($files)) + Response::jsonArray(array()); + + sort($files); + $list = array(); + + foreach($files as $k=>$fpath) + { + $text = basename($fpath); + $list[] = array( + 'name'=>$text, + 'url'=>str_replace($dirPath , $this->_configMain->get('wwwroot'), $fpath), + 'path'=>str_replace($dirPath, '', $fpath), + ); + } + Response::jsonSuccess($list); + } + + /** + * Rebuild class map + */ + public function rebuildMapAction() + { $this->_checkCanEdit(); if(!$this->createClassMap()){ Response::jsonError(); } - Response::jsonSuccess(); - } + Response::jsonSuccess(); + } /** * */ - public function createClassMap() - { + public function createClassMap() + { $mapBuilder = new Classmap($this->_configMain); $mapBuilder->update(); return $mapBuilder->save(); - } - - /** - * Get desktop module info - */ - protected function desktopModuleInfo() - { - $projectData = []; - $projectData['includes']['js'][] = '/js/app/system/Modules.js'; - $projectData['includes']['js'][] = '/js/app/system/FilesystemWindow.js'; - $projectData['includes']['js'][] = '/js/app/system/IconField.js'; - /* + } + + /** + * Get desktop module info + */ + protected function desktopModuleInfo() + { + $projectData = []; + $projectData['includes']['js'][] = '/js/app/system/Modules.js'; + $projectData['includes']['js'][] = '/js/app/system/FilesystemWindow.js'; + $projectData['includes']['js'][] = '/js/app/system/IconField.js'; + /* * Module bootstrap */ - if(file_exists($this->_configMain->get('jsPath').'app/system/desktop/' . strtolower($this->_module) . '.js')) - $projectData['includes']['js'][] = '/js/app/system/desktop/' . strtolower($this->_module) .'.js'; + if(file_exists($this->_configMain->get('jsPath').'app/system/desktop/' . strtolower($this->_module) . '.js')) + $projectData['includes']['js'][] = '/js/app/system/desktop/' . strtolower($this->_module) .'.js'; - return $projectData; - } + return $projectData; + } } From b836ac13e18f927d719281e80d483552efe26f3c Mon Sep 17 00:00:00 2001 From: hartois Date: Wed, 13 Jul 2016 11:11:25 +0300 Subject: [PATCH 2/2] Modules Manager: update modules locale on addModule --- dvelum/app/Modules/Manager.php | 603 +++++++++++++++++---------------- 1 file changed, 305 insertions(+), 298 deletions(-) diff --git a/dvelum/app/Modules/Manager.php b/dvelum/app/Modules/Manager.php index 43472c51..472a73fd 100644 --- a/dvelum/app/Modules/Manager.php +++ b/dvelum/app/Modules/Manager.php @@ -4,22 +4,22 @@ */ class Modules_Manager { - protected $_config; - protected $_distConfig; + protected $_config; + protected $_distConfig; protected $_curConfig = false; - protected $_mainconfigKey = 'backend_modules'; - /** - * @var Config_Abstract - */ - protected $_appConfig; - /** - * @var Config_File_Array - */ - protected $_modulesLocale; - - static protected $_classRoutes = false; - - public function __construct() + protected $_mainconfigKey = 'backend_modules'; + /** + * @var Config_Abstract + */ + protected $_appConfig; + /** + * @var Config_File_Array + */ + protected $_modulesLocale; + + static protected $_classRoutes = false; + + public function __construct() { $this->_appConfig = Registry::get('main', 'config'); $configPath = $this->_appConfig->get($this->_mainconfigKey); @@ -31,9 +31,9 @@ public function __construct() $this->_curConfig = new Config_File_Array(Config::storage()->getWrite() . $configPath); } - $locale = Lang::lang()->getName(); - $this->_modulesLocale = Lang::storage()->get($locale.'/modules/'.basename($configPath)); - } + $locale = Lang::lang()->getName(); + $this->_modulesLocale = Lang::storage()->get($locale.'/modules/'.basename($configPath)); + } /** * Get Modules menu localization @@ -47,293 +47,300 @@ public function getLocale($lang) return Lang::storage()->get($lang.'/modules/'.basename($configPath)); } - /** - * Get registered modules - * @return array - */ - public function getRegisteredModules() - { - $data = $this->_config->__toArray(); - return array_keys($data); - } - - /** - * Check if module exists - * @param string $name - * @return boolean - */ - public function isValidModule($name) - { - return $this->_config->offsetExists($name); - } - - /** - * Get module configuration - * @param string $name - * @return array - */ - public function getModuleConfig($name) - { - $data = $this->_config->get($name); - $data['title'] = $name; - - if(isset($this->_modulesLocale[$name])) - $data['title'] = $this->_modulesLocale[$name]; - - return $data; - } - - /** - * Get Module class - * @param string $name - * @return boolean false | string - */ - public function getModuleController($name) - { - if(!$this->isValidModule($name)) - return false; - - $cfg = $this->_config->get($name); - if(isset($cfg['class']) && !empty($cfg['class'])) - return $cfg['class']; - else - return ''; - } - - /** - * Get module name for class - * @param string $class - * @return string - */ - public function getModuleName($class) - { - return Utils_String::formatClassName(strtolower(str_replace(array('Backend_','_Controller'), '', $class))); - } - - /** - * Get module name for controller - * @param string $controller - * @return boolean false | string - */ - public function getControllerModule($controller) - { - if(!self::$_classRoutes){ - $config = $this->_config->__toArray(); - - foreach ($config as $module=>$cfg) - self::$_classRoutes[$cfg['class']] = $module; - } - - if(!isset(self::$_classRoutes[$controller])) - return false; - else - return self::$_classRoutes[$controller]; - } - - /** - * Get modules list - * @return array: - */ - public function getList() - { - $data = $this->_config->__toArray(); - foreach ($data as $module=>&$cfg) - { - if($this->_curConfig && $this->_curConfig->offsetExists($module)){ - $cfg['dist'] = false; - }else{ - $cfg['dist'] = true; - } - - if(!isset($cfg['in_menu'])) - $cfg['in_menu'] = true; - - if($this->_modulesLocale->offsetExists($module)){ - $cfg['title'] = $this->_modulesLocale->get($module); - }else{ - $cfg['title'] = $module; - } - - }unset($cfg); - return $data; - } - - /** - * Remove module - * @param string $name - * @return boolean - */ - public function removeModule($name) - { - if($this->_config->offsetExists($name)) - $this->_config->remove($name); - - if($this->_modulesLocale->offsetExists($name)) - $this->_modulesLocale->remove($name); - - if(!$this->_modulesLocale->save()) - return false; - - return $this->save(); - } - - /** - * Add module - * @param string $name - * @param array $config - * @return boolean - */ - public function addModule($name , array $config) - { - $this->_config->set($name , $config); - $this->resetCache(); - return $this->save(); - } - - /** - * Update module data - * @param $name - * @param array $data - * @return boolean - */ - public function updateModule($name , array $data) - { - if($name !== $data['id']){ - $this->_modulesLocale->remove($name); - $this->_config->remove($name); - } - - if(isset($data['title'])) - { - $this->_modulesLocale->set($data['id'] , $data['title']); - if(!$this->_modulesLocale->save()){ - return false; - } - unset($data['title']); - } - $this->_config->set($data['id'] , $data); - return $this->save(); - } - - /** - * Save modules config - * @return boolean - */ - public function save() - { - $this->resetCache(); - return $this->_config->save(); - } - /** - * Reset modules cache - */ - public function resetCache() - { - self::$_classRoutes = false; - Config::resetCache(); - } - /** - * Get configuration object - * @return Config_Abstract - */ - public function getConfig() - { - return $this->_config; - } - - /** - * Get list of Controllers - * @return array - */ - public function getControllers() - { - $backendConfig = Config::storage()->get('backend.php'); - $autoloadCfg = $this->_appConfig->get('autoloader'); - $systemControllers = $backendConfig->get('system_controllers'); - - $paths = $autoloadCfg['paths']; - $dir = $this->_appConfig->get('backend_controllers_dir'); - - $data = array(); - - foreach($paths as $path) - { - if(basename($path) === 'modules') - { - $folders = File::scanFiles($path, false, true, File::Dirs_Only); - - if(empty($folders)) - continue; - - foreach($folders as $item) - { - if(!is_dir($item.'/'.$dir)){ - continue; - } - $this->findControllers($item.'/'.$dir, $systemControllers, $data , ucfirst(basename($item)).'_'.$dir.'_'); - } - }else{ - if(!is_dir($path.'/'.$dir)){ - continue; - } - $this->findControllers($path.'/'.$dir,$systemControllers, $data, $dir.'_'); - } - - } - return array_values($data); - } - - /** - * Find controller files - * @param $path - * @param & array $result - */ - public function findControllers($path, $skipList, & $result, $classPrefix = '') - { - $folders = File::scanFiles($path, false, true, File::Dirs_Only); - - if(empty($folders)) - return; - - foreach ($folders as $item) - { - $name = basename($item); - - if(file_exists($item.'/Controller.php')) - { - $name = str_replace($path.'/', '', $item.'/Controller.php'); - $name = $classPrefix . Utils::classFromPath($name); - - /* + /** + * Get registered modules + * @return array + */ + public function getRegisteredModules() + { + $data = $this->_config->__toArray(); + return array_keys($data); + } + + /** + * Check if module exists + * @param string $name + * @return boolean + */ + public function isValidModule($name) + { + return $this->_config->offsetExists($name); + } + + /** + * Get module configuration + * @param string $name + * @return array + */ + public function getModuleConfig($name) + { + $data = $this->_config->get($name); + $data['title'] = $name; + + if(isset($this->_modulesLocale[$name])) + $data['title'] = $this->_modulesLocale[$name]; + + return $data; + } + + /** + * Get Module class + * @param string $name + * @return boolean false | string + */ + public function getModuleController($name) + { + if(!$this->isValidModule($name)) + return false; + + $cfg = $this->_config->get($name); + if(isset($cfg['class']) && !empty($cfg['class'])) + return $cfg['class']; + else + return ''; + } + + /** + * Get module name for class + * @param string $class + * @return string + */ + public function getModuleName($class) + { + return Utils_String::formatClassName(strtolower(str_replace(array('Backend_','_Controller'), '', $class))); + } + + /** + * Get module name for controller + * @param string $controller + * @return boolean false | string + */ + public function getControllerModule($controller) + { + if(!self::$_classRoutes){ + $config = $this->_config->__toArray(); + + foreach ($config as $module=>$cfg) + self::$_classRoutes[$cfg['class']] = $module; + } + + if(!isset(self::$_classRoutes[$controller])) + return false; + else + return self::$_classRoutes[$controller]; + } + + /** + * Get modules list + * @return array: + */ + public function getList() + { + $data = $this->_config->__toArray(); + foreach ($data as $module=>&$cfg) + { + if($this->_curConfig && $this->_curConfig->offsetExists($module)){ + $cfg['dist'] = false; + }else{ + $cfg['dist'] = true; + } + + if(!isset($cfg['in_menu'])) + $cfg['in_menu'] = true; + + if($this->_modulesLocale->offsetExists($module)){ + $cfg['title'] = $this->_modulesLocale->get($module); + }else{ + $cfg['title'] = $module; + } + + }unset($cfg); + return $data; + } + + /** + * Remove module + * @param string $name + * @return boolean + */ + public function removeModule($name) + { + if($this->_config->offsetExists($name)) + $this->_config->remove($name); + + if($this->_modulesLocale->offsetExists($name)) + $this->_modulesLocale->remove($name); + + if(!$this->_modulesLocale->save()) + return false; + + return $this->save(); + } + + /** + * Add module + * @param string $name + * @param array $config + * @return boolean + */ + public function addModule($name , array $config) + { + $this->_config->set($name , $config); + $this->resetCache(); + $ret = $this->save(); + if($ret && isset($config['title'])){ + $this->_modulesLocale->set($config['id'], $config['title']); + if(!$this->_modulesLocale->save()){ + return false; + } + } + return $ret; + } + + /** + * Update module data + * @param $name + * @param array $data + * @return boolean + */ + public function updateModule($name , array $data) + { + if($name !== $data['id']){ + $this->_modulesLocale->remove($name); + $this->_config->remove($name); + } + + if(isset($data['title'])) + { + $this->_modulesLocale->set($data['id'] , $data['title']); + if(!$this->_modulesLocale->save()){ + return false; + } + unset($data['title']); + } + $this->_config->set($data['id'] , $data); + return $this->save(); + } + + /** + * Save modules config + * @return boolean + */ + public function save() + { + $this->resetCache(); + return $this->_config->save(); + } + /** + * Reset modules cache + */ + public function resetCache() + { + self::$_classRoutes = false; + Config::resetCache(); + } + /** + * Get configuration object + * @return Config_Abstract + */ + public function getConfig() + { + return $this->_config; + } + + /** + * Get list of Controllers + * @return array + */ + public function getControllers() + { + $backendConfig = Config::storage()->get('backend.php'); + $autoloadCfg = $this->_appConfig->get('autoloader'); + $systemControllers = $backendConfig->get('system_controllers'); + + $paths = $autoloadCfg['paths']; + $dir = $this->_appConfig->get('backend_controllers_dir'); + + $data = array(); + + foreach($paths as $path) + { + if(basename($path) === 'modules') + { + $folders = File::scanFiles($path, false, true, File::Dirs_Only); + + if(empty($folders)) + continue; + + foreach($folders as $item) + { + if(!is_dir($item.'/'.$dir)){ + continue; + } + $this->findControllers($item.'/'.$dir, $systemControllers, $data , ucfirst(basename($item)).'_'.$dir.'_'); + } + }else{ + if(!is_dir($path.'/'.$dir)){ + continue; + } + $this->findControllers($path.'/'.$dir,$systemControllers, $data, $dir.'_'); + } + + } + return array_values($data); + } + + /** + * Find controller files + * @param $path + * @param & array $result + */ + public function findControllers($path, $skipList, & $result, $classPrefix = '') + { + $folders = File::scanFiles($path, false, true, File::Dirs_Only); + + if(empty($folders)) + return; + + foreach ($folders as $item) + { + $name = basename($item); + + if(file_exists($item.'/Controller.php')) + { + $name = str_replace($path.'/', '', $item.'/Controller.php'); + $name = $classPrefix . Utils::classFromPath($name); + + /* * Skip system controller */ - if(in_array($name, $skipList , true)) - continue; + if(in_array($name, $skipList , true)) + continue; - $result[$name] = array('id'=>$name,'title'=>$name); - } + $result[$name] = array('id'=>$name,'title'=>$name); + } - $this->findControllers($item, $skipList, $result, $classPrefix); - } - } + $this->findControllers($item, $skipList, $result, $classPrefix); + } + } - /** - * Get list of controllers without modules - */ - public function getAvailableControllers() - { - $list = $this->getControllers(); + /** + * Get list of controllers without modules + */ + public function getAvailableControllers() + { + $list = $this->getControllers(); - /* + /* * Hide registered controllers */ - $registered = array_flip(Utils::fetchCol('class' , $this->getConfig()->__toArray())); - foreach($list as $k=>$v){ - if(isset($registered[$v['id']])){ - unset($list[$k]); - } - } - return array_values($list); - } + $registered = array_flip(Utils::fetchCol('class' , $this->getConfig()->__toArray())); + foreach($list as $k=>$v){ + if(isset($registered[$v['id']])){ + unset($list[$k]); + } + } + return array_values($list); + } } \ No newline at end of file