/** * @param string $class * @param array $fields_to_retrieve * @param array $options */ public function get_all($class, array $fields_to_retrieve, $options = []) { $this->fields = $fields_to_retrieve; $this->options = $options; $this->class = $class; /** @var table $obj */ $obj = new $class(); $links = []; $mlinks = []; $obj->set_default_retrieve($fields_to_retrieve, $options); table::organise_links($obj, $fields_to_retrieve, $links, $mlinks); $dependencies = [get::__class_name($class)]; foreach ($links as $module => $link_info) { $field = $link_info['field']; $retrieves = $link_info['retrieve']; $dependencies[] = get::__class_name($module); $options['join'][$module] = $module . '.' . $field->field_name . '=' . $obj->class_name() . '.' . $field->field_name; foreach ($retrieves as $retrieve) { $fields_to_retrieve[] = $module . '.' . $retrieve; } } $key = 'get_all_' . $class . '_fetch_' . implode(',', $fields_to_retrieve) . '_options_' . serialize($options); $elements = \classes\cache::grab($key, function () use($class, $fields_to_retrieve, $options, $links, $mlinks, $obj) { if (!isset($options['order'])) { $options['order'] = get::__class_name($class) . '.position'; } $select = _db::get_query($class, $fields_to_retrieve, $options); $res = $select->execute(); if (_db::num($res)) { $row = _db::fetch($res, null); $mappings = $obj->get_field_mappings(array_keys($row)); do { /** @var table $class */ $object = new $class(); $object->set_from_row($row, $links, $mappings); foreach ($mlinks as $module => $blah) { $object->{$module . '_elements'} = new \classes\table_array(); $object->{$module} = new _collection(); } $this[] = $object; } while ($row = _db::fetch($res, null)); } $this->reset_iterator(); foreach ($mlinks as $module => $link_info) { /** @var \form\field_link $field */ $field = $link_info['field']; $retrieves = $link_info['retrieve']; $retrieves[] = 'l.' . $obj->get_primary_key_name() . ' AS linked_id'; $sub_class = $field->get_link_object(); $classes = $sub_class::get_all($retrieves, ['join' => [get::__class_name($class) . '_link_' . get::__class_name($sub_class) . ' l' => 'l.link_' . $sub_class->get_primary_key_name() . '=' . get::__class_name($sub_class) . '.' . $sub_class->get_primary_key_name()], 'where' => 'l.' . $obj->get_primary_key_name() . ' IN(' . implode(',', $this->get_table_keys()) . ')']); /** @var table $sub_object */ foreach ($classes as $sub_object) { $object = $this->find_table_key($sub_object->linked_id); if ($object) { $object->{$module . '_elements'}->push($sub_object); $object->{$module}->push($sub_object->get_primary_key()); } } } return $this->exchangeArray([]); }, $dependencies); $this->exchangeArray($elements); }
public function filter_unique(filter $field) { $values = []; $objects = []; $inner_field = $field->inner_field(); if ($inner_field instanceof field_mlink) { $this->iterate(function ($object) use(&$values, &$objects, $field) { foreach ($object->{$field->field_name} as $key => $link) { if (!isset($values[$link])) { $values[$link]['count'] = 1; $values[$link]['title'] = $object->{$field->field_name . '_elements'}[$key]->get_title(); $values[$link]['value'] = $link; } else { $values[$link]['count']++; } } }); } else { if ($inner_field instanceof field_link) { $field_name = get::__class_name($inner_field->get_link_module()); $this->iterate(function ($object) use(&$values, &$objects, $field, $field_name) { $key = $object->{$field_name}->get_primary_key(); if (!isset($values[$key])) { $values[$key]['count'] = 1; $values[$key]['title'] = $object->{$field_name}->get_title(); $values[$key]['value'] = $key; } else { $values[$key]['count']++; } }); } else { $this->iterate(function ($object) use(&$values, $field) { if (!isset($values[$object->{$field->field_name}])) { $values[$object->{$field->field_name}]['count'] = 1; $values[$object->{$field->field_name}]['title'] = $object->get_title(); $values[$object->{$field->field_name}]['value'] = $object->{$field->field_name}; } else { $values[$object->{$field->field_name}]['count']++; } }); } } if (isset($field->order)) { $order = $field->order == 'title' ? 'title' : 'count'; $reverse = isset($field->options['order_dir']) && $field->options['order_dir'] == 'desc'; usort($values, function ($a, $b) use($order, $reverse) { if ($reverse) { return $a[$order] > $b[$order] ? 1 : -1; } else { return $a[$order] < $b[$order] ? 1 : -1; } }); } $return = []; foreach ($values as $key => $value) { $return[$value['value']] = $value['title'] . ' (' . $value['count'] . ')'; } return $return; }
public function get_page_selector() { return get::__namespace($this->module, 0) . (isset($this->module->current) && $this->module->current->get_primary_key() ? '-' . $this->module->current->get_primary_key() : ''); }
/** * @return node */ public function get_cms_edit() { $form = $this->get_form(); $form->wrapper_class[] = 'container'; $form->wrapper_class[] = 'panel'; $form->wrapper_class[] = 'panel-body'; $form->id = 'cms_edit'; $form->set_from_request(); $form->set_from_object($this); foreach ($form->fields as $field) { if ($field instanceof field_file) { $form->action = '/index.php?module=' . get_class($this) . '&act=do_form_submit&no_ajax=on&ajax_origin=' . $form->id; } else { if ($field instanceof field_textarea) { $options = []; if (file_exists(root . '/js/ckeditor.js')) { $options['customConfig'] = '/js/ckeditor.js'; } \core::$inline_script[] = 'CKEDITOR.replace("' . $field->field_name . '"' . ($options ? ', ' . json_encode($options) : '') . ');'; } else { if ($field instanceof field_mlink) { $class = $field->get_link_object(); $class_name = get::__class_name($class); $this->do_retrieve_from_id([$class_name . '.' . $class->get_primary_key_name()], $this->get_primary_key()); } else { if ($field instanceof field_link) { $field->order = 'title'; } } } } $field->label .= ' <small class="field_name">(' . $field->field_name . ')</small>'; $field->raw = true; } if (!$this->get_primary_key()) { $form->get_field_from_name($this->get_primary_key_name())->set_attr('hidden', true); $form->{'parent_' . $this->get_primary_key_name()} = 0; } return $form->get_html(); }