/** inheritdoc */ public static function displayForm($value, &$settings, $model) { // Set up the values for the select $values = array(); if (isset($value) && $value instanceof \Doctrine\Common\Collections\Collection) { foreach ($value as $val) { $values[] = strval($val->get('id')); } } $target_prop = $settings['mapping']['isOwningSide'] === true ? $settings['mapping']['inversedBy'] : $settings['mapping']['mappedBy']; if (empty($target_prop) || is_null($model->id)) { $target_prop = false; } // Set up the values for the template $settings = static::settings($settings); $target_class = $settings['mapping']['targetEntity']; $target_table = \CMF\Admin::getTableForClass($target_class); $options = $target_class::options(\Arr::get($settings, 'filters', array()), array(), null, null, null, is_array($settings['select2']), \Arr::get($settings, 'group_by')); $settings['required'] = isset($settings['required']) ? $settings['required'] : false; $errors = $model->getErrorsForField($settings['mapping']['fieldName']); $has_errors = count($errors) > 0; $settings['title'] = $settings['title'] . ($settings['required'] ? ' *' : '') . ($has_errors ? ' - ' . $errors[0] : ''); $settings['cid'] = 'field_' . md5($settings['mapping']['fieldName'] . static::type()); $settings['add_link'] = \Uri::create('/admin/' . $target_table . '/create?_mode=inline&_cid=' . $settings['cid'] . ($target_prop !== false ? '&' . $target_prop . '=' . $model->id : '')); $settings['singular'] = $target_class::singular(); $settings['icon'] = $target_class::icon(); $settings['is_select2'] = false; // Permissions $settings['can_edit'] = \CMF\Auth::can('edit', $target_class); $settings['can_create'] = \CMF\Auth::can('create', $target_class) && $settings['can_edit']; $settings['create'] = $settings['create'] && $settings['can_create']; $settings['edit'] = $settings['edit'] && $settings['can_edit']; if ($settings['transfer'] === true) { $settings['input_attributes']['class'] .= ' input-xxlarge'; $transfer_options = array(); foreach ($options as $key => $value) { $transfer_options[] = array('value' => $key, 'content' => $value); } $content = strval(\View::forge('admin/fields/collection/multiselect.twig', array('settings' => $settings, 'options' => $options, 'values' => $values), false)); return array('content' => $content, 'widget' => $settings['widget'], 'assets' => array('js' => array('/admin/assets/js/bootstrap-transfer.js', '/admin/assets/js/fields/collection/transfer.js'), 'css' => array('/admin/assets/css/bootstrap-transfer.css')), 'js_data' => array('options' => $transfer_options, 'values' => $values, 'edit' => $settings['edit'], 'create' => $settings['create'])); } else { if (is_array($settings['select2'])) { $settings['sortable'] = $settings['select2']['sortable'] = $target_class::sortable() && isset($settings['mapping']['orderBy']) && isset($settings['mapping']['orderBy']['pos']) && $settings['mapping']['orderBy']['pos'] == 'ASC'; $settings['is_select2'] = true; $settings['input_attributes']['class'] .= 'input-xxlarge select2'; $content = strval(\View::forge('admin/fields/collection/multiselect.twig', array('settings' => $settings, 'options' => $options, 'values' => $values), false)); $settings['select2']['placeholder'] = 'click to select a ' . strtolower($settings['singular']) . '...'; $settings['select2']['target_table'] = $target_table; // Permissions $settings['select2']['create'] = $settings['create']; $settings['select2']['edit'] = $settings['edit']; return array('content' => $content, 'widget' => $settings['widget'], 'assets' => array('css' => array('/admin/assets/select2/select2.css'), 'js' => array('/admin/assets/select2/select2.min.js', '/admin/assets/js/fields/select2.js')), 'js_data' => $settings['select2']); } } $settings['input_attributes']['class'] .= ' input-xxlarge'; return array('content' => strval(\View::forge('admin/fields/collection/multiselect.twig', array('settings' => $settings, 'options' => $options, 'values' => $values), false)), 'assets' => array('js' => array('/admin/assets/js/fields/collection/multiselect.js')), 'widget' => false); return; }
/** inheritdoc */ public static function displayForm($value, &$settings, $model) { $settings = static::settings($settings); $include_label = isset($settings['label']) ? $settings['label'] : true; $required = isset($settings['required']) ? $settings['required'] : false; $errors = $model->getErrorsForField($settings['mapping']['fieldName']); $has_errors = count($errors) > 0; // Check stylesSet URL $stylesSet = \Arr::get($settings, 'stylesSet'); if (!empty($stylesSet) && substr($stylesSet, 0, 1) == '/') { // Add an absolute URL to the start $settings['stylesSet'] = rtrim(\Uri::base(false), '/') . $stylesSet; } // Add ckeditor to the class for the field $input_attributes = isset($settings['input_attributes']) ? $settings['input_attributes'] : array('class' => 'input-xxlarge'); $input_attributes['class'] = $input_attributes['class'] . " ckeditor-cmf"; $label = !$include_label ? '' : \Form::label($settings['title'] . ($required ? ' *' : '') . ($has_errors ? ' - ' . $errors[0] : ''), $settings['mapping']['fieldName'], array('class' => 'item-label')); $input = \Form::textarea($settings['mapping']['fieldName'], strval($value), $input_attributes); // Set up required information for any links specified if (isset($settings['links']) && is_array($settings['links'])) { $links = array(); foreach ($settings['links'] as $link_type => $link) { if (!class_exists($link_type)) { continue; } $link['table_name'] = \CMF\Admin::getTableForClass($link_type); $link['singular'] = $link_type::singular(); $link['plural'] = $link_type::plural(); $link['icon'] = $link_type::icon(); $links[$link_type] = $link; } $settings['links'] = $links; } if (isset($settings['stylesSet'])) { if (file_exists(DOCROOT . ltrim($settings['stylesSet'], '/'))) { $settings['stylesSet'] = 'default:' . \Uri::base(false) . ltrim($settings['stylesSet'], '/'); } else { unset($settings['stylesSet']); } } if (isset($settings['contentsCss'])) { if (strpos($settings['contentsCss'], '.php') === false && !file_exists(DOCROOT . ltrim($settings['contentsCss'], '/'))) { unset($settings['contentsCss']); } } // Return only the field and label if no wrap is required if (isset($settings['wrap']) && $settings['wrap'] === false) { return $label . $input; } // Return the widget if (isset($settings['widget']) && $settings['widget'] === true) { return array('assets' => array(), 'content' => $input, 'widget' => true, 'widget_title' => $settings['title'], 'widget_icon' => 'align-left', 'js_data' => $settings); } // Return the normal field return array('assets' => array(), 'content' => html_tag('div', array('class' => 'control-group ' . ($has_errors ? ' error' : '')), $label . $input), 'widget' => false, 'js_data' => $settings); }
/** inheritdoc */ public static function displayForm($value, &$settings, $model) { $id = isset($value) ? $value->id : ''; $settings = static::settings($settings); $settings['cid'] = 'field_' . md5($settings['mapping']['fieldName'] . static::type()); $required = isset($settings['required']) ? $settings['required'] : false; $include_label = isset($settings['label']) ? $settings['label'] : true; $target_class = $settings['mapping']['targetEntity']; $target_table = \CMF\Admin::getTableForClass($target_class); $target_prop = $settings['mapping']['isOwningSide'] === true ? $settings['mapping']['inversedBy'] : $settings['mapping']['mappedBy']; if (empty($target_prop) || is_null($model->id)) { $target_prop = false; } $add_link = \Uri::create('/admin/' . $target_table . '/create?_mode=inline&_cid=' . $settings['cid'] . ($target_prop !== false ? '&' . $target_prop . '=' . $model->id : '')); $options = $target_class::options(\Arr::get($settings, 'filters', array()), array(), null, null, null, is_array($settings['select2']), \Arr::get($settings, 'group_by')); $has_controls = $settings['create'] !== false; // Description? $description = isset($settings['description']) ? '<span class="help-block">' . $settings['description'] . '</span>' : ''; if ($settings['allow_empty']) { $options = array('' => '') + $options; } $errors = $model->getErrorsForField($settings['mapping']['fieldName']); $has_errors = count($errors) > 0; $input_attributes = $settings['input_attributes']; $label = !$include_label ? '' : \Form::label($settings['title'] . ($required ? ' *' : '') . ($has_errors ? ' - ' . $errors[0] : ''), $settings['mapping']['fieldName'], array('class' => 'item-label')); $add_link = html_tag('a', array('href' => $add_link, 'class' => 'btn btn-mini btn-create'), '<i class="fa fa-plus"></i> create ' . strtolower($target_class::singular())); // Permissions $settings['can_edit'] = \CMF\Auth::can('edit', $target_class); $settings['can_create'] = \CMF\Auth::can('create', $target_class) && $settings['can_edit']; $settings['create'] = $settings['create'] && $settings['can_create']; $settings['edit'] = $settings['edit'] && $settings['can_edit']; if ($settings['create'] === false) { $add_link = " "; } $controls_top = html_tag('div', array('class' => 'controls-top'), $add_link); if (is_array($settings['select2'])) { $input_attributes['class'] .= 'input-xxlarge select2'; $input = \Form::select($settings['mapping']['fieldName'], $id, $options, $input_attributes); $settings['select2']['placeholder'] = 'click to select ' . strtolower($target_class::singular()) . '...'; $settings['select2']['target_table'] = $target_table; // Permissions $settings['select2']['create'] = $settings['create']; $settings['select2']['edit'] = $settings['edit']; if (!$required) { $settings['select2']['allowClear'] = true; } return array('content' => html_tag('div', array('class' => 'controls control-group' . ($has_controls ? ' field-with-controls' : '') . ($has_errors ? ' error' : ''), 'id' => $settings['cid']), $label . $description . $input . $controls_top) . '<div class="clear"><!-- --></div>', 'widget' => false, 'assets' => array('css' => array('/admin/assets/select2/select2.css'), 'js' => array('/admin/assets/select2/select2.min.js', '/admin/assets/js/fields/select2.js')), 'js_data' => $settings['select2']); } $input_attributes['class'] .= ' input-xxlarge'; $input = \Form::select($settings['mapping']['fieldName'], $id, $options, $input_attributes); if (isset($settings['wrap']) && $settings['wrap'] === false) { return $label . $input; } return html_tag('div', array('class' => 'controls control-group' . ($has_controls ? ' field-with-controls' : '') . ($has_errors ? ' error' : ''), 'id' => $settings['cid']), $label . $description . $input . $controls_top) . '<div class="clear"><!-- --></div>'; }
/** inheritdoc */ public static function displayForm($value, &$settings, $model) { $settings = static::settings($settings); $include_label = isset($settings['label']) ? $settings['label'] : true; $required = isset($settings['required']) ? $settings['required'] : false; $errors = $model->getErrorsForField($settings['mapping']['fieldName']); $has_errors = count($errors) > 0; $attributes = array('class' => 'controls control-group ' . ($has_errors ? ' error' : '')); $input_attributes = isset($settings['input_attributes']) ? $settings['input_attributes'] : array('class' => 'input-xxlarge'); //add redactor to the class for the field $input_attributes['class'] = $input_attributes['class'] . " redactor"; $label_text = $settings['title'] . ($required ? ' *' : ''); $input = \Form::textarea($settings['mapping']['fieldName'], strval($value), $input_attributes); // Translation? if (\CMF::$lang_enabled && !\CMF::langIsDefault() && $model->isTranslatable($settings['mapping']['columnName'])) { // If there is no translation if (!$model->hasTranslation($settings['mapping']['columnName'])) { $attributes['class'] .= ' no-translation'; $label_text = '<span class="no-translation"><img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::defaultLang() . '.png') . '" /> ' . $label_text . '</span>'; } else { $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" /> ' . $label_text; } } // Build the label $label = !$include_label ? '' : \Form::label($label_text . ($has_errors ? ' - ' . $errors[0] : ''), $settings['mapping']['fieldName'], array('class' => 'item-label')); // Set up required information for any links specified if (isset($settings['links']) && is_array($settings['links'])) { $links = array(); foreach ($settings['links'] as $link_type => $link) { if (!class_exists($link_type)) { continue; } $link['table_name'] = \CMF\Admin::getTableForClass($link_type); $link['singular'] = $link_type::singular(); $link['plural'] = $link_type::plural(); $link['icon'] = $link_type::icon(); $links[$link_type] = $link; } $settings['links'] = $links; } // Return only the field and label if no wrap is required if (isset($settings['wrap']) && $settings['wrap'] === false) { return $label . $input; } // Return the widget if (isset($settings['widget']) && $settings['widget'] === true) { return array('assets' => array(), 'content' => $input, 'widget' => true, 'widget_title' => $label_text, 'widget_icon' => 'align-left', 'js_data' => $settings); } // Return the normal field return array('assets' => array(), 'content' => html_tag('div', $attributes, $label . $input), 'widget' => false, 'js_data' => $settings); }
/** inheritdoc */ public static function displayForm($value, &$settings, $model) { if (!\CMF\Auth::can(array('view', 'edit'), 'CMF\\Model\\Permission')) { return ''; } // Kick the permissions and get the active classes \CMF\Auth::create_permissions(); $all_actions = \CMF\Auth::all_actions(); $activeClasses = \CMF\Admin::activeClasses(); // Set up the values $values = array(); if (isset($value) && ($value instanceof \Doctrine\Common\Collections\Collection || is_array($value))) { foreach ($value as $val) { $resource = $val->resource; $action = $val->action; $actions = isset($values[$resource]) ? $values[$resource] : array(); if (!in_array($action, $actions)) { $actions[] = $action; } $values[$resource] = $actions; } } // Get the resources defined in the config $extra_resources = \CMF\Auth::extra_resources(); $resources = array(); $resource_group = array('title' => \Lang::get('admin.common.resources'), 'classes' => array()); $classes_index = 0; // Set the values of the resources foreach ($extra_resources as $resource_id => $extra_resource) { $extra_resource['values'] = isset($values[$resource_id]) ? $values[$resource_id] : array(); $resource_group['classes'][$resource_id] = $extra_resource; } // If there are resources, add them at the top and update the classes index if (count($resource_group['classes']) > 0) { $resources[] = $resource_group; $classes_index = 1; } $resources[] = array('title' => \Lang::get('admin.common.content_types'), 'classes' => array()); // Build the resources list... foreach ($activeClasses as $class_name => $classes) { if (count($classes) > 1) { $class_group = array(); foreach ($classes as $group_class) { $resource_title = $group_class::_static() ? $group_class::singular() : $group_class::plural(); $resource_icon = $group_class::icon(); $class_group[$group_class] = array('title' => $resource_title, 'icon' => $resource_icon, 'actions' => $group_class::_static() ? array('view', 'edit') : $all_actions, 'values' => isset($values[$group_class]) ? $values[$group_class] : array()); } uasort($class_group, function ($a, $b) { return strcmp(strtolower($a['title']), strtolower($b['title'])); }); $resources[] = array('title' => $class_name::plural(), 'classes' => $class_group); } else { $resource_title = $class_name::_static() ? $class_name::singular() : $class_name::plural(); $resource_icon = $class_name::icon(); $resources[$classes_index]['classes'][$class_name] = array('title' => $resource_title, 'icon' => $resource_icon, 'actions' => $class_name::_static() ? array('view', 'edit') : $all_actions, 'values' => isset($values[$class_name]) ? $values[$class_name] : array()); } } uasort($resources[$classes_index]['classes'], function ($a, $b) { return strcmp(strtolower($a['title']), strtolower($b['title'])); }); $content = strval(\View::forge('admin/fields/auth/permissions.twig', array('settings' => $settings, 'resources' => $resources, 'actions' => $all_actions), false)); return array('content' => $content, 'widget' => true, 'widget_title' => $settings['title'], 'assets' => array()); }
/** * Get the auto translatable fields which have changed for an entity */ protected function getChangeset($entity) { $entity_class = $entity->metadata()->name; $translatableFields = \CMF\Admin::getTranslatable($entity_class); $excludedFields = $entity_class::excludeAutoTranslations(); if (\Input::param('force_translate', false) !== false) { return array_values(array_diff(array_values($translatableFields), $excludedFields)); } else { $changeset = \D::manager()->getUnitOfWork()->getEntityChangeSet($entity); if (is_array($changeset)) { $changeset = array_keys($changeset); } else { $changeset = array(); } } return array_diff(array_values(array_intersect($translatableFields, $changeset)), $excludedFields); }
/** * 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; }
/** * Ensures that there is at least an 'all' permission set for every resource * @return void */ public static function create_permissions() { $actions = static::all_actions(); $actions[] = 'all'; $activeClasses = \CMF\Admin::activeClasses(); $activeClasses['user_defined'] = array_keys(\Config::get('cmf.auth.resources', array())); $roles = Role::select('item')->getQuery()->getResult(); $em = \D::manager(); foreach ($activeClasses as $parent_class => $classes) { foreach ($classes as $class_name) { $count = intval(Permission::select("count(item)")->where("item.resource = '{$class_name}'")->andWhere("item.action = 'all'")->getQuery()->getSingleScalarResult()); if ($count == 0) { $permission = new Permission(); $permission->set('action', 'all'); $permission->set('resource', $class_name); $em->persist($permission); foreach ($roles as $role) { $role->add('permissions', $permission); $em->persist($role); } } } } $em->flush(); }