setAttributes() public method

Set an attribute or attributes for the child element object.
public setAttributes ( array | string $a, string $v = null ) : Child
$a array | string
$v string
return Child
Example #1
0
 public function testChild()
 {
     $c = new Child('p', 'This is a paragraph', new Child('p', 'This is another paragraph'));
     $c->setAttributes('class', 'some-class');
     $this->assertEquals('p', $c->getNodeName());
     $this->assertEquals('This is a paragraph', $c->getNodeValue());
     $this->assertEquals('some-class', $c->getAttribute('class'));
 }
Example #2
0
<?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;
}
Example #3
0
 /**
  * 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 = '&nbsp;';
                             }
                             $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 = '&nbsp;';
                         }
                         $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] : '&nbsp;';
                     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;
 }
Example #4
0
 /**
  * Constructor
  *
  * Method to instantiate an UPS shipping adapter object
  *
  * @param  string  $accessKey
  * @param  string  $userId
  * @param  string  $password
  * @return \Pop\Shipping\Adapter\Ups
  */
 public function __construct($accessKey, $userId, $password)
 {
     $this->userId = $userId;
     $this->accessRequest = new Dom(Dom::XML);
     $this->rateRequest = new Dom(Dom::XML);
     $access = new Child('AccessRequest');
     $access->setAttributes('xml:lang', 'en-US');
     $key = new Child('AccessLicenseNumber', $accessKey);
     $id = new Child('UserId', $userId);
     $pwd = new Child('Password', $password);
     $access->addChild($key)->addChild($id)->addChild($pwd);
     $this->accessRequest->addChild($access);
 }
Example #5
0
 /**
  * Method to render the form using the template
  *
  * @return void
  */
 protected function renderWithTemplate()
 {
     // Initialize properties and variables.
     $isFile = !(stripos($this->template, '.phtml') === false && stripos($this->template, '.php') === false);
     $template = $this->template;
     $fileContents = $isFile ? file_get_contents($this->template) : null;
     $this->output = null;
     $children = $this->form->getChildren();
     // Loop through the child elements of the form.
     foreach ($children as $child) {
         // Clear the password field from display.
         if ($child->getAttribute('type') == 'password') {
             $child->setValue(null);
             $child->setAttributes('value', null);
         }
         // Get the element name.
         if ($child->getNodeName() == 'fieldset') {
             $chdrn = $child->getChildren();
             $attribs = $chdrn[0]->getAttributes();
         } else {
             $attribs = $child->getAttributes();
         }
         $name = isset($attribs['name']) ? $attribs['name'] : '';
         $name = str_replace('[]', '', $name);
         // Set the element's label, if applicable.
         if (null !== $child->getLabel()) {
             // Format the label name.
             $label = new Child('label', $child->getLabel());
             $label->setAttributes('for', $name);
             $labelAttributes = $child->getLabelAttributes();
             if (null !== $labelAttributes) {
                 foreach ($labelAttributes as $a => $v) {
                     $label->setAttributes($a, $v);
                 }
             } else {
                 if ($child->isRequired()) {
                     $label->setAttributes('class', 'required');
                 }
             }
             // Swap the element's label placeholder with the rendered label element.
             $labelSearch = '[{' . $name . '_label}]';
             $labelReplace = $label->render(true);
             $labelReplace = substr($labelReplace, 0, -1);
             $template = str_replace($labelSearch, $labelReplace, $template);
             ${$name . '_label'} = $labelReplace;
         }
         // Calculate the element's indentation.
         if (null === $fileContents) {
             $childIndent = substr($template, 0, strpos($template, '[{' . $name . '}]'));
             $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1);
         } else {
             $childIndent = substr($fileContents, 0, strpos($fileContents, '$' . $name));
             $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1);
         }
         // Some whitespace clean up
         $length = strlen($childIndent);
         $last = 0;
         $matches = array();
         preg_match_all('/[^\\s]/', $childIndent, $matches, PREG_OFFSET_CAPTURE);
         if (isset($matches[0])) {
             foreach ($matches[0] as $str) {
                 $childIndent = str_replace($str[0], null, $childIndent);
                 $last = $str[1];
             }
         }
         // Final whitespace clean up
         if (null !== $fileContents) {
             $childIndent = substr($childIndent, 0, 0 - abs($length - $last));
         }
         // Set each child element's indentation.
         $childChildren = $child->getChildren();
         $child->removeChildren();
         foreach ($childChildren as $cChild) {
             $cChild->setIndent($childIndent . '    ');
             $child->addChild($cChild);
         }
         // Swap the element's placeholder with the rendered element.
         $elementSearch = '[{' . $name . '}]';
         $elementReplace = $child->render(true, 0, null, $childIndent);
         $elementReplace = substr($elementReplace, 0, -1);
         $elementReplace = str_replace('</select>', $childIndent . '</select>', $elementReplace);
         $elementReplace = str_replace('</fieldset>', $childIndent . '</fieldset>', $elementReplace);
         $template = str_replace($elementSearch, $elementReplace, $template);
         ${$name} = $elementReplace;
     }
     // Set the rendered form content and remove the children.
     if (!$isFile) {
         $this->form->setNodeValue("\n" . $template . "\n" . $this->form->getIndent());
         $this->form->removeChildren();
         $this->output = $this->form->render(true);
     } else {
         $action = $this->action;
         $method = $this->method;
         ob_start();
         include $this->template;
         $this->output = ob_get_clean();
     }
 }
