Пример #1
0
 public function doNeighbours()
 {
     $item = fx::env('page');
     $q = $this->getFinder()->order(null)->limit(1)->where('site_id', fx::env('site_id'));
     $q_next = clone $q;
     $q_prev = clone $q;
     if ($this->getParam('sorting') === 'auto' && $item['infoblock_id']) {
         $item_ib_params = fx::data('infoblock', $item['infoblock_id'])->get('params');
         $ib_sorting = $item_ib_params['sorting'];
         $this->setParam('sorting', $ib_sorting == 'manual' || $ib_sorting == 'auto' ? 'priority' : $ib_sorting);
         $this->setParam('sorting_dir', $item_ib_params['sorting_dir']);
     }
     $sort_field = $this->getParam('sorting', 'priority');
     if ($sort_field === 'auto') {
         $sort_field = 'priority';
     }
     $dir = strtolower($this->getParam('sorting_dir', 'asc'));
     $where_prev = array(array($sort_field, $item[$sort_field], $dir == 'asc' ? '<' : '>'));
     $where_next = array(array($sort_field, $item[$sort_field], $dir == 'asc' ? '>' : '<'));
     $group_by_parent = $this->getParam('group_by_parent');
     if ($group_by_parent) {
         $c_parent = fx::content($item['parent_id']);
         // todo: psr0 need verify
         $q_prev->order('parent.priority', 'desc')->where('parent.priority', $c_parent['priority'], '<=');
         $q_next->order('parent.priority', 'asc')->where('parent.priority', $c_parent['priority'], '>=');
         $where_prev[] = array('parent_id', $item['parent_id'], '!=');
         $where_next[] = array('parent_id', $item['parent_id'], '!=');
     }
     $q_prev->order($sort_field, $dir == 'asc' ? 'desc' : 'asc')->where($where_prev, null, 'or');
     $prev = $q_prev->all();
     $q_next->order($sort_field, $dir)->where($where_next, null, 'or');
     $next = $q_next->all();
     //fx::log($q_prev->showQuery(), $q_next->showQuery());
     return array('prev' => $prev, 'current' => $item, 'next' => $next);
 }
Пример #2
0
 protected function addSubmenuItems($e)
 {
     $items = $e['items'];
     $submenu_type = $this->getParam('submenu');
     if ($submenu_type === 'none') {
         return;
     }
     $finder = fx::content($this->getComponent()->get('keyword'));
     switch ($submenu_type) {
         case 'all':
             $finder->descendantsOf($items);
             break;
         case 'active':
             $path = fx::env('page')->getPath();
             $finder->where('parent_id', $path->getValues('id'));
             break;
     }
     $items->concat($finder->all());
 }
Пример #3
0
 public function handleMove()
 {
     $rel_item_id = null;
     if (isset($this['__move_before'])) {
         $rel_item_id = $this['__move_before'];
         $rel_dir = 'before';
     } elseif (isset($this['__move_after'])) {
         $rel_item_id = $this['__move_after'];
         $rel_dir = 'after';
     }
     if (!$rel_item_id) {
         return;
     }
     $rel_item = fx::content($rel_item_id);
     if (!$rel_item) {
         return;
     }
     $rel_priority = fx::db()->getVar(array('select priority from {{floxim_main_content}} where id = %d', $rel_item_id));
     //fx::debug($rel_priority, $rel_item_id);
     if ($rel_priority === false) {
         return;
     }
     // 1 2 3 |4| 5 6 7 (8) 9 10
     $old_priority = $this['priority'];
     $this['priority'] = $rel_dir == 'before' ? $rel_priority : $rel_priority + 1;
     $q_params = array();
     $q = 'update {{floxim_main_content}} ' . 'set priority = priority + 1 ' . 'where ';
     if ($this['parent_id']) {
         $q .= 'parent_id = %d and ';
         $q_params[] = $this['parent_id'];
     }
     $q .= 'infoblock_id = %d ' . 'and priority >= %d ' . 'and id != %d';
     $q_params[] = $this['infoblock_id'];
     $q_params[] = $this['priority'];
     $q_params[] = $this['id'];
     if ($old_priority !== null) {
         $q .= ' and priority < %d';
         $q_params[] = $old_priority;
     }
     array_unshift($q_params, $q);
     fx::db()->query($q_params);
 }
