Rev 7 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download
<?php namespace ProcessWire;/*** /site/templates/_func.php** Example of shared functions used by template files** This file is currently included by _init.php** For more information see README.txt**//*** Returns a shorten string, parameter is max. characters to return** Just get first x characters of text, eg for showing a teaser...** @param string $description* @param integer $max_length* @return string first $max_length characters of $description**/function shortText($description, $max_length = 240) {$the_description = strip_tags($description);if (strlen($the_description) > $max_length && preg_match('#^\s*(.{'. $max_length . ',}?)[,.\s]+.*$#s', $the_description, $matches)) {return $matches[1];} else {return $the_description;}} //function shortText/*** Given a float number, return a value 0.0 <= value <= 1.0.** The value represent an opacity value ranging from 0.0 (no transparancy) untill* 1.0 (full transparent).** @param float $opacity* @return float as string (0.0 <= value <= 1.0)**/function getOpacity($opacity) {if ($opacity > 1.0) $opacity = 1.0;if ($opacity < 0.0) $opacity = 0.0;return $opacity;} //function getOpacity/*** Builds a nested list (menu items) of a single menu.** A recursive function to display nested list of menu items.** @access private* @param Int $parent ID of menu item.* @param Array $menu Object of menu items to display.* @param Int $first Helper variable to designate first menu item.* @return string $out.**/function buildMenuFromObject($parent = 0, $menu, $first = 0) {if(!is_object($menu)) return;$out = '';$has_child = false;foreach ($menu as $m) {$newtab = $m->newtab ? " target='_blank'" : '';// if this menu item is a parent; create the sub-items/child-menu-itemsif ($m->parentID == $parent) {// if this menu item is a parent; create the inner-items/child-menu-items// if this is the first childif ($has_child === false) {$has_child = true;// This is a parentif ($first == 0){$out .= "<ul class='list-unstyled components'>\n";$first = 1;}else $out .= "\n<ul class='dropdown'>\n";}$class = $m->isCurrent ? ' class="active"' : '';// a menu item$out .= '<li' . $class . '><a href="' . $m->url . '"' . $newtab . '>' . $m->title;// if menu item has childrenif ($m->isParent) {$out .= '</a>';}else $out .= '</a>';// call function again to generate nested list for sub-menu items belonging to this menu item.$out .= buildMenuFromObject($m->id, $menu, $first);$out .= "</li>\n";}// end if parent}// end foreachif ($has_child === true) $out .= "</ul>\n";return $out;} //function buildMenuFromObject/*** Given a group of pages, render a simple <ul> navigation** This is here to demonstrate an example of a simple shared function.* Usage is completely optional.** @param PageArray $items* @return string**/function renderNav(PageArray $items) {// $out is where we store the markup we are creating in this function$out = '';// cycle through all the itemsforeach($items as $item) {// render markup for each navigation item as an <li>if($item->id == wire('page')->id) {// if current item is the same as the page being viewed, add a "current" class to it$out .= "<li class='current'>";} else {// otherwise just a regular list item$out .= "<li>";}// markup for the link$out .= "<a href='$item->url'>$item->title</a> ";// if the item has summary text, include that tooif($item->summary) $out .= "<div class='summary'>$item->summary</div>";// close the list item$out .= "</li>";}// if output was generated above, wrap it in a <ul>if($out) $out = "<ul class='nav'>$out</ul>\n";// return the markup we generated abovereturn $out;} //function renderNav/*** Given a group of pages, render a <ul> navigation tree** This is here to demonstrate an example of a more intermediate level* shared function and usage is completely optional. This is very similar to* the renderNav() function above except that it can output more than one* level of navigation (recursively) and can include other fields in the output.** @param array|PageArray $items* @param int $maxDepth How many levels of navigation below current should it go?* @param string $fieldNames Any extra field names to display (separate multiple fields with a space)* @param string $class CSS class name for containing <ul>* @return string**/function renderNavTree($items, $maxDepth = 0, $fieldNames = '', $class = 'nav') {// if we were given a single Page rather than a group of them, we'll pretend they// gave us a group of them (a group/array of 1)if($items instanceof Page) $items = array($items);// $out is where we store the markup we are creating in this function$out = '';// cycle through all the itemsforeach($items as $item) {// markup for the list item...// if current item is the same as the page being viewed, add a "current" class to it$out .= $item->id == wire('page')->id ? "<li class='current'>" : "<li>";// markup for the link$out .= "<a href='$item->url'>$item->title</a>";// if there are extra field names specified, render markup for each one in a <div>// having a class name the same as the field nameif($fieldNames) foreach(explode(' ', $fieldNames) as $fieldName) {$value = $item->get($fieldName);if($value) $out .= " <div class='$fieldName'>$value</div>";}// if the item has children and we're allowed to output tree navigation (maxDepth)// then call this same function again for the item's childrenif($item->hasChildren() && $maxDepth) {if($class == 'nav') $class = 'nav nav-tree';$out .= renderNavTree($item->children, $maxDepth-1, $fieldNames, $class);}// close the list item$out .= "</li>";}// if output was generated above, wrap it in a <ul>if($out) $out = "<ul class='$class'>$out</ul>\n";// return the markup we generated abovereturn $out;} //function renderNavTree