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; }
public function isAvailableOnPage($page) { if ($this['site_id'] != $page['site_id']) { return; } if ($page->hasVirtualPath()) { $ids = $page->getPath()->getValues('id'); } else { $ids = $page->getParentIds(); } $ids[] = $page['id']; $ids[] = 0; // root if (!in_array($this['page_id'], $ids)) { return false; } // if page_id=0 blunt - all pages, ignored by the filter scope.pages if ($this['page_id'] != 0) { // scope - "this page only" if (fx::dig($this, 'scope.pages') == 'this' && $this['page_id'] != $page['id']) { return false; } // scope - "this level, and we look parent if (fx::dig($this, 'scope.pages') == 'children' && $this['page_id'] == $page['id']) { return false; } } // check for compliance with the filter type page $scope_page_type = fx::dig($this, 'scope.page_type'); if ($scope_page_type && fx::getComponentFullName($scope_page_type) != fx::getComponentFullName($page['type'])) { return false; } return true; }
/** * Get the option router by name * fx::router('front') */ public function getRouter($router_name) { $class = 'Floxim\\Floxim\\Router\\' . ucfirst($router_name); return fx::dig($this->routers, $class . '.router'); }