Example #1
0
 public function getForInfoblocks(System\Collection $infoblocks, $layout_id)
 {
     $ib_ids = $infoblocks->getValues('id');
     $this->where('infoblock_id', $ib_ids);
     if ($layout_id) {
         $this->where('layout_id', $layout_id);
     }
     return $this->all();
 }
Example #2
0
 /**
  * For each $item of the current collection find corresponging $other from $what collection,
  * which have $cond_field equal to $item[$check_field]
  * and append them to $item[$res_field] as a new collection.
  * If $extract_field argument is specified, $other[$extract_field] is used instead if $other
  * @param Collection $what
  * @param string $cond_field
  * @param string $res_field
  * @param string $check_field
  * @param string $extract_field
  * @return \Floxim\Floxim\System\Collection
  */
 public function attachMany(Collection $what, $cond_field, $res_field, $check_field = 'id', $extract_field = null, $extract_real_field = null)
 {
     // what = [post1,post2]
     // this = [user1, user2]
     // cond_field = 'author'
     // res_field = 'posts'
     $res_index = array();
     foreach ($what as $what_item) {
         $index_key = $what_item[$cond_field];
         if (!isset($res_index[$index_key])) {
             if ($extract_field) {
                 $new_collection = fx::collection();
                 $new_collection->is_sortable = $what->is_sortable;
                 $linkers = $what->fork();
                 $linkers->addFilter($cond_field, $index_key);
                 if ($extract_real_field) {
                     $linkers->linkedBy = $extract_real_field;
                 }
                 $new_collection->linkers = $linkers;
                 if (isset($what->relFinders) && isset($what->relFinders[$extract_field])) {
                     $new_collection->finder = $what->relFinders[$extract_field];
                 }
             } else {
                 $new_collection = $what->fork();
                 $new_collection->addFilter($cond_field, $index_key);
             }
             $res_index[$index_key] = $new_collection;
         }
         if (!$extract_field) {
             $res_index[$index_key][] = $what_item;
         } else {
             $end_value = $what_item[$extract_field];
             $res_index[$index_key][] = $end_value;
             $res_index[$index_key]->linkers[] = $what_item;
         }
         //$res_index[$index_key][]= $extract_field ? $what_item[$extract_field] : $what_item;
     }
     foreach ($this as $our_item) {
         $check_value = $our_item[$check_field];
         $res_collection = isset($res_index[$check_value]) ? $res_index[$check_value] : fx::collection();
         $our_item[$res_field] = $res_collection;
         if ($our_item instanceof \Floxim\Main\Content\Entity && isset($res_collection->linkers)) {
             $res_collection->linkers->selectField = $our_item->getFormField($res_field);
         }
     }
     return $this;
 }
Example #3
0
 public function getAllFields()
 {
     if (is_null($this->all_fields)) {
         $fields = new System\Collection();
         foreach ($this->getChain() as $component) {
             $fields->concat($component->fields());
         }
         $fields->indexUnique('keyword');
         $this->all_fields = $fields;
     }
     return $this->all_fields;
 }
