FOF30\Factory\Scaffolding\Layout\FormErector::build PHP Метод

build() публичный Метод

public build ( )
    public function build()
    {
        // Get a reference to the model
        $model = $this->model;
        // Create the attributes of the form's base element
        $this->xml->addAttribute('validate', 'true');
        // Create the fieldset sections of the form file
        $labelKey = $this->getLangKeyPrefix() . 'GROUP_BASIC';
        $this->addString($labelKey, 'Basic');
        $fieldSet = $this->xml->addChild('fieldset');
        $fieldSet->addAttribute('name', 'scaffolding');
        $fieldSet->addAttribute('label', $labelKey);
        // Get the database fields
        $allFields = $model->getTableFields();
        // Ordering is not included
        if ($model->hasField('ordering')) {
            $fieldName = $model->getFieldAlias('ordering');
            unset($allFields[$fieldName]);
        }
        // Primary key is not inclided
        $primaryKeyField = $model->getKeyName();
        unset($allFields[$primaryKeyField]);
        // Get a list of "do not display" fields
        $doNotShow = $this->getDoNotShow();
        foreach ($allFields as $fieldName => $fieldDefinition) {
            // Skip the fields which shouldn't be displayed
            if (in_array($fieldName, $doNotShow)) {
                continue;
            }
            // Get the lowercase field name and prepare to handle specially named fields
            $lowercaseFieldName = strtolower($fieldName);
            // access => AccessLevel
            if ($model->getFieldAlias('access') == $fieldName) {
                $this->applyAccessLevelField($model, $fieldSet, $fieldName);
                continue;
            }
            // tag => Tag
            if ($model->getFieldAlias('tag') == $fieldName) {
                $this->applyTagField($model, $fieldSet, $fieldName);
                continue;
            }
            // enabled => Published
            if ($model->getFieldAlias('enabled') == $fieldName) {
                $this->applyPublishedField($model, $fieldSet, $fieldName);
                continue;
            }
            // cache_handler => CacheHandler
            if ($lowercaseFieldName == 'cache_handler') {
                $this->applyCacheHandlerField($model, $fieldSet, $fieldName);
                continue;
            }
            // component_id => Components
            if ($lowercaseFieldName == 'component_id') {
                $this->applyComponentsField($model, $fieldSet, $fieldName);
                continue;
            }
            // body, introtext, fulltext, description => Editor
            if (in_array($lowercaseFieldName, array('body', 'introtext', 'fulltext', 'description'))) {
                $this->applyEditorField($model, $fieldSet, $fieldName);
                continue;
            }
            // email, *_email => Email
            if ($lowercaseFieldName == 'email' || substr($lowercaseFieldName, -6) == 'email') {
                $this->applyEmailField($model, $fieldSet, $fieldName);
                continue;
            }
            // image, media, *_image => Media
            if (in_array($lowercaseFieldName, array('image', 'media')) || substr($lowercaseFieldName, -6) == '_image') {
                $this->applyMediaField($model, $fieldSet, $fieldName);
                continue;
            }
            // language, lang, lang_id => Language
            if (in_array($lowercaseFieldName, array('language', 'lang', 'lang_id'))) {
                $this->applyLanguageField($model, $fieldSet, $fieldName);
                continue;
            }
            // password, passwd, pass => Password
            if (in_array($lowercaseFieldName, array('password', 'passwd', 'pass'))) {
                $this->applyPasswordField($model, $fieldSet, $fieldName);
                continue;
            }
            // plugin_id => Plugins
            if ($lowercaseFieldName == 'plugin_id') {
                $this->applyPluginsField($model, $fieldSet, $fieldName);
                continue;
            }
            // asset_id => Rules (new tab)
            if ($lowercaseFieldName == 'asset_id') {
                // Do not show the rules tab in read views
                if (!$this->addDescriptions) {
                    continue;
                }
                $this->xml->addAttribute('tabbed', 1);
                $fieldSet->addAttribute('class', 'tab-pane active');
                $rulesSet = $this->xml->addChild('fieldset');
                $baseKey = $this->getLangKeyPrefix() . 'GROUP_PERMISSIONS';
                $this->addString($baseKey, 'Permissions');
                $this->addString($baseKey . '_DESC', 'Permissions for ' . $this->model->getContainer()->inflector->singularize($this->viewName));
                $rulesSet->addAttribute('name', 'rules');
                $rulesSet->addAttribute('class', 'tab-pane');
                $rulesSet->addAttribute('label', $baseKey);
                if ($this->addDescriptions) {
                    $rulesSet->addAttribute('description', $baseKey . '_DESC');
                }
                $field = $rulesSet->addChild('field');
                $field->addAttribute('type', 'Hidden');
                $field->addAttribute('emptylabel', 'true');
                $field->addAttribute('filter', 'unset');
                $field->addAttribute('name', $model->getFieldAlias('asset_id'));
                $field = $rulesSet->addChild('field');
                $field->addAttribute('name', 'rules');
                $field->addAttribute('type', 'Rules');
                $field->addAttribute('emptylabel', 'true');
                $field->addAttribute('translate_label', 'false');
                $field->addAttribute('filter', 'rules');
                $field->addAttribute('validate', 'rules');
                $field->addAttribute('section', 'component');
                $field->addAttribute('component', $this->builder->getContainer()->componentName);
                continue;
            }
            // session_handler => SessionHandler
            if ($lowercaseFieldName == 'session_handler') {
                $this->applySessionHandlerField($model, $fieldSet, $fieldName);
                continue;
            }
            // tel, telephone, phone => Tel
            if (in_array($lowercaseFieldName, array('tel', 'telephone', 'phone'))) {
                $this->applyTelField($model, $fieldSet, $fieldName);
                continue;
            }
            // timezone, tz, time_zone => Timezone
            if (in_array($lowercaseFieldName, array('timezone', 'tz', 'time_zone'))) {
                $this->applyTimezoneField($model, $fieldSet, $fieldName);
                continue;
            }
            // url, link, href => Url
            if (in_array($lowercaseFieldName, array('url', 'link', 'href'))) {
                $this->applyUrlField($model, $fieldSet, $fieldName);
                continue;
            }
            // user, user_id, userid, uid => User
            if (in_array($lowercaseFieldName, array('user', 'user_id', 'userid', 'uid'))) {
                $this->applyUserField($model, $fieldSet, $fieldName);
                continue;
            }
            // group, group_id, groupid, gid => UserGroup
            if (in_array($lowercaseFieldName, array('group', 'group_id', 'groupid', 'gid'))) {
                $this->applyUserGroupField($model, $fieldSet, $fieldName);
                continue;
            }
            // Special handling for myComponent_whatever_id fields
            $myComponentPrefix = $this->builder->getContainer()->bareComponentName . '_';
            if (strpos($fieldName, $myComponentPrefix) === 0 && substr($fieldName, -3) == '_id') {
                $parts = explode('_', $fieldName);
                array_pop($parts);
                array_shift($parts);
                // myComponent_something_id => Relation or Model
                if (count($parts) == 1) {
                    $foreignName = array_shift($parts);
                } else {
                    $foreignName1 = array_shift($parts);
                    $foreignName1 = $this->model->getContainer()->inflector->pluralize($foreignName1);
                    $foreignName2 = array_shift($parts);
                    $foreignName2 = $this->model->getContainer()->inflector->pluralize($foreignName2);
                    $modelName = $model->getName();
                    $modelName = $this->model->getContainer()->inflector->pluralize($modelName);
                    $foreignName = $foreignName1 == $modelName ? $foreignName2 : $foreignName1;
                }
                try {
                    if (empty($parts)) {
                        throw new DataModel\Relation\Exception\RelationNotFound();
                    }
                    $model->getRelations()->getRelation($parts[0]);
                    $this->applyRelationField($model, $fieldSet, $fieldName);
                    continue;
                } catch (DataModel\Relation\Exception\RelationNotFound $e) {
                    $foreignName = $this->model->getContainer()->inflector->pluralize($foreignName);
                    try {
                        $this->applyModelField($model, $fieldSet, $fieldName, $foreignName);
                        continue;
                    } catch (\Exception $e) {
                    }
                }
            }
            // Other fields, use getFieldType
            $typeDef = $this->getFieldType($fieldDefinition->Type);
            switch ($typeDef['type']) {
                case 'Text':
                    $this->applyTextField($model, $fieldSet, $fieldName);
                    break;
                case 'Editor':
                    $this->applyEditorField($model, $fieldSet, $fieldName);
                    break;
                case 'Calendar':
                    $this->applyCalendarField($model, $fieldSet, $fieldName);
                    break;
                case 'Checkbox':
                    $this->applyCheckboxField($model, $fieldSet, $fieldName);
                    break;
                case 'Integer':
                    $this->applyIntegerField($model, $fieldSet, $fieldName);
                    break;
                case 'Numeric':
                    $this->applyNumericField($model, $fieldSet, $fieldName);
                    break;
                case 'GenericList':
                    $this->applyGenericListField($model, $fieldSet, $fieldName, $typeDef['params']);
                    break;
            }
        }
        $this->pushResults();
    }

Usage Example

Пример #1
0
 public function build()
 {
     $this->addDescriptions = false;
     parent::build();
     $this->xml->addAttribute('type', 'read');
     $this->pushResults();
 }