Esempio n. 1
0
 /**
  * Renders the field's form element for editing in the admin site
  * @see \Admin::getFieldSettings()
  * @param mixed $value The current value of the property, if there is one
  * @param array $settings Field settings, created through \Admin::getFieldSettings()
  * @param object $model The model, if it is being edited.
  * @return string The form control
  */
 public static function displayForm($value, &$settings, $model)
 {
     $class = get_called_class();
     $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;
     $input_attributes = isset($settings['input_attributes']) ? $settings['input_attributes'] : array('class' => 'input-xxlarge');
     if (!isset($input_attributes['id'])) {
         $input_attributes['id'] = 'form_' . $settings['mapping']['fieldName'];
     }
     $attributes = array('class' => 'controls control-group' . ($has_errors ? ' error' : '') . ' field-type-' . $class::type($settings));
     $label_text = $settings['title'] . ($required ? ' *' : '');
     // Translation?
     if (\CMF::$lang_enabled && !\CMF::langIsDefault() && isset($settings['mapping']['columnName']) && $model->isTranslatable($settings['mapping']['columnName'])) {
         // If there is no translation
         if (!$model->hasTranslation($settings['mapping']['columnName'])) {
             $attributes['class'] .= ' no-translation';
             $input_attributes['class'] .= ' no-translation';
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::defaultLang() . '.png') . '" />&nbsp; ' . $label_text;
         } else {
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" />&nbsp; ' . $label_text;
         }
     }
     // Description?
     $description = isset($settings['description']) ? '<span class="help-block">' . $settings['description'] . '</span>' : '';
     // Build the input
     $input = '<input type="text" name="' . $settings['mapping']['fieldName'] . '" ' . array_to_attr($input_attributes) . ' value="' . \Security::htmlentities(strval($value), ENT_QUOTES) . '" />';
     // Build the label
     $label = !$include_label ? '' : html_tag('label', array('class' => 'item-label', 'for' => $settings['mapping']['fieldName']), $label_text . ($has_errors ? ' - ' . $errors[0] : ''));
     // Prepend or append things...
     if (isset($settings['prepend'])) {
         $input = html_tag('div', array('class' => 'input-prepend'), html_tag('span', array('class' => 'add-on'), $settings['prepend']) . $input);
     }
     if (isset($settings['append'])) {
         $input = html_tag('div', array('class' => 'input-append'), $input . html_tag('span', array('class' => 'add-on'), $settings['append']));
     }
     // Don't wrap the input if wrap is set to false
     if (isset($settings['wrap']) && $settings['wrap'] === false) {
         return $label . $input;
     }
     // Add the 'keep updated' control if the field has a template
     if (isset($settings['template']) && !empty($settings['template'])) {
         $attributes['class'] .= ' field-with-controls field-with-template';
         $auto_update_setting = 'settings[' . $settings['mapping']['fieldName'] . '][auto_update]';
         $auto_update_content = \Form::hidden($auto_update_setting, '0', array()) . html_tag('label', array('class' => 'checkbox auto-update-label'), \Form::checkbox($auto_update_setting, '1', \Arr::get($settings, 'auto_update', true), array('class' => 'auto-update')) . strtolower(\Lang::get('admin.common.auto_update')));
         $auto_update = html_tag('div', array('class' => 'controls-top'), $auto_update_content);
         $label .= $auto_update;
         return array('content' => html_tag('div', $attributes, $label . $description . $input) . '<div class="clear"><!-- --></div>', 'widget' => false, 'assets' => array('js' => array('/admin/assets/js/twig.min.js', '/admin/assets/js/fields/template.js')), 'js_data' => $settings);
     }
     return html_tag('div', $attributes, $label . $description . $input);
 }
