Пример #1
0
    public static function add($typeName, $typeDesc, $fields, $allowedChildren, $allowedParents, $module = 'type', $generateClasses = true, $formbuilderStructure = false, $createHMVC = false, $seo = false)
    {
        $typesTable = new K_Tree_Types_Model();
        if (!preg_match('/[a-z0-9.-]+/s', $typeName)) {
            throw new Exception('Wrong type name: ' . $typeName);
        }
        if (!is_array($fields) || empty($fields)) {
            if (json_decode($fields) != null) {
                $fields = json_decode($fields);
                $fields = self::objectToArray($fields);
            } else {
                $fields = array();
                //throw new Exception('Cannot create empty type: '.$typeName);
            }
        }
        if (!is_array($allowedChildren)) {
            $allowedChildren = (array) $allowedChildren;
            foreach ($allowedChildren as $key => $value) {
                if ($value == 'Все') {
                    $allowedChildren[$key] = 'all';
                }
                if ($value == 'Нет') {
                    $allowedChildren = array();
                    break;
                }
            }
            //throw new Exception('Childrens must be array: '.$typeName);
        }
        if (!is_array($allowedParents)) {
            $allowedParents = (array) $allowedParents;
            foreach ($allowedParents as $key => $value) {
                if ($value == 'Все') {
                    $allowedParents[$key] = 'all';
                }
                if ($value == 'Нет') {
                    $allowedParents = array();
                    break;
                }
            }
            //throw new Exception('Parents must be array: '.$typeName);
        }
        if (!is_dir(APP_PATH . '/' . $module)) {
            throw new Exception('Wrong module directory: ' . $typeName);
        }
        $pageExists = $typesTable->count(K_Db_Select::create()->where('`type_name` = "' . $typeName . '"'));
        if ($pageExists > 0) {
            throw new Exception('Current type already exists: ' . $typeName);
        }
        $time = time();
        $insertIntoTypesData = array('type_name' => $typeName, 'type_desc' => $typeDesc, 'type_fields' => serialize($formbuilderStructure), 'types_module' => $module, 'type_added' => $time, 'type_modified' => $time);
        $insertId = $typesTable->save($insertIntoTypesData);
        $newTableName = 'type_' . $typeName;
        $seoFields = array('title', 'keys', 'h1', 'desc');
        $sql = 'CREATE TABLE IF NOT EXISTS`' . $newTableName . '` (
			`' . $newTableName . '_id` INT UNSIGNED NOT NULL ,`' . $newTableName . '_pid` INT UNSIGNED NOT NULL,';
        // если сео тип то добавляем поля с префиксом SEO
        foreach ($fields as $fieldId => $field) {
            if ($field['type'] == 'submit' || $field['type'] == 'reset') {
                continue;
            }
            $sql .= '`' . $newTableName . '_' . $field['values']['name'] . '` ' . self::setType($field['type'], $field['vlds']) . ' NOT NULL ,';
        }
        // если сео тип то добавляем поля с префиксом SEO
        if ($seo) {
            foreach ($seoFields as $field) {
                $sql .= '`' . 'seo' . '_' . $field . '` VARCHAR(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,';
            }
        }
        $sql .= 'PRIMARY KEY (`' . $newTableName . '_id`)
		) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;';
        $query = new K_Db_Query();
        $query->q($sql);
        if ($generateClasses) {
            self::generateModel($typeName, $module);
            self::generateController($typeName, $module, $allowedChildren, $allowedParents, $fields);
            self::generateGUI($typeName, $module, $allowedChildren, $allowedParents, $fields, $seo);
        }
        if ($createHMVC) {
            self::generateTypeBlockController($typeName, array('type' => ucfirst($typeName)));
            self::generateTypeBlockTemplates($typeName);
        }
    }