Example #4
0
 public function suit(System\Collection $infoblocks, $layout_id)
 {
     $layout = fx::data('layout', $layout_id);
     $layout_ib = null;
     $stub_ibs = new System\Collection();
     // Collect all Infoblox without the visual part
     // Find the InfoBlock-layout
     foreach ($infoblocks as $ib) {
         if ($ib->getVisual()->get('is_stub')) {
             $stub_ibs[] = $ib;
         }
         if ($ib->isLayout()) {
             $layout_ib = $ib;
         }
     }
     $layout_rate = array();
     $all_visual = fx::data('infoblock_visual')->getForInfoblocks($stub_ibs, false);
     foreach ($all_visual as $c_vis) {
         $c_layout_id = $c_vis['layout_id'];
         $infoblocks->findOne('id', $c_vis['infoblock_id'])->setVisual($c_vis, $c_layout_id);
         if (!isset($layout_rate[$c_layout_id])) {
             $layout_rate[$c_layout_id] = 0;
         }
         // count how many visual blocks are defined for each layout
         // later we should sort this to find correct "original" layout made by human
         $layout_rate[$c_layout_id]++;
     }
     //$source_layout_id = $c_layout_id;
     $avail_layout_ids = array_keys($layout_rate);
     // temp: use first
     // $source_layout_id = $avail_layout_ids[0];
     $source_layout_id = end($avail_layout_ids);
     if (!$layout_ib) {
         $layout_ib = fx::router('front')->getLayoutInfoblock(fx::env('page'));
     }
     $area_map = array();
     if ($layout_ib->getVisual()->get('is_stub') || !$layout_ib->getTemplate()) {
         $this->adjustLayoutVisual($layout_ib, $layout_id, $source_layout_id);
         $layout_visual = $layout_ib->getVisual();
         $area_map = $layout_visual['area_map'];
     } else {
         $layout_visual = $layout_ib->getVisual();
         $old_layout_template = $layout_ib->getPropInherited('visual.template', $source_layout_id);
         if ($old_layout_template) {
             $old_areas = fx::template($old_layout_template)->getAreas();
             $new_areas = fx::template($layout_visual['template'])->getAreas();
             //$tplv['real_areas'] = $test_layout_tpl->getAreas();
             $area_map = $this->mapAreas($old_areas, $new_areas);
             $area_map = $area_map['map'];
         }
     }
     $layout_template_name = $layout_ib->getPropInherited('visual.template');
     $layout_template = fx::template($layout_template_name);
     // seems to be second call of ::getAreas(), can be cached or reused
     $c_areas = $layout_template->getAreas();
     $c_wrappers = array();
     foreach ($infoblocks as $ib) {
         $ib_visual = $ib->getVisual($layout_id);
         if (!$ib_visual['is_stub']) {
             continue;
         }
         $old_area = $ib->getPropInherited('visual.area', $source_layout_id);
         // Suit record infoblock to the area where list infoblock is placed
         if ($ib->getPropInherited('action') == 'record') {
             $content_type = $ib->getPropInherited('controller');
             $content_ibs = fx::data('infoblock')->where('page_id', $ib['page_id'])->getContentInfoblocks($content_type);
             if (count($content_ibs)) {
                 $list_ib = $content_ibs->first();
                 $list_ib_vis = $list_ib->getVisual($layout_id);
                 if ($list_ib_vis && $list_ib_vis['area']) {
                     $ib_visual['area'] = $list_ib_vis['area'];
                 }
             }
         }
         if (!$ib_visual['area']) {
             if ($old_area && isset($area_map[$old_area])) {
                 $ib_visual['area'] = $area_map[$old_area];
                 $ib_visual['priority'] = $ib->getPropInherited('visual.priority', $source_layout_id);
             } elseif (preg_match("~^grid_~", $old_area)) {
                 $ib_visual['area'] = $old_area;
             }
         }
         $ib_controller = fx::controller($ib->getPropInherited('controller'), $ib->getPropInherited('params'), $ib->getPropInherited('action'));
         $area_meta = isset($c_areas[$ib_visual['area']]) ? $c_areas[$ib_visual['area']] : null;
         $controller_templates = $ib_controller->getAvailableTemplates($layout['keyword'], $area_meta);
         $old_template = $ib->getPropInherited('visual.template', $source_layout_id);
         $used_template_props = null;
         foreach ($controller_templates as $c_tpl) {
             if ($c_tpl['full_id'] === $old_template) {
                 $ib_visual['template'] = $c_tpl['full_id'];
                 $used_template_props = $c_tpl;
                 break;
             }
         }
         if (!$ib_visual['template']) {
             $that = $this;
             $old_template_id = preg_replace("~^.*?:~", '', $old_template);
             $controller_templates = fx::collection($controller_templates);
             $controller_templates->sort(function (&$tpl) use($that, $old_template_id) {
                 $res = $that->compareNames($tpl['id'], $old_template_id);
                 $tpl['name_match'] = $res;
                 return 1 / ($res + 1);
             });
             $res_template = $controller_templates->first();
             $ib_visual['template'] = $res_template['full_id'];
             $used_template_props = $res_template;
         }
         if (!$ib_visual['area']) {
             $block_size = self::getSize($used_template_props['size']);
             $c_area = null;
             $c_area_count = 0;
             foreach ($c_areas as $ca) {
                 $area_size = self::getSize($ca['size']);
                 $area_count = self::checkSizes($block_size, $area_size);
                 if ($area_count >= $c_area_count) {
                     $c_area_count = $area_count;
                     $c_area = $ca['id'];
                 }
             }
             $ib_visual['area'] = $c_area;
         }
         $old_wrapper = $ib->getPropInherited('visual.wrapper', $source_layout_id);
         if ($old_wrapper) {
             if (!isset($c_wrappers[$c_area])) {
                 $c_wrappers[$c_area] = self::getAvailableWrappers($layout_template, $area_meta);
             }
             $old_wrapper_id = preg_replace("~^.+\\:~", '', $old_wrapper);
             $avail_wrappers = $c_wrappers[$c_area];
             if (count($avail_wrappers)) {
                 $new_wrapper = fx::collection($avail_wrappers)->sort(function ($w) use($old_wrapper_id) {
                     return 1 / (1 + Suitable::compareNames($w['name'], $old_wrapper_id));
                 })->first();
                 $ib_visual['wrapper'] = $new_wrapper['full_id'];
                 $ib_visual['wrapper_visual'] = $ib->getPropInherited('visual.wrapper_visual', $source_layout_id);
             }
         }
         //if ($old_wrapper && $area_meta) {
         if ($area_meta) {
             $area_suit = self::parseAreaSuitProp(isset($area_meta['suit']) ? $area_meta['suit'] : null);
             if ($area_suit['default_wrapper']) {
                 $ib_visual['wrapper'] = $area_suit['default_wrapper'][0];
                 $ib_visual['wrapper_visual'] = $ib->getPropInherited('visual.wrapper_visual', $source_layout_id);
             }
         }
         unset($ib_visual['is_stub']);
         $ib_visual->save();
     }
 }