Esempio n. 2
0
 /** 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') . '" />&nbsp; ' . $label_text . '</span>';
         } else {
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" />&nbsp; ' . $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);
 }
Esempio n. 3
0
 /** inheritdoc */
 public static function displayForm($value, &$settings, $model)
 {
     $required = isset($settings['required']) ? $settings['required'] : false;
     $errors = $model->getErrorsForField($settings['mapping']['fieldName']);
     $has_errors = count($errors) > 0;
     $input_attributes = array('class' => 'input input-xxlarge');
     $attributes = array('class' => 'field-type-link controls control-group' . ($has_errors ? ' error' : ''));
     $href_name = $settings['mapping']['fieldName'] . '[href]';
     $value['href'] = isset($value['href']) ? $value['href'] : null;
     $label_text = $settings['title'] . ($required ? ' *' : '');
     // Translation?
     if (\CMF::$lang_enabled && !\CMF::langIsDefault() && isset($settings['mapping']['columnName']) && $model->isTranslatable($settings['mapping']['columnName'])) {
         // If there is no translation
         if (!$model->hasTranslation($settings['mapping']['columnName'])) {
             $attributes['class'] .= ' no-translation';
             $input_attributes['class'] .= ' no-translation';
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::defaultLang() . '.png') . '" />&nbsp; ' . $label_text;
         } else {
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" />&nbsp; ' . $label_text;
         }
     }
     // EXTERNAL CHECKBOX
     $external_name = $settings['mapping']['fieldName'] . '[external]';
     $external_value = \Arr::get($value, 'external', false);
     $external = \Form::hidden($external_name, '0') . html_tag('label', array('class' => 'checkbox external-checkbox'), \Form::checkbox($external_name, '1', $external_value, array()) . ' custom');
     $label = \Form::label($label_text . ($has_errors ? ' - ' . $errors[0] : ''), $href_name, array('class' => 'item-label')) . $external . html_tag('div', array('class' => 'clear'), '&nbsp;');
     if ($external_value) {
         $attributes['class'] .= ' external';
     }
     // EXTERNAL INPUT CONTENT
     $href_value_ext = $external_value ? $value['href'] : '';
     $ext_input = \Form::input($href_name, $href_value_ext, $input_attributes);
     $ext_content = html_tag('div', array('class' => 'external-link'), $ext_input);
     // INTERNAL DROPDOWN CONTENT
     $options = static::getOptions($settings, $model);
     $href_value_int = $external_value ? '' : $value['href'];
     // Check if the value is actually an alias
     if (!empty($href_value_int)) {
         $url_value = \CMF\Model\URL::find($href_value_int);
         if ($url_value && ($alias = $url_value->alias)) {
             $href_value_int = $alias->id;
         }
     }
     $input = \Form::select($href_name, $href_value_int, $options, $input_attributes);
     $int_content = html_tag('div', array('class' => 'internal-link'), $input);
     return html_tag('div', $attributes, $label . $int_content . $ext_content) . html_tag('div', array(), '');
 }
Esempio n. 4
0
 /** inheritdoc */
 public static function displayForm($value, &$settings, $model)
 {
     $include_label = isset($settings['label']) ? $settings['label'] : true;
     $target_class = $settings['mapping']['targetEntity'];
     if (is_null($value) || !$value instanceof $target_class) {
         $value = new $target_class();
     }
     // Show a simple alias form if the input var is set
     if (\Input::param('alias', false) !== false) {
         $linkValue = array();
         if (!empty($value)) {
             $linkValue['href'] = $value->isExternal() ? $value->url : strval($value->id);
             $linkValue['external'] = $value->isExternal();
         }
         return \CMF\Field\Object\Link::displayForm($linkValue, $settings, $model);
     }
     $model_class = get_class($model);
     $errors = $model->getErrorsForField($settings['mapping']['fieldName']);
     $has_errors = count($errors) > 0;
     $attributes = array('class' => 'field-type-url controls control-group' . ($has_errors ? ' error' : ''));
     $slug_name = $settings['mapping']['fieldName'] . '[slug]';
     $label_text = $settings['title'] . ($has_errors ? ' - ' . $errors[0] : '');
     if (\CMF::$lang_enabled && !\CMF::langIsDefault()) {
         if (!$value->hasTranslation('slug')) {
             $attributes['class'] .= ' no-translation';
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::defaultLang() . '.png') . '" />&nbsp; ' . $label_text;
         } else {
             $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" />&nbsp; ' . $label_text;
         }
     }
     $keep_updated_setting = 'settings[' . $settings['mapping']['fieldName'] . '][keep_updated]';
     $keep_updated = \Form::hidden($keep_updated_setting, '0', array()) . html_tag('label', array('class' => 'checkbox keep-updated'), \Form::checkbox($keep_updated_setting, '1', \Arr::get($settings, 'keep_updated', true), array()) . strtolower(\Lang::get('admin.common.auto_update')));
     $input = \Form::input($slug_name, $value->slug, array('class' => 'input-xlarge', 'data-copy-from' => implode(',', $model_class::slugFields())));
     $label = !$include_label ? '' : html_tag('label', array('class' => 'item-label', 'for' => $slug_name), $label_text) . $keep_updated . html_tag('div', array('class' => 'clear'), '&nbsp;');
     $prefix = $value->prefix;
     $prepend = html_tag('span', array('class' => 'add-on'), empty($prefix) ? '/' : $prefix);
     $input = html_tag('div', array('class' => 'input-prepend'), $prepend . $input);
     $clear = '<div class="clear"><!-- --></div>';
     if (isset($settings['wrap']) && $settings['wrap'] === false) {
         return $label . $input;
     }
     return html_tag('div', $attributes, $label . $input) . $clear;
 }