Example #6
0
 /**
  * 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);
 }
Example #7
0
 /**
  * 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.');
             }
         }
     }
 }
Example #8
0
 /**
  * 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;
 }
Example #9
0
 /**
  * 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;
 }
Example #10
0
 /**
  * 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;
     }
 }
Example #11
0
 /**
  * Build social nav
  *
  * @param  string $description
  * @param  string $keywords
  * @return mixed
  */
 public function buildMetaTags($description = null, $keywords = null)
 {
     $metas = null;
     $hasDesc = false;
     $hasKeys = false;
     $config = Table\Config::findById('seo_config');
     if (isset($config->value) && !empty($config->value) && $config->value != '') {
         $cfg = unserialize($config->value);
         if (count($cfg['meta']) > 0) {
             foreach ($cfg['meta'] as $meta) {
                 $content = $meta['content'];
                 if ($meta['name'] == 'description') {
                     $hasDesc = true;
                     if (null !== $description && $description != '') {
                         $content = $description;
                     }
                 }
                 if ($meta['name'] == 'keywords') {
                     $hasKeys = true;
                     if (null !== $keywords && $keywords != '') {
                         $content = $keywords;
                     }
                 }
                 $m = new Child('meta');
                 $m->setAttributes(['name' => $meta['name'], 'content' => htmlentities($content, ENT_QUOTES, 'UTF-8')]);
                 $metas .= '    ' . (string) $m;
             }
             if (!$hasDesc && null !== $description && $description != '') {
                 $m = new Child('meta');
                 $m->setAttributes(['name' => 'description', 'content' => htmlentities($description, ENT_QUOTES, 'UTF-8')]);
                 $metas .= '    ' . (string) $m;
             }
             if (!$hasKeys && null !== $keywords && $keywords != '') {
                 $m = new Child('meta');
                 $m->setAttributes(['name' => 'keywords', 'content' => htmlentities($keywords, ENT_QUOTES, 'UTF-8')]);
                 $metas .= '    ' . (string) $m;
             }
         }
     }
     if (null === $metas) {
         if (null !== $description && $description != '') {
             $m = new Child('meta');
             $m->setAttributes(['name' => 'description', 'content' => htmlentities($description, ENT_QUOTES, 'UTF-8')]);
             $metas .= '    ' . (string) $m;
         }
         if (null !== $keywords && $keywords != '') {
             $m = new Child('meta');
             $m->setAttributes(['name' => 'keywords', 'content' => htmlentities($keywords, ENT_QUOTES, 'UTF-8')]);
             $metas .= '    ' . (string) $m;
         }
     }
     return $metas;
 }
