Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
 public function get_filters()
 {
     $filters = filter::get_all(['title', 'link_mid AS link_mid', 'link_fid AS link_fid', 'order'], ['where_equals' => ['link_mid' => static::get_module_id()]]);
     $filters->iterate(function (filter $filter) {
         foreach ($this->get_fields() as $field) {
             if ($field->fid == $filter->link_fid) {
                 $filter->set_field($field);
                 return;
             }
         }
         throw new \RuntimeException('Filter field ' . $filter->fid . ' is linked to a field that doesn\'t belong to its module');
     });
     return $filters;
 }