示例#1
0
 /**
  * Gets the field settings for a model. Adds in default settings if there aren't any specified.
  * @param string $class_name
  * @return array Associative array of field settings
  */
 public static function getFieldSettings($class_name)
 {
     if (isset(static::$field_settings[$class_name])) {
         return static::$field_settings[$class_name];
     }
     $fields = $class_name::fields();
     $fields_types = \Config::get('cmf.fields_types');
     $metadata = $class_name::metadata();
     $alias = false;
     $visibleFields = array();
     // If we are trying to create an alias, we need to check for a URL field
     if (\Input::param('alias', false) !== false) {
         // Check for URL...
         $urls = $metadata->getAssociationsByTargetClass('CMF\\Model\\URL');
         if (count($urls) > 0) {
             // Find the URL field and hide all others if found
             foreach ($urls as $urlFieldName => $urlField) {
                 if ($urlField['orphanRemoval']) {
                     $visibleFields[] = $urlFieldName;
                     $fields[$urlFieldName]['before'] = null;
                     $fields[$urlFieldName]['after'] = null;
                     array_walk($fields, function (&$item, $key) use($urlFieldName) {
                         $item['visible'] = $key == $urlFieldName;
                         $item['template'] = null;
                     });
                     $title_fields = array('menu_title', 'title', 'name', 'label');
                     $title_field = null;
                     foreach ($title_fields as $title_field) {
                         if ($metadata->hasField($title_field)) {
                             $fields[$title_field]['visible'] = true;
                             $fields[$title_field]['before'] = $urlFieldName;
                             $fields[$title_field]['after'] = null;
                             $fields[$title_field]['template'] = null;
                             $visibleFields[] = $title_field;
                             break;
                         }
                         $i++;
                     }
                     foreach ($title_fields as $title_field2) {
                         if ($title_field2 == $title_field) {
                             continue;
                         }
                         if ($metadata->hasField($title_field2)) {
                             $fields[$title_field2]['template'] = '{{' . $title_field . '}}';
                         }
                     }
                     $alias = true;
                     break;
                 }
             }
         }
     }
     $field_mappings = $metadata->fieldMappings;
     $association_mappings = $metadata->associationMappings;
     $field_list = array_keys(\Arr::merge($fields, $metadata->reflFields));
     foreach ($field_list as $key) {
         $field = isset($fields[$key]) ? $fields[$key] : array();
         if ($field === true) {
             $field = array('visible' => !$alias);
         } else {
             if ($field === false) {
                 $field = array('visible' => false);
             }
         }
         if (isset($field_mappings[$key])) {
             // It's a normal field
             $mapping = $field_mappings[$key];
         } else {
             if (isset($association_mappings[$key])) {
                 // It's an association.
                 $mapping = $association_mappings[$key];
                 $association_type = static::$association_types[$mapping['type']];
                 if ($alias && !in_array($key, $visibleFields)) {
                     $field['visible'] = false;
                 }
                 // If there's a custom type defined based on the table name, use it
                 $special_mapping_type = $association_type . '_' . static::getTableForClass($mapping['targetEntity']);
                 // If that custom type doesn't exist, try and use the '_inline' version for associations with orphanRemoval = true
                 // Also an inline version can be invoked by having an 'inline' setting on the field. If this value is a string then it
                 // will also get appended to the inline mapping type. Eg. 'onetomany_inline_stacked' if 'inline' => 'stacked'
                 if (!isset($fields_types[$special_mapping_type]) && ($mapping['orphanRemoval'] === true || isset($field['inline']))) {
                     $special_mapping_type = $association_type . '_inline' . (isset($field['inline']) && is_string($field['inline']) ? '_' . $field['inline'] : '');
                 }
                 $mapping['type'] = isset($fields_types[$special_mapping_type]) ? $special_mapping_type : $association_type;
             } else {
                 if (property_exists($class_name, $key)) {
                     // It's just a property of the class, no mapping info
                     if (!isset($field['field'])) {
                         $field['field'] = 'CMF\\Field\\Base';
                     }
                     if (!isset($field['title'])) {
                         $field['title'] = \Admin::getFieldDisplayAttribute($class_name, $key, 'title', \Inflector::humanize($key));
                     }
                     $field['mapping'] = array('fieldName' => $key);
                     $fields[$key] = $field;
                     continue;
                 } else {
                     // This field can't be used. Remove it from the output
                     if (isset($fields[$key])) {
                         unset($fields[$key]);
                     }
                     continue;
                 }
             }
         }
         $field['mapping'] = $mapping;
         // Add in the field class from the field types map if it's not there
         if (!isset($field['field'])) {
             $field['field'] = isset($fields_types[$mapping['type']]) ? $fields_types[$mapping['type']] : 'CMF\\Field\\None';
         }
         // Set up translated attributes
         $field_class = $field['field'];
         $translatableAttrs = $field_class::getTranslatableAttributes();
         if (is_array($translatableAttrs)) {
             foreach ($translatableAttrs as $attr) {
                 if ($attr == 'title') {
                     continue;
                 }
                 $value = \Arr::get($field, $attr);
                 if (empty($value) || !is_string($value)) {
                     continue;
                 }
                 \Arr::set($field, $attr, \Admin::getFieldDisplayAttribute($class_name, $key, $attr, $value));
             }
             $field['title'] = \Admin::getFieldDisplayAttribute($class_name, $key, 'title', isset($field['title']) ? $field['title'] : \Inflector::humanize($key));
         }
         $fields[$key] = $field;
     }
     return static::$field_settings[$class_name] = $fields;
 }