Example #12
0
 /**
  * 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;
 }
Example #13
0
 /**
  * Method to render the form using a basic 1:1 DT/DD layout
  *
  * @return string
  */
 protected function renderWithoutTemplate()
 {
     // Initialize properties.
     $this->output = null;
     $children = $this->getChildren();
     $this->removeChildren();
     $id = null !== $this->getAttribute('id') ? $this->getAttribute('id') . '-field-group' : 'pop-form-field-group';
     // Create DL element.
     $i = 1;
     $dl = new Child('dl', null, null, false, $this->getIndent());
     $dl->setAttribute('id', $id . '-' . $i);
     $dl->setAttribute('class', $id);
     if (isset($this->headers[$i - 1])) {
         $fieldGroupHeader = new Child('h' . $this->headers[$i - 1]['weight'], $this->headers[$i - 1]['header']);
         $fieldGroupHeader->setAttribute('class', $id . '-header');
         $fieldGroupHeader->setIndent($this->getIndent());
         $this->addChild($fieldGroupHeader);
     }
     // 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->addChild($dl);
                 $i++;
                 $dl = new Child('dl', null, null, false, $this->getIndent());
                 $dl->setAttribute('id', $id . '-' . $i);
                 $dl->setAttribute('class', $id);
                 if (isset($this->headers[$i - 1])) {
                     $fieldGroupHeader = new Child('h' . $this->headers[$i - 1]['weight'], $this->headers[$i - 1]['header']);
                     $fieldGroupHeader->setAttribute('class', $id . '-header');
                     $fieldGroupHeader->setIndent($this->getIndent());
                     $this->addChild($fieldGroupHeader);
                 }
             }
         }
         // Clear the password field from display.
         if ($child->getAttribute('type') == 'password') {
             $child->setValue(null);
             $child->setAttribute('value', null);
         }
         // If the element label is set, render the appropriate DT and DD elements.
         if ($child instanceof Element\AbstractElement && null !== $child->getLabel()) {
             // Create the DT and DD elements.
             $dt = new Child('dt', null, null, false, $this->getIndent() . '    ');
             $dd = new Child('dd', null, null, false, $this->getIndent() . '    ');
             // Format the label name.
             $lblName = $child->getNodeName() == 'fieldset' ? '1' : '';
             $label = new Child('label', $child->getLabel(), null, false, $this->getIndent() . '        ');
             $label->setAttribute('for', $name . $lblName);
             $labelAttributes = $child->getLabelAttributes();
             if (count($labelAttributes) > 0) {
                 foreach ($labelAttributes as $a => $v) {
                     if ($a == 'class' && $child->isRequired()) {
                         $v .= ' required';
                     }
                     $label->setAttribute($a, $v);
                 }
             } else {
                 if ($child->isRequired()) {
                     $label->setAttribute('class', 'required');
                 }
             }
             // Add the appropriate children to the appropriate elements.
             $dt->addChild($label);
             $child->setIndent($this->getIndent() . '        ');
             $childChildren = $child->getChildren();
             $child->removeChildren();
             foreach ($childChildren as $cChild) {
                 $cChild->setIndent($this->getIndent() . '            ');
                 $child->addChild($cChild);
             }
             $dd->addChild($child);
             if (null !== $child->getHint()) {
                 $hint = new Child('span', $child->getHint(), null, false, $this->getIndent() . '        ');
                 if (count($child->getHintAttributes()) > 0) {
                     $hint->setAttributes($child->getHintAttributes());
                 }
                 $dd->addChild($hint);
             }
             $dl->addChildren([$dt, $dd]);
             // Else, render only a DD element.
         } else {
             $dd = new Child('dd', null, null, false, $this->getIndent() . '    ');
             $child->setIndent($this->getIndent() . '        ');
             $dd->addChild($child);
             if ($child instanceof Element\AbstractElement && null !== $child->getHint()) {
                 $hint = new Child('span', $child->getHint(), null, false, $this->getIndent() . '        ');
                 if (count($child->getHintAttributes()) > 0) {
                     $hint->setAttributes($child->getHintAttributes());
                 }
                 $dd->addChild($hint);
             }
             $dl->addChild($dd);
         }
     }
     // Add the DL element and its children to the form element.
     $this->addChild($dl);
     return parent::render(true);
 }