function create_full_cache($pages = null) { $search_fields = array('url', 'file_path', 'title', 'author', 'content'); $store = array(); if (!isset($pages)) { $pages = Helpers::file_cache('./content'); } foreach ($pages as $page) { if ($page['is_folder']) { $current_page = AssetFactory::get($page['path']); # Skip for password protected pages if ($current_page['password_protect'] || $current_page['hide_from_search']) { continue; } # Only save search field data foreach ($current_page as $key => $value) { if (!in_array($key, $search_fields)) { unset($current_page[$key]); } } $store[] = $current_page; $children = self::create_full_cache(Helpers::file_cache($page['path'])); if (is_array($children)) { $store = array_merge($store, $children); } } } return $store; }
function create_full_cache($pages = null) { $search_fields = array('url', 'file_path', 'title', 'author', 'content', 'object_name', 'designer', 'builder', 'category', 'tags', 'contributors', 'realisation_place', 'required_hardware', 'license', 'client'); $store = array(); if (!isset($pages)) { $pages = Helpers::file_cache('./content'); } foreach ($pages as $page) { if ($page['is_folder']) { $current_page = AssetFactory::get($page['path']); # Skip for password protected pages if (isset($current_page['password_protect']) || isset($current_page['hide_from_search'])) { continue; } # Only save search field data foreach ($current_page as $key => $value) { if (!in_array($key, $search_fields)) { unset($current_page[$key]); } } $store[] = $current_page; $children = self::create_full_cache(Helpers::file_cache($page['path'])); if (is_array($children)) { $store = array_merge($store, $children); } } } return $store; }
static function parse_foreach($data, $template) { # split out the partial into the parts Before, Inside, and After the foreach loop preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts); # run the replacements on the pre-"foreach" part of the partial $template = self::parse($data, $template_parts[1]); # traverse one level deeper into the data hierachy $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false; # check for any nested matches $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach'); if ($pages) { foreach ($pages as $data_item) { # transform data_item into its appropriate Object $data_object =& AssetFactory::get($data_item); # recursively parse the inside part of the foreach loop $template .= self::parse($data_object, $template_parts[3]); } } # run the replacements on the post-"foreach" part of the partial $template .= self::parse($data, $template_parts[4]); return $template; }
function sortby($object, $value) { $this->sortby_value = $value; $sorted = array(); # expand sub variables if required if (is_array($object)) { foreach ($object as $key) { if (is_string($key)) { $sorted[] =& AssetFactory::get($key); } } } # sort the array uasort($sorted, array($this, 'custom_str_sort')); return $sorted; }
static function parse_foreach($data, $template) { # split out the partial into the parts Before, Inside, and After the foreach loop preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts); # run the replacements on the pre-"foreach" part of the partial $template = self::parse($data, $template_parts[1]); # allow loop limiting if (preg_match('/\\[\\d*:\\d*\\]$/', $template_parts[2])) { preg_match('/([\\$\\@].+?)\\[(\\d*):(\\d*)\\]$/', $template_parts[2], $matches); $template_parts[2] = $matches[1]; $start_limit = empty($matches[2]) ? 0 : $matches[2]; if (!empty($matches[3])) { $end_limit = $matches[3]; } } # traverse one level deeper into the data hierachy $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false; # slice down the data array if required if (is_array($pages) && isset($start_limit)) { $pages = array_slice($pages, $start_limit, $end_limit); } # check for any nested matches $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach'); if ($pages) { foreach ($pages as $data_item) { # transform data_item into its appropriate Object $data_object =& AssetFactory::get($data_item); # recursively parse the inside part of the foreach loop $template .= self::parse($data_object, $template_parts[3]); } } # run the replacements on the post-"foreach" part of the partial $template .= self::parse($data, $template_parts[4]); return $template; }
static function parse_foreach($data, $template) { # split out the partial into the parts Before, Inside, and After the foreach loop preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts); # run the replacements on the pre-"foreach" part of the partial $template = self::parse($data, $template_parts[1]); # new: allow limitation syntax # e.g. $children[:5] to only get first 5 children preg_match('/([\\$\\@a-z0-9_].+?)([\\[\\]\\:\\d]+?|)$/', $template_parts[2], $limit); # if there is a limit set in the template if ($limit[2]) { preg_match('/\\[\\:([\\d]+?)\\]/', $limit[2], $limit[2]); # so here is the limit of things to get: $slice = $limit[2][1]; } else { $slice = false; } # i'm lazy. i won't break anything i have to fix afterwards. $template_parts[2] = $limit[1]; # traverse one level deeper into the data hierachy $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false; # check for any nested matches $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach'); if ($pages) { # slice the array according to set limit if ($slice) { $pages = array_slice($pages, 0, $slice); } foreach ($pages as $data_item) { # transform data_item into its appropriate Object $data_object =& AssetFactory::get($data_item); # recursively parse the inside part of the foreach loop $template .= self::parse($data_object, $template_parts[3]); } } # run the replacements on the post-"foreach" part of the partial $template .= self::parse($data, $template_parts[4]); return $template; }
/** * Returns the attribute value for a given array/object. * * @param mixed $object The object or array from where to get the item * @param mixed $item The item to get from the array or object * @param array $arguments An array of arguments to pass if the item is an object method * @param string $type The type of attribute (@see Twig_TemplateInterface) * @param Boolean $isDefinedTest Whether this is only a defined check * @param Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not * * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true * * @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false */ protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { $item = (string) $item; // Stacey-specific // If the attribute is a string path, then we need to pass it through the asset factory to create its page variables if (is_string($object)) { $object =& AssetFactory::get($object); } // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { if (is_array($object) && array_key_exists($item, $object) || $object instanceof ArrayAccess && isset($object[$item])) { if ($isDefinedTest) { return true; } return $object[$item]; } if (Twig_TemplateInterface::ARRAY_CALL === $type) { if ($isDefinedTest) { return false; } if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } if (is_object($object)) { throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object))); // array } else { throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object)))); } } } if (!is_object($object)) { if ($isDefinedTest) { return false; } if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object)); } $class = get_class($object); // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { /* apparently, this is not needed as this is already covered by the array_key_exists() call below if (!isset(self::$cache[$class]['properties'])) { foreach (get_object_vars($object) as $k => $v) { self::$cache[$class]['properties'][$k] = true; } } */ if (isset($object->{$item}) || array_key_exists($item, $object)) { if ($isDefinedTest) { return true; } if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); } return $object->{$item}; } } // object method if (!isset(self::$cache[$class]['methods'])) { self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object))); } $lcItem = strtolower($item); if (isset(self::$cache[$class]['methods'][$lcItem])) { $method = $item; } elseif (isset(self::$cache[$class]['methods']['get' . $lcItem])) { $method = 'get' . $item; } elseif (isset(self::$cache[$class]['methods']['is' . $lcItem])) { $method = 'is' . $item; } elseif (isset(self::$cache[$class]['methods']['__call'])) { $method = $item; } else { if ($isDefinedTest) { return false; } if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object))); } if ($isDefinedTest) { return true; } if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } $ret = call_user_func_array(array($object, $method), $arguments); if ($object instanceof Twig_TemplateInterface) { return new Twig_Markup($ret); } return $ret; }