/** * Constructor * * Instantiate the datalist text input form element * * @param string $name * @param array $values * @param string $value * @param string $indent * @return Datalist */ public function __construct($name, array $values, $value = null, $indent = null) { parent::__construct($name, $value, $indent); $this->setAttribute('list', $name . '_datalist'); if (null !== $values) { $this->datalist = new Child('datalist', null, null, $this->indent); $this->datalist->setAttribute('id', $name . '_datalist'); foreach ($values as $key => $val) { $this->datalist->addChild((new Child('option', $val))->setAttribute('value', $key)); } } }
/** * Constructor * * Instantiate the select form element object * * @param string $name * @param string|array $values * @param string $indent * @param array $config * @return Select */ public function __construct($name, $values, $indent = null, array $config = null) { $marked = isset($config['marked']) ? $config['marked'] : null; $multiple = isset($config['multiple']) ? (bool) $config['multiple'] : false; $data = isset($config['data']) ? $config['data'] : null; parent::__construct($this->type, null, null, false, $indent); $this->setAttributes(['name' => $name, 'id' => $name]); $this->setAsMultiple($multiple); $this->setMarked($marked); $values = self::parseValues($values, $data); // Create the child option elements. foreach ($values as $k => $v) { if (is_array($v)) { $opt = new Child('optgroup', null, null, false, $indent); $opt->setAttribute('label', $k); foreach ($v as $ky => $vl) { $o = new Child('option', null, null, false, $indent); $o->setAttribute('value', $ky); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($ky, $this->marked, true)) { $o->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $ky == $this->marked) { $o->setAttribute('selected', 'selected'); } } $o->setNodeValue($vl); $opt->addChild($o); } } else { $opt = new Child('option', null, null, false, $indent); $opt->setAttribute('value', $k); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($k, $this->marked, true)) { $opt->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $k == $this->marked) { $opt->setAttribute('selected', 'selected'); } } $opt->setNodeValue($v); } $this->addChild($opt); } $this->setValue($values); $this->setName($name); }
<?php require_once '../../bootstrap.php'; use Pop\Dom\Child; use Pop\Dom\Dom; try { $title = new Child('title', 'This is the title'); $meta = new Child('meta'); $meta->setAttributes(array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')); $head = new Child('head'); $head->addChildren(array($title, $meta)); $h1 = new Child('h1', 'This is a header'); $p = new Child('p', 'This is a paragraph.'); $div = new Child('div'); $div->setAttributes('id', 'contentDiv'); $div->addChildren(array($h1, $p)); $body = new Child('body'); $body->addChild($div); $html = new Child('html'); $html->setAttributes(array('xmlns' => 'http://www.w3.org/1999/xhtml', 'xml:lang' => 'en')); $html->addChildren(array($head, $body)); $doc = new Dom(Dom::XHTML11, 'utf-8', $html); $doc->render(); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
/** * Build single view * * @param mixed $object * @param string $dateFormat * @throws \Phire\Exception * @return mixed */ public function buildSingle($object, $dateFormat = null) { if (!isset($this->data['id'])) { throw new \Phire\Exception('Error: A view has not been selected.'); } $view = null; $viewName = str_replace(' ', '-', strtolower($this->data['name'])); $linkField = $this->hasLinkField($this->data['single_fields_names']); $dateField = $this->hasDateField($this->data['single_fields_names']); if (null === $linkField && isset($object->uri)) { $linkField = 'uri'; } if (null === $dateField && isset($object->publish)) { $dateField = 'publish'; } switch ($this->data['single_style']) { case 'table': $view = new Child('table'); $view->setAttributes(['id' => $viewName . '-single-view-' . $this->data['id'], 'class' => $viewName . '-single-view']); foreach ($this->data['single_fields_names'] as $field) { if ($field !== $linkField) { $tr = new Child('tr'); if ($this->data['single_headers']) { $tr->addChild(new Child('th', ucwords(str_replace(['_', '-'], [' ', ' '], $field)) . ':')); } if ($field == 'title' && null !== $linkField && isset($object[$field]) && isset($object[$linkField])) { $td = new Child('td'); $a = new Child('a', $object[$field]); $a->setAttribute('href', $object[$linkField]); $td->addChild($a); $tr->addChild($td); } else { if ($field !== $linkField) { if (isset($object[$field])) { if ($field === $dateField && null !== $dateFormat) { $value = date($dateFormat, strtotime($object[$field])); } else { $value = $object[$field]; } } else { $value = ' '; } $tr->addChild(new Child('td', $value)); } } $view->addChild($tr); } } break; case 'ul': case 'ol': $view = new Child('div'); $view->setAttributes(['id' => $viewName . '-single-view-' . $this->data['id'], 'class' => $viewName . '-single-view']); $list = $this->data['single_style'] == 'ol' ? new Child('ol') : new Child('ul'); foreach ($this->data['single_fields_names'] as $field) { $li = new Child('li', null, null, true); if ($this->data['single_headers'] && $field !== $linkField) { $li->addChild(new Child('strong', ucwords(str_replace(['_', '-'], [' ', ' '], $field)) . ':')); } if ($field == 'title' && null !== $linkField && isset($object[$field]) && isset($object[$linkField])) { $a = new Child('a', $object[$field]); $a->setAttribute('href', $object[$linkField]); $li->addChild($a); $list->addChild($li); } else { if ($field !== $linkField) { if (isset($object[$field])) { if ($field === $dateField && null !== $dateFormat) { $value = date($dateFormat, strtotime($object[$field])); } else { $value = $object[$field]; } } else { $value = ' '; } $li->setNodeValue($value); $list->addChild($li); } } } $view->addChild($list); break; case 'div': $view = new Child('div'); $view->setAttributes(['id' => $viewName . '-single-view-' . $this->data['id'], 'class' => $viewName . '-single-view']); $section = new Child('section'); if (in_array('title', $this->data['single_fields_names']) && isset($object['title'])) { if (null !== $linkField && isset($object[$linkField])) { $h2 = new Child('h2'); $a = new Child('a', $object['title']); $a->setAttribute('href', $object[$linkField]); $h2->addChild($a); } else { $h2 = new Child('h2', $object['title']); } $section->addChild($h2); } if (null !== $dateField && null !== $dateFormat) { if (in_array($dateField, $this->data['single_fields_names']) && isset($object[$dateField])) { $section->addChild(new Child('h5', date($dateFormat, strtotime($object[$dateField])))); } } foreach ($this->data['single_fields_names'] as $field) { if ($field !== 'title' && ($field !== $dateField || null === $dateFormat) && $field !== $linkField) { $value = isset($object[$field]) ? $object[$field] : ' '; if ($this->data['single_headers']) { $value = '<strong>' . ucwords(str_replace(['_', '-'], [' ', ' '], $field)) . '</strong>: ' . $value; } $section->addChild(new Child('p', $value)); } } $view->addChild($section); break; } return $view; }
/** * Build rate request * * @return void */ protected function buildRateRequest() { $rating = new Child('RatingServiceSelectionRequest'); $request = new Child('Request'); $transaction = new Child('TransactionReference'); $pickup = new Child('PickupType'); $shipment = new Child('Shipment'); $customer = new Child('CustomerContext', 'Rating and Service'); $xpci = new Child('XpciVersion', '1.0'); $requestAction = new Child('RequestAction', 'Rate'); $requestOption = new Child('RequestOption', 'Shop'); $transaction->addChild($customer)->addChild($xpci); $request->addChild($transaction)->addChild($requestAction)->addChild($requestOption); $pickup->addChild(new Child('Code', $this->pickupType))->addChild(new Child('Description', self::$pickupTypes[$this->pickupType])); $shipment->addChild(new Child('Description', 'Rate')); $shipper = new Child('Shipper'); $shipper->addChild(new Child('ShipperNumber', $this->userId)); $shipTo = new Child('ShipTo'); $shipFrom = new Child('ShipFrom'); if (null !== $this->shipTo['CompanyName']) { $shipTo->addChild(new Child('CompanyName', $this->shipTo['CompanyName'])); } if (null !== $this->shipFrom['CompanyName']) { $shipFrom->addChild(new Child('CompanyName', $this->shipFrom['CompanyName'])); } $shipToAddress = new Child('Address'); foreach ($this->shipTo as $key => $value) { if ($key !== 'CompanyName') { $shipToAddress->addChild(new Child($key, $value)); } } $shipFromAddress = new Child('Address'); foreach ($this->shipFrom as $key => $value) { if ($key !== 'CompanyName') { $shipFromAddress->addChild(new Child($key, $value)); } } $shipTo->addChild($shipToAddress); $shipFrom->addChild($shipFromAddress); $shipper->addChild($shipFromAddress); $service = new Child('Service'); $service->addChild(new Child('Code', $this->service))->addChild(new Child('Description', self::$services[$this->service])); $package = new Child('Package'); $packageType = new Child('PackagingType'); $packageType->addChild(new Child('Code', $this->packageType))->addChild(new Child('Description', self::$packagingTypes[$this->packageType])); $package->addChild($packageType)->addChild(new Child('Description', 'Rate')); if (null !== $this->dimensions['Length'] && null !== $this->dimensions['Width'] && null !== $this->dimensions['Height']) { $dimensions = new Child('Dimensions'); $unit = new Child('UnitOfMeasurement'); $unit->addChild(new Child('Code', $this->dimensions['UnitOfMeasurement'])); $dimensions->addChild($unit)->addChild(new Child('Length', $this->dimensions['Length']))->addChild(new Child('Width', $this->dimensions['Width']))->addChild(new Child('Height', $this->dimensions['Height'])); $package->addChild($dimensions); } $weight = new Child('PackageWeight'); $unit = new Child('UnitOfMeasurement'); $unit->addChild(new Child('Code', $this->weight['UnitOfMeasurement'])); $weight->addChild($unit)->addChild(new Child('Weight', $this->weight['Weight'])); $package->addChild($weight); $shipment->addChild($shipper)->addChild($shipTo)->addChild($shipFrom)->addChild($service)->addChild($package); $rating->addChild($request)->addChild($pickup)->addChild($shipment); $this->rateRequest->addChild($rating); }
/** * Method to render the form using a basic 1:1 DT/DD layout * * @return void */ protected function renderWithoutTemplate() { // Initialize properties. $this->output = null; $children = $this->form->getChildren(); $this->form->removeChildren(); $id = null !== $this->form->getAttribute('id') ? $this->form->getAttribute('id') . '-field-group-' : 'pop-form-field-group-'; // Create DL element. $i = 1; $dl = new Child('dl', null, null, false, $this->form->getIndent()); $dl->setAttributes('id', $id . $i); // Loop through the children and create and attach the appropriate DT and DT elements, with labels where applicable. foreach ($children as $child) { if ($child->getNodeName() == 'fieldset') { $chdrn = $child->getChildren(); if (isset($chdrn[0])) { $attribs = $chdrn[0]->getAttributes(); } } else { $attribs = $child->getAttributes(); } $name = isset($attribs['name']) ? $attribs['name'] : ''; $name = str_replace('[]', '', $name); if (count($this->groups) > 0) { if (isset($this->groups[$i]) && $this->groups[$i] == $name) { $this->form->addChild($dl); $i++; $dl = new Child('dl', null, null, false, $this->form->getIndent()); $dl->setAttributes('id', $id . $i); } } // Clear the password field from display. if ($child->getAttribute('type') == 'password') { $child->setValue(null); $child->setAttributes('value', null); } // If the element label is set, render the appropriate DT and DD elements. if (null !== $child->getLabel()) { // Create the DT and DD elements. $dt = new Child('dt', null, null, false, $this->form->getIndent() . ' '); $dd = new Child('dd', null, null, false, $this->form->getIndent() . ' '); // Format the label name. $lblName = $child->getNodeName() == 'fieldset' ? '1' : ''; $label = new Child('label', $child->getLabel(), null, false, $this->form->getIndent() . ' '); $label->setAttributes('for', $name . $lblName); $labelAttributes = $child->getLabelAttributes(); if (null !== $labelAttributes) { foreach ($labelAttributes as $a => $v) { $label->setAttributes($a, $v); } } else { if ($child->isRequired()) { $label->setAttributes('class', 'required'); } } // Add the appropriate children to the appropriate elements. $dt->addChild($label); $child->setIndent($this->form->getIndent() . ' '); $childChildren = $child->getChildren(); $child->removeChildren(); foreach ($childChildren as $cChild) { $cChild->setIndent($this->form->getIndent() . ' '); $child->addChild($cChild); } $dd->addChild($child); $dl->addChildren(array($dt, $dd)); // Else, render only a DD element. } else { $dd = new Child('dd', null, null, false, $this->form->getIndent() . ' '); $child->setIndent($this->form->getIndent() . ' '); $dd->addChild($child); $dl->addChild($dd); } } // Add the DL element and its children to the form element. $this->form->addChild($dl); $this->output = $this->form->render(true); }
/** * Build rate request * * @return void */ protected function buildRequest() { $package = new Child('Package'); $package->setAttributes('ID', '1ST'); $package->addChild(new Child('Service', 'ALL'))->addChild(new Child('ZipOrigination', $this->shipFrom['ZipOrigination']))->addChild(new Child('ZipDestination', $this->shipTo['ZipDestination']))->addChild(new Child('Pounds', $this->weight['Pounds']))->addChild(new Child('Ounces', $this->weight['Ounces']))->addChild(new Child('Container', $this->container))->addChild(new Child('Size', $this->containerSize)); if (null !== $this->dimensions['Length'] && null !== $this->dimensions['Width'] && null !== $this->dimensions['Height']) { $package->addChild(new Child('Width', $this->dimensions['Width']))->addChild(new Child('Length', $this->dimensions['Length']))->addChild(new Child('Height', $this->dimensions['Height'])); if (null == $this->dimensions['Girth']) { $this->dimensions['Girth'] = 2 * $this->dimensions['Width'] + 2 * $this->dimensions['Height']; } $package->addChild(new Child('Girth', $this->dimensions['Girth'])); } $package->addChild(new Child('Machinable', $this->machinable))->addChild(new Child('DropOffTime', '12:00'))->addChild(new Child('ShipDate', date('Y-m-d'))); $this->request->addChild($package); }
/** * Initialize the feed. * * @throws Exception * @return void */ protected function init() { if ($this->feedType == Writer::RSS) { // Set up the RSS child node. $rss = new Child('rss'); $rss->setAttributes('version', '2.0'); $rss->setAttributes('xmlns:content', 'http://purl.org/rss/1.0/modules/content/'); $rss->setAttributes('xmlns:wfw', 'http://wellformedweb.org/CommentAPI/'); // Set up the Channel child node and the header children. $channel = new Child('channel'); foreach ($this->headers as $key => $value) { $channel->addChild(new Child($key, $value)); } // Set up the Item child nodes and add them to the Channel child node. foreach ($this->items as $itm) { $item = new Child('item'); foreach ($itm as $key => $value) { $item->addChild(new Child($key, $value)); } $channel->addChild($item); } // Add the Channel child node to the RSS child node, add the RSS child node to the DOM. $rss->addChild($channel); $this->addChild($rss); } else { if ($this->feedType == Writer::ATOM) { // Set up the Feed child node. $feed = new Child('feed'); $feed->setAttributes('xmlns', 'http://www.w3.org/2005/Atom'); if (isset($this->headers['language'])) { $feed->setAttributes('xml:lang', $this->headers['language']); } // Set up the header children. foreach ($this->headers as $key => $value) { if ($key == 'author') { $auth = new Child($key); $auth->addChild(new Child('name', $value)); $feed->addChild($auth); } else { if ($key == 'link') { $link = new Child($key); $link->setAttributes('href', $value); $feed->addChild($link); } else { if ($key != 'language') { $val = stripos($key, 'date') !== false || stripos($key, 'published') !== false ? date($this->dateFormat, strtotime($value)) : $value; $feed->addChild(new Child($key, $val)); } } } } // Set up the Entry child nodes and add them to the Feed child node. foreach ($this->items as $itm) { $item = new Child('entry'); foreach ($itm as $key => $value) { if ($key == 'link') { $link = new Child($key); $link->setAttributes('href', $value); $item->addChild($link); } else { $val = stripos($key, 'date') !== false || stripos($key, 'published') !== false ? date($this->dateFormat, strtotime($value)) : $value; $item->addChild(new Child($key, $val)); } } $feed->addChild($item); } // Add the Feed child node to the DOM. $this->addChild($feed); } else { if ($this->feedType == Writer::JSON || $this->feedType == Writer::PHP) { // Set up the header data. foreach ($this->headers as $key => $value) { $val = stripos($key, 'date') !== false || stripos($key, 'published') !== false ? date($this->dateFormat, strtotime($value)) : $value; $this->data[$key] = $val; } // Set up the items data $this->data['items'] = array(); foreach ($this->items as $itm) { foreach ($itm as $key => $value) { $val = stripos($key, 'date') !== false || stripos($key, 'published') !== false ? date($this->dateFormat, strtotime($value)) : $value; $itm[$key] = $val; } $this->data['items'][] = $itm; } // Set the content type $this->contentType = $this->feedType == Writer::JSON ? 'application/json' : 'text/plain'; } else { throw new Exception('Error: The feed type must be only RSS, ATOM, JSON or PHP.'); } } } }
/** * Traverse the config object * * @param array $tree * @param int $depth * @param string $parentHref * @throws Exception * @return \Pop\Dom\Child */ protected function traverse(array $tree, $depth = 1, $parentHref = null) { // Create overriding top level parent, if set if ($depth == 1 && isset($this->config['top'])) { $parent = isset($this->config['top']) && isset($this->config['top']['node']) ? $this->config['top']['node'] : 'ul'; $child = null; if (isset($this->config['child']) && isset($this->config['child']['node'])) { $child = $this->config['child']['node']; } else { if ($parent == 'ul') { $child = 'li'; } } // Create parent node $nav = new Child($parent); // Set top attributes if they exist if (isset($this->config['top']) && isset($this->config['top']['id'])) { $nav->setAttributes('id', $this->config['top']['id']); } if (isset($this->config['top']) && isset($this->config['top']['class'])) { $nav->setAttributes('class', $this->config['top']['class']); } if (isset($this->config['top']['attributes'])) { foreach ($this->config['top']['attributes'] as $attrib => $value) { $nav->setAttributes($attrib, $value); } } } else { // Set up parent/child node names $parent = isset($this->config['parent']) && isset($this->config['parent']['node']) ? $this->config['parent']['node'] : 'ul'; $child = null; if (isset($this->config['child']) && isset($this->config['child']['node'])) { $child = $this->config['child']['node']; } else { if ($parent == 'ul') { $child = 'li'; } } // Create parent node $nav = new Child($parent); // Set parent attributes if they exist if (isset($this->config['parent']) && isset($this->config['parent']['id'])) { $nav->setAttributes('id', $this->config['parent']['id'] . '-' . $this->parentLevel); } if (isset($this->config['parent']) && isset($this->config['parent']['class'])) { $nav->setAttributes('class', $this->config['parent']['class'] . '-' . $depth); } if (isset($this->config['parent']['attributes'])) { foreach ($this->config['parent']['attributes'] as $attrib => $value) { $nav->setAttributes($attrib, $value); } } } $this->parentLevel++; $depth++; // Recursively loop through the nodes foreach ($tree as $node) { $allowed = true; if (isset($node['acl'])) { if (null === $this->acl) { throw new Exception('The access control object is not set.'); } if (null === $this->role) { throw new Exception('The current role is not set.'); } $resource = isset($node['acl']['resource']) ? $node['acl']['resource'] : null; $permission = isset($node['acl']['permission']) ? $node['acl']['permission'] : null; $allowed = $this->acl->isAllowed($this->role, $resource, $permission); } if ($allowed && isset($node['name']) && isset($node['href'])) { // Create child node and child link node $a = new Child('a', $node['name']); if (substr($node['href'], 0, 1) == '/' || substr($node['href'], 0, 1) == '#' || substr($node['href'], -1) == '#' || substr($node['href'], 0, 4) == 'http') { $href = $node['href']; } else { if (substr($parentHref, -1) == '/') { $href = $parentHref . $node['href']; } else { $href = $parentHref . '/' . $node['href']; } } $a->setAttributes('href', $href); if ($this->returnFalse && ($href == '#' || substr($href, -1) == '#')) { $a->setAttributes('onclick', 'return false;'); } $url = $_SERVER['REQUEST_URI']; if (strpos($url, '?') !== false) { $url = substr($url, strpos($url, '?')); } $linkClass = null; if ($href == $url) { if (isset($this->config['on'])) { $linkClass = $this->config['on']; } } else { if (isset($this->config['off'])) { $linkClass = $this->config['off']; } } // If the node has any attributes if (isset($node['attributes'])) { foreach ($node['attributes'] as $attrib => $value) { $value = $attrib == 'class' && null !== $linkClass ? $value . ' ' . $linkClass : $value; $a->setAttributes($attrib, $value); } } else { if (null !== $linkClass) { $a->setAttributes('class', $linkClass); } } if (null !== $child) { $navChild = new Child($child); // Set child attributes if they exist if (isset($this->config['child']) && isset($this->config['child']['id'])) { $navChild->setAttributes('id', $this->config['child']['id'] . '-' . $this->childLevel); } if (isset($this->config['child']) && isset($this->config['child']['class'])) { $navChild->setAttributes('class', $this->config['child']['class'] . '-' . ($depth - 1)); } if (isset($this->config['child']['attributes'])) { foreach ($this->config['child']['attributes'] as $attrib => $value) { $navChild->setAttributes($attrib, $value); } } // Add link node $navChild->addChild($a); $this->childLevel++; // If there are children, loop through and add them if (isset($node['children']) && is_array($node['children']) && count($node['children']) > 0) { $childrenAllowed = true; // Check if the children are allowed if (isset($node['acl'])) { $i = 0; foreach ($node['children'] as $nodeChild) { if (null === $this->acl) { throw new Exception('The access control object is not set.'); } if (null === $this->role) { throw new Exception('The current role is not set.'); } $resource = isset($nodeChild['acl']['resource']) ? $nodeChild['acl']['resource'] : null; $permission = isset($nodeChild['acl']['permission']) ? $nodeChild['acl']['permission'] : null; if (!$this->acl->isAllowed($this->role, $resource, $permission)) { $i++; } } if ($i == count($node['children'])) { $childrenAllowed = false; } } if ($childrenAllowed) { $navChild->addChild($this->traverse($node['children'], $depth, $href)); } } // Add child node $nav->addChild($navChild); } else { $nav->addChild($a); } } } return $nav; }
/** * Build social nav * * @return mixed */ public function buildNav() { $nav = null; $hasUrl = false; $config = Table\Config::findById('social_config'); if (isset($config->value) && !empty($config->value) && $config->value != '') { $cfg = unserialize($config->value); $style = $cfg['style']; $this->data = array_merge($this->data, $cfg); switch ($cfg['size']) { case 'large': $size = 64; break; case 'medium': $size = 48; break; default: $size = 32; } $nav = new Child('nav'); $nav->setAttribute('class', 'social-media-' . $size); foreach ($cfg['order'] as $name) { if (!empty($cfg['urls'][$name]['url']) && $cfg['urls'][$name]['url'] != '') { $a = new Child('a', ucfirst($name)); $a->setAttributes(['class' => $name . '-' . $size . '-' . $style, 'href' => $cfg['urls'][$name]['url']]); if ($cfg['urls'][$name]['new']) { $a->setAttribute('target', '_blank'); } $nav->addChild($a); $hasUrl = true; } } if (!$hasUrl) { $nav = null; } } return $nav; }
/** * Constructor * * Instantiate the form element object * * @param string $type * @param string $name * @param string $value * @param string|array $marked * @param string $indent * @throws Exception * @return \Pop\Form\Element */ public function __construct($type, $name, $value = null, $marked = null, $indent = null) { $this->name = $name; $this->type = $type; // Check the element type, else set the properties. if (!in_array($type, $this->allowedTypes)) { throw new Exception('Error: That input type is not allowed.'); } // Create the element based on the type passed. switch ($type) { // Textarea element case 'textarea': parent::__construct('textarea', null, null, false, $indent); $this->setAttributes(array('name' => $name, 'id' => $name)); $this->nodeValue = $value; $this->value = $value; break; // Select element // Select element case 'select': parent::__construct('select', null, null, false, $indent); $this->setAttributes(array('name' => $name, 'id' => $name)); // Create the child option elements. foreach ($value as $k => $v) { if (is_array($v)) { $opt = new Child('optgroup', null, null, false, $indent); $opt->setAttributes('label', $k); foreach ($v as $ky => $vl) { $o = new Child('option', null, null, false, $indent); $o->setAttributes('value', $ky); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($ky, $this->marked)) { $o->setAttributes('selected', 'selected'); } } else { if ($ky == $this->marked) { $o->setAttributes('selected', 'selected'); } } $o->setNodeValue($vl); $opt->addChild($o); } } else { $opt = new Child('option', null, null, false, $indent); $opt->setAttributes('value', $k); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($k, $this->marked)) { $opt->setAttributes('selected', 'selected'); } } else { if ($k == $this->marked) { $opt->setAttributes('selected', 'selected'); } } $opt->setNodeValue($v); } $this->addChild($opt); } $this->value = $value; break; // Radio element(s) // Radio element(s) case 'radio': parent::__construct('fieldset', null, null, false, $indent); $this->setAttributes('class', 'radio-btn-fieldset'); // Create the radio elements and related span elements. $i = null; foreach ($value as $k => $v) { $rad = new Child('input', null, null, false, $indent); $rad->setAttributes(array('type' => $type, 'class' => 'radio-btn', 'name' => $name, 'id' => $name . $i, 'value' => $k)); // Determine if the current radio element is checked. if ($k == $this->marked) { $rad->setAttributes('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttributes('class', 'radio-span'); $span->setNodeValue($v); $this->addChildren(array($rad, $span)); $i++; } $this->value = $value; break; // Checkbox element(s) // Checkbox element(s) case 'checkbox': parent::__construct('fieldset', null, null, false, $indent); $this->setAttributes('class', 'check-box-fieldset'); // Create the checkbox elements and related span elements. $i = null; foreach ($value as $k => $v) { $chk = new Child('input', null, null, false, $indent); $chk->setAttributes(array('type' => $type, 'class' => 'check-box', 'name' => $name . '[]', 'id' => $name . $i, 'value' => $k)); // Determine if the current radio element is checked. if (in_array($k, $this->marked)) { $chk->setAttributes('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttributes('class', 'check-span'); $span->setNodeValue($v); $this->addChildren(array($chk, $span)); $i++; } $this->value = $value; break; // Input element // Input element default: if ($type == 'button') { $nodeType = 'button'; $type = 'submit'; } else { $nodeType = 'input'; } parent::__construct($nodeType, null, null, false, $indent); $this->setAttributes(array('type' => $type, 'name' => $name, 'id' => $name)); if (!is_array($value)) { if ($nodeType == 'button') { $this->nodeValue = $value; } $this->setAttributes('value', $value); } $this->value = $value; } // If a certain value is marked (selected or checked), set the property here. if (null !== $marked) { $this->marked = $marked; } }
/** * Get content archive * * @param boolean $count * @return mixed */ public function getArchive($count = true) { $sql = Table\Content::sql(); $sql->select(['publish' => DB_PREFIX . 'content.publish', 'in_date' => DB_PREFIX . 'content_types.in_date']); $sql->select()->join(DB_PREFIX . 'content_types', [DB_PREFIX . 'content_types.id' => DB_PREFIX . 'content.type_id']); $sql->select()->where('status = :status')->where('in_date = :in_date'); $sql->select()->orderBy('publish', 'DESC'); $params = ['status' => 1, 'in_date' => 1]; $content = Table\Content::execute((string) $sql, $params); $archive = []; foreach ($content->rows() as $c) { $year = substr($c->publish, 0, 4); if (isset($archive[$year])) { $archive[$year]++; } else { $archive[$year] = 1; } } $archiveNav = null; if (count($archive) > 0) { $archiveNav = new Child('ul'); $archiveNav->setAttributes(['id' => 'archive-nav', 'class' => 'archive-nav']); foreach ($archive as $year => $num) { $link = $count ? $year . ' <span>(' . $num . ')</span>' : $year; $a = new Child('a', $link); $a->setAttribute('href', BASE_PATH . '/' . $year); $li = new Child('li'); $li->addChild($a); $archiveNav->addChild($li); } } return $archiveNav; }