/** * Get all category items for the given block */ function _get_items_array($cat_name = '', $recursive_sort = true, $all = false) { if (empty($cat_name)) { $cat_name = $this->_default_cats_block; } $cat_id = $this->_get_cat_id_by_name($cat_name); if (empty($cat_id)) { return false; } $cache_name = intval($cat_id) . '_' . intval($all); if (!isset($this->_items_cache[$cache_name])) { $cat_id = $this->_get_cat_id_by_name($cat_name); $custom_fields = []; if ($cat_id) { foreach (explode(',', $this->_category_sets[$cat_id]['custom_fields']) as $f) { $f = trim($f); if ($f) { $custom_fields[$f] = $f; } } } $cat_items = $all ? 'category_items_all' : 'category_items'; foreach ((array) main()->get_data($cat_items) as $a) { if ($a['cat_id'] != $cat_id) { continue; } // Try to parse 'dynamic' attributes for the item if ($this->USE_DYNAMIC_ATTS && $custom_fields) { $custom_attrs = []; if ($a['other_info']) { $custom_attrs = (array) _attrs_string2array($a['other_info']); } foreach ((array) $custom_fields as $f) { $a[$f] = isset($custom_attrs[$f]) ? (string) $custom_attrs[$f] : ''; } } $raw_items[$a['id']] = $a; } $this->_items_cache[$cache_name] = $raw_items; } else { $raw_items = $this->_items_cache[$cache_name]; } if ($recursive_sort && !empty($raw_items)) { $raw_items = $this->_recursive_sort_items($raw_items); } return $raw_items ? $raw_items : false; }
/** */ function _apply_custom_fields($cur_menu_info, $menu_items) { $custom_fields = []; if ($cur_menu_info['custom_fields']) { foreach (explode(',', str_replace(';', ',', trim($cur_menu_info['custom_fields']))) as $f) { $f = trim($f); if ($f) { $custom_fields[$f] = $f; } } } if ($this->USE_DYNAMIC_ATTS && $custom_fields) { foreach ((array) $menu_items as $item_id => $item_info) { $custom_attrs = []; if (!strlen($item_info['other_info'])) { continue; } $custom_attrs = (array) _attrs_string2array($item_info['other_info']); foreach ((array) $custom_fields as $f) { $menu_items[$item_id][$f] = strval($custom_attrs[$f]); } } } return $menu_items; }
/** * Try to return class method output */ function call_class_method($class_name = '', $custom_path = '', $method_name = '', $method_params = '', $tpl_name = '', $silent = false, $use_cache = false, $cache_ttl = -1, $cache_key_override = '') { if (!strlen($class_name) || !strlen($method_name)) { return false; } $class_name === '@object' && ($class_name = $_GET['object']); $method_name === '@action' && ($method_name = $_GET['action']); if (!$this->USE_SYSTEM_CACHE) { $use_cache = false; } if ($use_cache) { $cache_name = $this->_get_exec_cache_name($class_name, $custom_path, $method_name, $method_params, $tpl_name); $cache_ttl = intval($cache_ttl); if ($cache_ttl < 1) { // set to 0 to use cache module default value $cache_ttl = $this->EXEC_CACHE_TTL; } $cached = $this->modules['cache']->get($cache_name, $cache_ttl); if (!empty($cached)) { return $cached[0]; } } if ($class_name == 'main') { $obj = $this; } else { $obj = $this->init_class($class_name, $custom_path, $method_params); if (!is_object($obj) && !$custom_path) { $custom_path = 'classes/'; $obj = $this->init_class($class_name, $custom_path, $method_params); } } if (!is_object($obj)) { if (!$silent) { trigger_error('MAIN: module "' . $class_name . '" init failed' . (!empty($tpl_name) ? ' (template "' . $tpl_name . '")' : ''), E_USER_WARNING); } return false; } if (!method_exists($obj, $method_name)) { if (!$silent) { trigger_error('MAIN: no method "' . $method_name . '" in module "' . $class_name . '"' . (!empty($tpl_name) ? ' (template "' . $tpl_name . '")' : ''), E_USER_WARNING); } return false; } // Try to process method params (string like attrib1=value1;attrib2=value2) if (is_string($method_params) && strlen($method_params)) { $method_params = (array) _attrs_string2array($method_params); } $result = $obj->{$method_name}($method_params); if ($use_cache) { $this->modules['cache']->set($cache_name, [$result]); } return $result; }
/** * Wrapper for '_PATTERN_INCLUDE', allows you to include stpl, optionally pass $replace params to it */ function _include_stpl($stpl_name = '', $params = '', $replace = [], $if_exists = false) { if (!is_array($replace)) { $replace = []; } $force_storage = ''; // Force to include template from special storage, example: @framework:script_js if ($stpl_name[0] === '@') { list($force_storage, $stpl_name) = explode(':', substr($stpl_name, 1)); } if ($if_exists && !tpl()->exists($stpl_name, $force_storage)) { return false; } $replace = (array) _attrs_string2array($params) + (array) $replace; return $this->parse($stpl_name, $replace); }
public function test_empty() { $this->assertEquals(['k1' => ''], _attrs_string2array('k1=')); $this->assertEquals(['k1' => ''], _attrs_string2array('k1')); $this->assertEquals(['k1' => ''], _attrs_string2array(' k1 = ')); }
/** * Wrapper around 'url()' function, called like this inside templates: * {url(object=home_page;action=test)} */ function _url_wrapper($params = []) { // Try to process method params (string like attrib1=value1;attrib2=value2) if (is_string($params) && strlen($params)) { // Url like this: /object/action/id if ($params[0] == '/') { // Do nothing, just directly pass this to url() as string } elseif (false !== strpos($params, '=')) { $params = _attrs_string2array($params); } else { list($object, $action, $id, $page) = explode(';', str_replace(',', ';', $params)); $params = ['object' => $object, 'action' => $action, 'id' => $id, 'page' => $page]; } } return url($params); }
/** */ function custom_fields($name, $custom_fields, $extra = [], $replace = []) { if (!is_array($extra)) { $extra = []; } $extra['name'] = $extra['name'] ?: $name; $extra['custom_fields'] = $custom_fields; $func = function ($extra, $r, $form) { $custom_fields = explode(',', $extra['custom_fields']); $sub_array_name = $extra['sub_array'] ?: 'custom'; $custom_info = _attrs_string2array($r[$extra['name']]); $body = []; $form->_chained_mode = false; foreach ((array) $custom_fields as $field_name) { if (empty($field_name)) { continue; } $str = _class('html')->input(['id' => 'custom_' . $field_name . '_' . $r['id'], 'name' => $sub_array_name . '[' . $field_name . ']', 'desc' => $field_name, 'value' => $custom_info[$field_name]]); $desc = ucfirst(str_replace('_', ' ', $field_name)) . ' [Custom]'; $body[] = $form->container($str, $desc); } $form->_chained_mode = true; return implode(PHP_EOL, $body); }; if ($this->_chained_mode) { $this->_body[] = ['func' => $func, 'extra' => $extra, 'replace' => $replace, 'name' => __FUNCTION__]; return $this; } return $func((array) $extra + (array) $this->_extra, (array) $replace + (array) $this->_replace, $this); }