/** * Генератор названий обработчиков * * @param string $event * @param string $element * @return array */ private function getHandlerNames($event = '', $element = '') { // получим основные названия обработчиков $handlers = array(); $handlers[] = $event . '_' . $element; $e = $element; while (mb_strpos($e, '_') !== false) { $e = CUtils::strLeftBack($e, '_'); $handlers[] = $event . '_' . $e; } $handlers[] = $event; // для цифровых обработчиков сгенерируем нецифровые аналоги $pattern = '/(_[0-9]+)/'; foreach ($handlers as $handler) { if (preg_match($pattern, $handler) > 0) { $h = preg_replace($pattern, '', $handler); if (!in_array($h, $handlers)) { $handlers[] = $h; } } } // сгенерируем дополнительно полный список $result = array(); foreach ($handlers as $handler) { $result[] = 'handle_before_' . $handler; $result[] = 'handle_' . $handler; $result[] = 'handle_after_' . $handler; } // для отладки пропишем все обработчики в бин $this->getStatefullFormBean()->getElement($element)->setHandlers($result); return $result; }
/** * @return CUserRole */ public static function getCurrentTask() { $self = $_SERVER["PHP_SELF"]; $root_folder = CSettingsManager::getSettingValue("root_folder"); $self = str_replace($root_folder, "", $self); if (substr($self, 0, 1) == "/") { $self = substr($self, 1); } /** * Начинаем поиск задач, походящих под описание. Если не находим, * то отрезаем справа / */ $queryCount = 0; $tasks = CActiveRecordProvider::getWithCondition(TABLE_USER_ROLES, "url='" . $self . "'"); while ($tasks->getCount() == 0 && strlen($self) > 1 && $queryCount <= 5) { $self = CUtils::strLeftBack($self, "/"); if (substr($self, strlen($self) - 1) != "/") { $self .= "/"; } $tasks = CActiveRecordProvider::getWithCondition(TABLE_USER_ROLES, "url='" . $self . "'"); $queryCount++; } $task = null; if ($tasks->getCount() > 0) { foreach ($tasks->getItems() as $ar) { $task = new CUserRole($ar); } } return $task; }
public function actionGenerate() { $params = new CGeneratableController(); $params->setAttributes(CRequest::getArray($params::getClassName())); if ($params->validate()) { $files = array(); /** * Создаем сам контроллер */ $templateController = file_get_contents(CORE_CWD . "/_core/_models/generator/templates/controller.tpl"); $controllerFields = array("controllerName" => $params->controllerName, "controllerFile" => CUtils::strRightBack($params->controllerFile, CORE_DS), "controllerPath" => $params->controllerPath, "pageTitle" => $params->pageTitle, "modelName" => $params->modelName, "modelTable" => $params->modelTable, "modelManager" => $params->modelManager, "modelManagerGetter" => $params->modelManagerGetter, "viewPath" => $params->viewPath); foreach ($controllerFields as $key => $value) { $templateController = str_replace("#" . $key . "#", $value, $templateController); } $filePath = CORE_CWD . CORE_DS . $params->controllerPath . $params->controllerName . ".class.php"; CUtils::createFoldersToPath(CUtils::strLeftBack($filePath, CORE_DS)); file_put_contents($filePath, $templateController); $files[] = $filePath; /** * Создаем модуль, которым этот контроллер рулит */ $templateModule = file_get_contents(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "module.tpl"); $moduleFields = array("controllerName" => $params->controllerName); foreach ($moduleFields as $key => $value) { $templateModule = str_replace("#" . $key . "#", $value, $templateModule); } $filePath = CORE_CWD . CORE_DS . $params->controllerFile; CUtils::createFoldersToPath(CUtils::strLeftBack($filePath, CORE_DS)); file_put_contents($filePath, $templateModule); $files[] = $filePath; /** * Модель данных, тоже пусть будет для полноты картины */ if ($params->modelGenerate == 1) { $templateModel = file_get_contents(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "model.tpl"); $modelFields = array("modelName" => $params->modelName, "modelTable" => $params->modelTable); foreach ($modelFields as $key => $value) { $templateModel = str_replace("#" . $key . "#", $value, $templateModel); } $filePath = CORE_CWD . CORE_DS . $params->modelPath . $params->modelName . ".class.php"; CUtils::createFoldersToPath(CUtils::strLeftBack($filePath, CORE_DS)); file_put_contents($filePath, $templateModel); $files[] = $filePath; } /** * Получим список полей модели, которые хранятся в БД * и из них сразу наклепаем столбцов в таблице и полей в форме */ $fields = array(); $modelName = $params->modelName; $model = new $modelName(); if (is_a($model, "CActiveModel")) { foreach ($model->getDbTableFields()->getItems() as $field) { $fields[] = $field->name; } } $viewFormFields = array(); $viewTableHeadFields = array(); $viewTableBodyFields = array(); foreach ($fields as $field) { $templateField = file_get_contents(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "field.text.tpl"); $fieldFields = array("fieldTitle" => $field); foreach ($fieldFields as $key => $value) { $templateField = str_replace("#" . $key . "#", $value, $templateField); } $viewFormFields[] = $templateField; $templateField = file_get_contents(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "field.tablebody.tpl"); $fieldFields = array("fieldTitle" => $field); foreach ($fieldFields as $key => $value) { $templateField = str_replace("#" . $key . "#", $value, $templateField); } $viewTableBodyFields[] = $templateField; $templateField = file_get_contents(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "field.tablehead.tpl"); $fieldFields = array("fieldTitle" => $field); foreach ($fieldFields as $key => $value) { $templateField = str_replace("#" . $key . "#", $value, $templateField); } $viewTableHeadFields[] = $templateField; } $viewFormFields = implode("\n\n", $viewFormFields); $viewTableHeadFields = implode("\n", $viewTableHeadFields); $viewTableBodyFields = implode("\n", $viewTableBodyFields); /** * Набор представлений на все случаи жизни */ CUtils::createFoldersToPath(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_views" . CORE_DS . $params->viewPath); $viewFiles = array(CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "index.tpl", CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "common.right.tpl", CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "add.tpl", CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "edit.tpl", CORE_CWD . CORE_DS . "_core" . CORE_DS . "_models" . CORE_DS . "generator" . CORE_DS . "templates" . CORE_DS . "form.tpl"); $viewFields = array("viewIndexTitle" => $params->viewIndexTitle, "viewIndexNoObjects" => $params->viewIndexNoObjects, "viewObjectSingleName" => $params->viewObjectSingleName, "viewObjectSingleNameRP" => $params->viewObjectSingleNameRP, "controllerFile" => CUtils::strRightBack($params->controllerFile, CORE_DS), "viewPath" => $params->viewPath, "viewFormFields" => $viewFormFields, "viewTableHeadFields" => $viewTableHeadFields, "viewTableBodyFields" => $viewTableBodyFields); foreach ($viewFiles as $viewFile) { $templateView = file_get_contents($viewFile); foreach ($viewFields as $key => $value) { $templateView = str_replace("#" . $key . "#", $value, $templateView); } $filePath = CORE_CWD . CORE_DS . "_core" . CORE_DS . "_views" . CORE_DS . $params->viewPath . CORE_DS . CUtils::strRightBack($viewFile, CORE_DS); file_put_contents($filePath, $templateView); $files[] = $filePath; } $this->setData("files", $files); $this->renderView("__generator/controller/success.tpl"); return true; } $this->setData("controller", $params); $this->renderView("__generator/controller/add.tpl"); }