Пример #4
0
 /**
  * List all content of specified type
  *
  * @param type $input
  */
 public function all($input)
 {
     $content_type = $input['content_type'];
     $list = array('type' => 'list', 'values' => array(), 'labels' => array('id' => 'ID'), 'entity' => 'content');
     if ($content_type === 'content') {
         $list['labels']['type'] = 'Type';
     }
     $com = fx::data('component', $content_type);
     $fields = $com->getAllFields();
     $ib_field = $fields->findOne('keyword', 'infoblock_id');
     if ($ib_field) {
         $list['labels']['infoblock'] = $ib_field['name'];
     }
     $fields->findRemove(function ($f) {
         if ($f['keyword'] === 'parent_id' || $f['keyword'] === 'type' || $f['keyword'] === 'site_id') {
             return false;
         }
         return $f['type_of_edit'] == Field\Entity::EDIT_NONE;
     });
     foreach ($fields as $f) {
         $list['labels'][$f['keyword']] = $f['name'];
     }
     $finder = fx::content($content_type);
     $pager = $finder->createPager(array('url_template' => $input['url_template'], 'current_page' => $input['page'], 'items_per_page' => 100));
     $items = $finder->all();
     $list['pager'] = $pager->getData();
     $ib_ids = $items->getValues('infoblock_id');
     $infoblocks = fx::data('infoblock', $ib_ids)->indexUnique('id');
     foreach ($items as $item) {
         $r = array('id' => $item['id']);
         $r['type'] = $item['type'];
         $c_ib = $infoblocks->findOne('id', $item['infoblock_id']);
         $r['infoblock'] = $c_ib ? $c_ib['name'] : '-';
         foreach ($fields as $f) {
             $val = $item[$f['keyword']];
             switch ($f['type']) {
                 case Field\Entity::FIELD_LINK:
                     if ($val) {
                         $linked = fx::data($f->getRelatedType(), $val);
                         $val = $linked['name'];
                     }
                     break;
                 case Field\Entity::FIELD_STRING:
                 case Field\Entity::FIELD_TEXT:
                     $val = strip_tags($val);
                     $val = mb_substr($val, 0, 150);
                     break;
                 case Field\Entity::FIELD_IMAGE:
                     $val = fx::image($val, 'max-width:100px,max-height:50px');
                     $val = '<img src="' . $val . '" alt="" />';
                     break;
                 case Field\Entity::FIELD_MULTILINK:
                     $val = fx::alang('%d items', 'system', count($val));
                     break;
             }
             $r[$f['keyword']] = $val;
         }
         $list['values'][] = $r;
     }
     $this->response->addButtons(array(array('key' => "delete", 'content_type' => $content_type)));
     return array('fields' => array('list' => $list));
 }
Пример #5
0
 /**
  * Sort collection of infoblocks by scope "strength":
  * 1. "this page only" - the best
  * 2. "children of type..."
  * 3. "page and children of type"
  * 4. "children"
  * 5. "page and children"
  * If blocks have same expression type, the strongest is one bound to the deeper page
  * If mount page is the same, the newer is stronger
  *
  * @param fx_collection $infoblocks
  */
 public function sortInfoblocks($infoblocks)
 {
     $infoblocks->sort(function ($a, $b) {
         $a_scope = $a->getScopeWeight();
         $b_scope = $b->getScopeWeight();
         if ($a_scope > $b_scope) {
             return -1;
         }
         if ($a_scope < $b_scope) {
             return 1;
         }
         $a_page = fx::content('page', $a['page_id']);
         $b_page = fx::content('page', $b['page_id']);
         $a_level = $a_page ? count($a_page->getParentIds()) : 0;
         $b_level = $b_page ? count($b_page->getParentIds()) : 0;
         if ($a_level > $b_level) {
             return -1;
         }
         if ($a_level < $b_level) {
             return 1;
         }
         return $a['id'] < $b['id'] ? 1 : -1;
     });
     return $infoblocks;
 }
Пример #6
0
 protected function getLostFinder()
 {
     $finder = fx::content($this->getComponent()->get('keyword'))->where('infoblock_id', 0)->where('site_id', fx::env('site_id'));
     return $finder;
 }