Esempio n. 5
0
 public function showTranslationStatus($model)
 {
     return \CMF::$lang_enabled && !\CMF::langIsDefault() && isset($this->settings['mapping']['columnName']) && $model->isTranslatable($this->settings['mapping']['columnName']);
 }
Esempio n. 6
0
 /**
  * Renders the item list (the table view)
  * @param string $table_name
  * @return void
  */
 public function action_index($table_name, $tab_id = null)
 {
     $class_name = \Admin::getClassForTable($table_name);
     if ($class_name === false) {
         return $this->customPageOr404(array($table_name, $tab_id), "type");
     }
     // Redirect straight to the edit page if the item is static
     if ($class_name::_static() === true) {
         $static_item = $class_name::select('item')->setMaxResults(1)->getQuery()->getResult();
         if (count($static_item) > 0) {
             $static_item = $static_item[0];
             \Response::redirect("/admin/{$table_name}/" . $static_item->id . "/edit", 'location');
         } else {
             \Response::redirect("/admin/{$table_name}/create", 'location');
         }
     }
     if (!\CMF\Auth::can('view', $class_name)) {
         return $this->show403('action_plural', array('action' => \Lang::get('admin.verbs.view'), 'resource' => strtolower($class_name::plural())));
     }
     // Here's where we catch special types, eg tree nodes. These can now be rendered using a special template
     if (is_subclass_of($class_name, 'CMF\\Model\\Node')) {
         return $this->treeView($class_name);
     }
     // Get permissions
     $can_create = \CMF\Auth::can('create', $class_name);
     $can_edit = \CMF\Auth::can('edit', $class_name);
     $can_delete = \CMF\Auth::can('delete', $class_name);
     $can_manage = \CMF\Auth::can(array('view', 'edit'), 'CMF\\Model\\Permission');
     // Get the data for the list
     $metadata = $class_name::metadata();
     $query_fields = $class_name::query_fields();
     $pagination = $class_name::pagination();
     $sortable = $class_name::sortable();
     $per_page = $class_name::per_page();
     $sort_group = is_callable($class_name . '::sortGroup') ? $class_name::sortGroup() : null;
     $sort_process = $class_name::sortProcess();
     $excluded_ids = array();
     $fields = \Admin::getFieldSettings($class_name);
     $list_tabs = $class_name::listTabs();
     $list_fields = $class_name::listFields();
     $list_filters = array();
     if (empty($list_fields)) {
         $list_fields = array_keys($fields);
     }
     $columns = array();
     $joins = array();
     $methods = array();
     // See if we have any imported models
     // $importedIds = $class_name::getImportedIds();
     // if (count($importedIds)) {
     // 	if (!count($list_tabs)) {
     // 		$list_tabs['main'] = array(
     // 			'title' => 'Main',
     // 			'filters' => array('id NOT IN ('.implode(',',$importedIds).')')
     // 		);
     // 	}
     // 	$list_tabs['imported'] = array(
     // 		'title' => 'Imported ('.count($importedIds).')',
     // 		'filters' => array('id IN ('.implode(',',$importedIds).')')
     // 	);
     // }
     // Find out the tab we're on
     if (count($list_tabs) > 0) {
         $this->default_tab = key($list_tabs);
         if ($tab_id === null || !isset($list_tabs[$tab_id])) {
             $tab_id = $this->default_tab;
         }
         $tab = \Arr::get($list_tabs, $tab_id, array());
         $list_fields = \Arr::get($tab, 'fields', $list_fields);
         $list_filters = \Arr::get($tab, 'filters', $list_filters);
         $this->current_tab = $tab_id;
     }
     // Create static items
     \Admin::createStaticInstances($metadata);
     // See if the list order has been set in the session. If not, try and use the model's default order
     $order = \Session::get($metadata->table['name'] . ".list.order", null);
     if (is_null($order)) {
         $order = $class_name::order();
     } else {
         $order = $order + $class_name::order();
     }
     // Start the query builder...
     $qb = $class_name::select('item', 'item', 'item.id');
     // Retrieve any custom joins from config on the model
     $manual_joins = $class_name::joins();
     foreach ($manual_joins as $join_alias => $manual_join) {
         $qb->leftJoin($manual_join, $join_alias)->addSelect($join_alias);
     }
     // Add any joins to the query builder (prevents the query buildup that comes from accessing lazily loaded associations)
     foreach ($list_fields as $num => $field) {
         if ($field == 'id') {
             continue;
         }
         // If there is a dot notation try and locate the field
         if (strpos($field, ".") !== false) {
             $parts = explode(".", $field);
             $field_name = array_shift($parts);
             // If this dot notation refers to an association, we need to find the field data for the target type!
             if ($metadata->isSingleValuedAssociation($field_name)) {
                 $target_class = $metadata->getAssociationTargetClass($field_name);
                 $target_fields = \Admin::getFieldSettings($target_class);
                 foreach ($target_fields as $target_field => $target_field_settings) {
                     $target_field_settings['title'] = $target_class::singular() . ' ' . $target_field_settings['title'];
                     $fields[$field_name . '.' . $target_field] = $target_field_settings;
                 }
             }
         } else {
             $field_name = $field;
         }
         // This could be a method on the model
         if (!isset($fields[$field])) {
             if (array_key_exists($field_name, $manual_joins)) {
                 $join_heading = \Inflector::humanize(\Inflector::underscore($field_name));
                 $parts = explode(".", $field);
                 if (count($parts) == 2) {
                     $field_colons = implode(':', $parts);
                     if (isset($order[$field_colons])) {
                         $dir = strtolower($order[$field_colons]);
                         $rev = $dir == 'asc' ? 'desc' : 'asc';
                         $arrows = html_tag('span', array('class' => 'arrow-down'), '&#x25BC;') . html_tag('span', array('class' => 'arrow-up'), '&#x25B2;');
                         $verbose = $dir == 'asc' ? 'descending' : 'ascending';
                         $join_heading = html_tag('a', array('href' => \Uri::create("/admin/{$table_name}/list/order?{$field_colons}={$rev}"), 'class' => 'sort-link ' . $dir, 'title' => 'Sort by ' . $join_heading . ' ' . $verbose), $join_heading . ' ' . $arrows);
                     } else {
                         $join_heading = html_tag('a', array('href' => \Uri::create("/admin/{$table_name}/list/order?{$field_colons}=asc"), 'class' => 'sort-link', 'title' => 'Sort by ' . $join_heading . ' ascending'), $join_heading);
                     }
                 }
                 $columns[] = array('num' => $num, 'type' => 'join', 'join' => $parts[0], 'name' => count($parts) > 1 ? $parts[1] : 'display', 'heading' => $join_heading);
             } else {
                 if (method_exists($class_name, $field_name)) {
                     $column = array('num' => $num, 'type' => 'method', 'name' => $field_name, 'heading' => \Inflector::humanize(\Inflector::underscore($field_name)));
                     /*
                     					if (isset($order[$field_name])) {
                         $dir = strtolower($order[$field_name]);
                         $rev = ($dir == 'asc') ? 'desc' : 'asc';
                         $arrows = html_tag('span', array( 'class' => 'arrow-down' ), '&#x25BC;').html_tag('span', array( 'class' => 'arrow-up' ), '&#x25B2;');
                         $verbose = ($dir == 'asc') ? 'descending' : 'ascending';
                         $column['heading'] = html_tag('a', array( 'href' => \Uri::create("/admin/$table_name/list/order?$field_name=$rev"), 'class' => 'sort-link '.$dir, 'title' => 'Sort by '.$column['heading'].' '.$verbose ), $column['heading'].' '.$arrows);
                     } else {
                         $column['heading'] = html_tag('a', array( 'href' => \Uri::create("/admin/$table_name/list/order?$field_name=asc"), 'class' => 'sort-link', 'title' => 'Sort by '.$column['heading'].' ascending' ), $column['heading']);
                     }
                     */
                     $methods[] = $field_name;
                     $columns[] = $column;
                 }
             }
             continue;
         }
         if ($metadata->isSingleValuedAssociation($field_name) && !in_array($field_name, $joins) && !array_key_exists($field_name, $manual_joins)) {
             $qb->leftJoin('item.' . $field_name, $field_name)->addSelect($field_name);
             $joins[] = $field_name;
         } else {
             if ($metadata->isCollectionValuedAssociation($field_name) && !in_array($field_name, $joins) && !array_key_exists($field_name, $manual_joins)) {
                 $qb->leftJoin('item.' . $field_name, $field_name)->addSelect($field_name);
                 $joins[] = $field_name;
             }
         }
         // Get the field class and type
         $field_class = $fields[$field]['field'];
         $field_type = $field_class::type();
         $column = array('name' => $field, 'type' => $field_type);
         if (!$sortable) {
             $field_colons = str_replace('.', ':', $field);
             if (isset($order[$field_colons])) {
                 $dir = strtolower($order[$field_colons]);
                 $rev = $dir == 'asc' ? 'desc' : 'asc';
                 $arrows = html_tag('span', array('class' => 'arrow-down'), '&#x25BC;') . html_tag('span', array('class' => 'arrow-up'), '&#x25B2;');
                 $verbose = $dir == 'asc' ? 'descending' : 'ascending';
                 $column['heading'] = html_tag('a', array('href' => \Uri::create("/admin/{$table_name}/list/order?{$field_colons}={$rev}"), 'class' => 'sort-link ' . $dir, 'title' => 'Sort by ' . $fields[$field]['title'] . ' ' . $verbose), $fields[$field]['title'] . ' ' . $arrows);
             } else {
                 $column['heading'] = html_tag('a', array('href' => \Uri::create("/admin/{$table_name}/list/order?{$field_colons}=asc"), 'class' => 'sort-link', 'title' => 'Sort by ' . $fields[$field]['title'] . ' ascending'), $fields[$field]['title']);
             }
         } else {
             $column['heading'] = $fields[$field]['title'];
         }
         $columns[] = $column;
     }
     // Add dropdown list filters
     $filter_by = $class_name::list_filters();
     if (is_array($filter_by) && count($filter_by) > 0) {
         $filters = array();
         foreach ($filter_by as $filter_field) {
             if ($metadata->hasAssociation($filter_field)) {
                 $filter_class = $metadata->getAssociationTargetClass($filter_field);
                 $filter_name = \Arr::get($fields, "{$filter_field}.title", '');
                 $filter_options = array('' => 'All ' . $filter_class::plural()) + $filter_class::options();
                 $filter_val = \Input::get($filter_field);
                 $filter_is_tree = is_subclass_of($filter_class, 'CMF\\Model\\Node');
                 if (!empty($filter_val) && $filter_is_tree && ($filterEntity = $filter_class::find($filter_val))) {
                     $filter_val = $filterEntity->getChildrenIds(false, null, 'ASC', true);
                 }
                 if (empty($filter_val) && $filter_field == $sort_group) {
                     $sortable = false;
                 }
                 $filters[$filter_field] = array('label' => 'Show ' . strtolower($filter_name) . ':', 'options' => $filter_options);
                 if (!in_array($filter_field, $joins) && !array_key_exists($filter_field, $manual_joins)) {
                     $qb->leftJoin('item.' . $filter_field, $filter_field)->addSelect($filter_field);
                     $joins[] = $filter_field;
                 }
                 if (!empty($filter_val)) {
                     $paramName = $filter_name . 'filter';
                     if (is_array($filter_val)) {
                         if (count($filter_val) > 1 && $filter_field == $sort_group) {
                             $sortable = false;
                         }
                         $qb->andWhere("{$filter_field} IN(:{$paramName})");
                     } else {
                         $qb->andWhere("{$filter_field} = :{$paramName}");
                     }
                     $qb->setParameter($paramName, $filter_val);
                 }
             }
         }
         $this->filters = $filters;
     }
     // Add list filters
     foreach ($list_filters as $num => $filter) {
         $filter_str = is_array($filter) ? 'item.' . implode(' OR item.', $filter) : 'item.' . $filter;
         $qb->andWhere($filter_str);
     }
     if (!empty($query_fields) && count($query_fields) > 0) {
         //if query string then search the fields provided
         $this->searchable = true;
         $query = \Input::get('query');
         $this->query = $query;
         if ($query) {
             $query_parts = array_map('trim', explode(' ', $query));
             $query_str = array();
             $query_val = array();
             $x = 0;
             foreach ($query_fields as $field) {
                 foreach ($query_parts as $query_part) {
                     $query_str[$x] = "item." . $field . " LIKE ?" . $x;
                     $query_val[$x] = "%{$query_part}%";
                     $x++;
                 }
             }
             $qb->andWhere(implode(' OR ', $query_str));
             foreach ($query_val as $key => $val) {
                 $qb->setParameter($key, $val);
             }
         }
     }
     if (\CMF::$lang_enabled && !\CMF::langIsDefault() && $class_name::langEnabled()) {
         array_unshift($columns, array('name' => '', 'type' => 'lang', 'heading' => ''));
     }
     // Make the list drag and drop, if editing is possible
     if ($sortable && $can_edit) {
         $pagination = false;
         array_unshift($columns, array('name' => '', 'type' => 'handle', 'heading' => ''));
     }
     // Add the sortable ordering
     if ($sortable) {
         $has_group = !is_null($sort_group) && property_exists($class_name, $sort_group);
         if ($has_group) {
             if (!in_array($sort_group, $joins)) {
                 $qb->leftJoin('item.' . $sort_group, $sort_group)->addSelect($sort_group);
                 $joins[] = $sort_group;
             }
             if ($metadata->hasAssociation($sort_group)) {
                 $assoc_class = $metadata->getAssociationTargetClass($sort_group);
                 $assoc_field = property_exists($assoc_class, 'name') ? 'name' : (property_exists($assoc_class, 'title') ? 'title' : 'id');
                 $qb->addOrderBy("{$sort_group}.{$assoc_field}", 'ASC');
             } else {
                 $qb->addOrderBy("item.{$sort_group}", 'ASC');
             }
         }
         $qb->addOrderBy('item.pos', 'ASC');
     } else {
         // Add the ordering to the query builder
         foreach ($order as $field => $direction) {
             if (in_array($field, $methods)) {
                 continue;
             }
             $field_name = $field;
             $assoc_field = 'title';
             // If there is a dot notation (or colon) try and locate the field
             if (strpos($field, ".") !== false) {
                 $parts = explode(".", $field);
                 $field_name = array_shift($parts);
                 $assoc_field = array_shift($parts);
             } else {
                 if (strpos($field, ":") !== false) {
                     $parts = explode(":", $field);
                     $field_name = array_shift($parts);
                     $assoc_field = array_shift($parts);
                 }
             }
             if (array_key_exists($field_name, $manual_joins)) {
                 $qb->addOrderBy("{$field_name}.{$assoc_field}", $direction);
             } else {
                 if ($metadata->hasAssociation($field_name)) {
                     $assoc_class = $metadata->getAssociationTargetClass($field_name);
                     if (!property_exists($assoc_class, $assoc_field)) {
                         $assoc_field = property_exists($assoc_class, 'title') ? 'title' : 'id';
                     }
                     $qb->addOrderBy("{$field_name}.{$assoc_field}", $direction);
                 } else {
                     $qb->addOrderBy("item.{$field_name}", $direction);
                 }
             }
         }
     }
     if ($pagination) {
         /* PAGINATION */
         $countquery = clone $qb;
         $countPaginator = new Paginator($countquery, true);
         $count = $countPaginator->count();
         $config = array('pagination_url' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'total_items' => $count, 'per_page' => $per_page, 'uri_segment' => 'p', 'wrapper' => '<div class="pagination"><ul>{pagination}</ul></div>', 'first' => '<li class="first">{link}</li>', 'previous' => '<li class="prev">{link}</i>', 'previous-inactive' => '', 'regular' => '<li>{link}</li>', 'active' => '<li class="active">{link}</li>', 'next' => '<li class="next">{link}</li>', 'next-inactive' => '', 'previous-marker' => '&lt; Prev', 'next-marker' => 'Next &gt;');
         $pagination = \Pagination::forge('default', $config);
         $qb->setMaxResults($pagination->per_page);
         $qb->setFirstResult($pagination->offset);
         $this->pagination = $pagination->render();
         $this->per_page = $per_page;
         $this->page_count = ceil($count / $per_page);
         $this->total_items = $count;
         $this->current_page = \Input::param('p', 1);
         $rows = new Paginator($qb, true);
         $ids = array();
         foreach ($rows as $prow) {
             $ids[] = $prow->id;
         }
         reset($ids);
     } else {
         $this->pagination = null;
         $rows = $qb->getQuery()->getResult();
         $ids = array_keys($rows);
     }
     // Another pass at the ordering for methods
     /*
     		foreach ($order as $field => $direction) {
     			
     	if (!in_array($field, $methods)) continue;
     	
     	if ($direction == 'asc') {
     		uasort($rows, function($a, $b) use($field) {
     		    return strcmp(strtolower($a->$field()), strtolower($b->$field()));
     		});
     	} else {
     		uasort($rows, function($a, $b) use($field) {
     		    return strcmp(strtolower($b->$field()), strtolower($a->$field()));
     		});
     	}
     	
     }
     */
     // Item-specific permissions
     $user = \CMF\Auth::current_user();
     $item_permissions = array();
     if (!$user->super_user) {
         $user_roles = $user->roles->toArray();
         $permissions = \CMF\Model\Permission::select('item.id, item.action, item.resource, item.item_id')->leftJoin('item.roles', 'roles')->where("item.resource = '{$class_name}'");
         if (count($ids) > 0) {
             $permissions->andWhere("item.item_id IN(?1)")->setParameter(1, $ids);
         }
         if (count($user_roles) > 0) {
             $permissions->andWhere("roles IN (?2)")->setParameter(2, $user->roles->toArray());
         }
         $permissions = $permissions->getQuery()->getArrayResult();
         foreach ($permissions as $permission) {
             $item_actions = isset($item_permissions[$permission['item_id']]) ? $item_permissions[$permission['item_id']] : array();
             $item_actions[] = $permission['action'];
             $item_permissions[$permission['item_id']] = $item_actions;
         }
         foreach ($item_permissions as $item_id => $item_actions) {
             if (in_array('none', $item_actions) || count($item_actions) > 0 && !in_array('view', $item_actions)) {
                 $excluded_ids[] = $item_id;
             }
         }
     }
     // Import actions
     $importMethods = $class_name::importMethods();
     // Actions
     $this->actions = $class_name::actions();
     \Admin::setCurrentClass($class_name);
     $this->class_lang_enabled = $class_name::langEnabled();
     $this->plural = $class_name::plural();
     $this->excluded_ids = $excluded_ids;
     $this->item_permissions = $item_permissions;
     $this->singular = $class_name::singular();
     $this->icon = $class_name::icon();
     $this->rows = $rows;
     $this->columns = $columns;
     $this->fields = $fields;
     $this->table_name = $metadata->table['name'];
     $this->template = 'admin/item/list.twig';
     $this->superlock = $class_name::superlock();
     $this->sortable = $sortable && $can_edit;
     $this->sort_group = $sort_group;
     $this->tabs = $list_tabs;
     $this->sort_process = $sort_process;
     // Find all possible types that can be added
     $classes = array();
     $classes[$class_name] = array('plural' => $this->plural, 'singular' => $this->singular, 'icon' => $this->icon, 'table_name' => $metadata->table['name'], 'can_create' => $can_create && $can_edit, 'can_edit' => $can_edit, 'can_delete' => $can_delete, 'superclass' => $class_name::superclass());
     foreach ($metadata->subClasses as $sub_class) {
         $subclass_metadata = $sub_class::metadata();
         if (($subclass_cancreate = \CMF\Auth::can('create', $sub_class)) && !$sub_class::_static()) {
             $classes[$sub_class] = array('superlock' => $sub_class::superlock(), 'plural' => $sub_class::plural(), 'singular' => $sub_class::singular(), 'icon' => $sub_class::icon(), 'table_name' => $subclass_metadata->table['name'], 'can_edit' => \CMF\Auth::can('edit', $sub_class), 'can_delete' => \CMF\Auth::can('delete', $sub_class), 'superclass' => false);
         }
     }
     $this->classes = $classes;
     // Permissions
     $this->can_create = $can_create && $can_edit;
     $this->can_edit = $can_edit;
     $this->can_delete = $can_delete;
     $this->can_manage = $can_manage;
     $this->can_import = !empty($importMethods) && $can_manage;
     // Add the stuff for JS
     $this->js['table_name'] = $metadata->table['name'];
     $this->js['plural'] = $this->plural;
     $this->js['singular'] = $this->singular;
 }