Exemple #1
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();
     }
 }