Пример #7
0
 public function saveVar($input)
 {
     $result = array();
     if (isset($input['page_id'])) {
         fx::env('page_id', $input['page_id']);
     }
     $ib = fx::data('infoblock', $input['infoblock']['id']);
     if ($ib->isLayout()) {
         $root_ib = $ib->getRootInfoblock();
         $ib_visual = $root_ib->getVisual();
     } elseif ($visual_id = fx::dig($input, 'infoblock.visual_id')) {
         $ib_visual = fx::data('infoblock_visual', $visual_id);
     } else {
         $ib_visual = $ib->getVisual();
     }
     // group vars by type to process content vars first
     // because we need content id for 'content-visual' vars on adding a new entity
     $vars = fx::collection($input['vars'])->apply(function ($v) {
         if ($v['var']['type'] == 'livesearch' && !$v['value']) {
             $v['value'] = array();
         }
     })->group(function ($v) {
         return $v['var']['var_type'];
     });
     $contents = fx::collection();
     $new_entity = null;
     if (isset($input['new_entity_props'])) {
         $new_props = $input['new_entity_props'];
         $new_com = fx::component($new_props['type']);
         $new_entity = fx::content($new_props['type'])->create($new_props);
         $contents['new@' . $new_com['id']] = $new_entity;
         // we are working with linker and user pressed "add new" button to create linked entity
         if (isset($input['create_linked_entity'])) {
             $linked_entity_com = fx::component($input['create_linked_entity']);
             $linked_entity = fx::content($linked_entity_com['keyword'])->create();
             $contents['new@' . $linked_entity_com['id']] = $linked_entity;
             // bind the new entity to the linker prop
             if (isset($new_props['_link_field'])) {
                 $link_field = $new_com->getFieldByKeyword($new_props['_link_field'], true);
                 $target_prop = $link_field['format']['prop_name'];
                 $new_entity[$target_prop] = $linked_entity;
             }
         }
     }
     if (isset($vars['content'])) {
         $content_groups = $vars['content']->group(function ($v) {
             $vid = $v['var']['content_id'];
             if (!$vid) {
                 $vid = 'new';
             }
             return $vid . '@' . $v['var']['content_type_id'];
         });
         foreach ($content_groups as $content_id_and_type => $content_vars) {
             list($content_id, $content_type_id) = explode("@", $content_id_and_type);
             if ($content_id !== 'new') {
                 $c_content = fx::content($content_type_id, $content_id);
                 if (!$c_content) {
                     continue;
                 }
                 $contents[$content_id_and_type] = $c_content;
             }
             $vals = array();
             foreach ($content_vars as $var) {
                 $vals[$var['var']['name']] = $var['value'];
             }
             if (isset($contents[$content_id_and_type])) {
                 $contents[$content_id_and_type]->setFieldValues($vals, array_keys($vals));
             } else {
                 fx::log('Content not found in group', $contents, $content_id, $vals);
             }
         }
     }
     $new_id = false;
     $result['saved_entities'] = array();
     foreach ($contents as $cid => $c) {
         try {
             $c->save();
             $result['saved_entities'][] = $c->get();
             if ($cid == 'new') {
                 $new_id = $c['id'];
             }
         } catch (\Exception $e) {
             $result['status'] = 'error';
             if ($e instanceof \Floxim\Floxim\System\Exception\EntityValidation) {
                 $result['errors'] = $e->toResponse();
             }
             break;
         }
     }
     if (isset($vars['visual'])) {
         foreach ($vars['visual'] as $c_var) {
             $var = $c_var['var'];
             $value = $c_var['value'];
             $var['id'] = preg_replace("~\\#new_id\\#\$~", $new_id, $var['id']);
             $visual_set = $var['template_is_wrapper'] ? 'wrapper_visual' : 'template_visual';
             if ($value == 'null') {
                 $value = null;
             }
             $c_visual = $ib_visual[$visual_set];
             if (!is_array($c_visual)) {
                 $c_visual = array();
             }
             if ($value == 'null') {
                 unset($c_visual[$var['id']]);
             } else {
                 $c_visual[$var['id']] = $value;
             }
             $ib_visual[$visual_set] = $c_visual;
         }
         $ib_visual->save();
     }
     if (isset($vars['ib_param'])) {
         $modified_params = array();
         foreach ($vars['ib_param'] as $c_var) {
             $var = $c_var['var'];
             $value = $c_var['value'];
             if (!isset($var['stored']) || $var['stored'] && $var['stored'] != 'false') {
                 $ib->digSet('params.' . $var['name'], $value);
             }
             $modified_params[$var['name']] = $value;
         }
         if (count($modified_params) > 0) {
             $controller = $ib->initController();
             $ib->save();
             $controller->handleInfoblock('save', $ib, array('params' => $modified_params));
         }
     }
     return $result;
 }
Пример #8
0
<?php

use Floxim\Floxim\System\Fx as fx;
$component = $this->getComponent();
$sort_fields = $component->getAllFields()->find(function ($f) {
    if ($f instanceof \Floxim\Floxim\Field\Link || $f instanceof \Floxim\Floxim\Field\MultiLink || $f instanceof \Floxim\Floxim\Field\Text || $f instanceof \Floxim\Floxim\Field\Image || in_array($f['keyword'], array('priority', 'is_published', 'is_branch_published', 'type', 'url', 'h1', 'title'))) {
        return false;
    }
    return true;
})->getValues(fx::isAdmin() ? 'name' : 'id', 'keyword');
$content_exists = fx::content($component['keyword'])->contentExists();
$is_new_infoblock = !$this->getParam('infoblock_id');
$component_infoblocks = fx::data('infoblock')->getContentInfoblocks($component['keyword']);
fx::cdebug(debug_backtrace());
return array('actions' => array('*list*' => array('settings' => array('limit' => array('label' => fx::alang('Count entries', 'controller_component'), 'class_name' => 'fx_field_limit'), 'pagination' => array('label' => fx::alang('Paginate?', 'controller_component'), 'type' => 'hidden', 'parent' => array('limit' => '!=')), 'sorting' => array('name' => 'sorting', 'label' => fx::alang('Sorting', 'controller_component'), 'type' => 'select', 'values' => $sort_fields), 'sorting_dir' => array('name' => 'sorting_dir', 'label' => fx::alang('Order', 'controller_component'), 'type' => 'select', 'values' => array('asc' => fx::alang('Ascending', 'controller_component'), 'desc' => fx::alang('Descending', 'controller_component')), 'parent' => array('sorting' => '!=manual')))), '*list' => array('disabled' => true), '*list_infoblock' => array('name' => fx::util()->ucfirst($component->getItemName('list')), 'install' => function ($ib, $ctr, $params) {
    $ctr->bindLostContent($ib, $params);
}, 'default_scope' => function () {
    $ds = fx::env('page_id') . '-this-';
    return $ds;
}, 'settings' => array('sorting' => array('values' => array(array('manual', fx::alang('Manual', 'controller_component'))) + $sort_fields)) + $this->getParentConfigFields() + $this->getTargetConfigFields() + $this->getLostContentField(), 'defaults' => array('!pagination' => true)), '*list_filtered' => array('name' => fx::util()->ucfirst(fx::alang('%s by filter', 'controller_component', $component->getItemName('list'))), 'settings' => array('conditions' => function ($ctr) {
    return $ctr->getConditionsField();
}, 'sorting' => array('values' => array(array('manual', fx::alang('Manual', 'controller_component'))) + $sort_fields)), 'defaults' => array('limit' => 10)), '*list_selected' => array('name' => fx::util()->ucfirst(fx::alang('%s selected', 'controller_component', $component->getItemName('list'))), 'settings' => array('selected' => function ($ctr) {
    return $ctr->getSelectedField();
}, 'allow_select_doubles' => array('type' => 'checkbox', 'label' => fx::alang('Allow doubles', 'controller_component')), 'is_pass_through' => array('label' => fx::alang('Pass-through data', 'controller_component'), 'type' => 'checkbox', 'parent' => array('scope[complex_scope]' => '!~this')), 'sorting' => array('values' => array(array('manual', fx::alang('Manual', 'controller_component'))) + $sort_fields)), 'defaults' => array('!pagination' => false, '!limit' => 0, '!allow_select_doubles' => true, 'is_pass_through' => 'false'), 'save' => function ($ib, $ctr, $params) {
    // update linkers
    $ctr->saveSelectedLinkers($params['params']['selected']);
}, 'delete' => function ($ib, $ctr, $params) {
    // drop linkers
    $ctr->dropSelectedLinkers();
}), '*list_filtered*, *list_selected*, *listing_by*' => array('check_context' => function () use($content_exists) {
    return $content_exists;