Advertisement
kundius

modx_fm

Dec 11th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.09 KB | None | 0 0
  1. public function addition() {
  2.     $content = $this->modx->documentOutput;
  3.     $tags= array ();
  4.     if ($collected = $this->modx->parser->collectElementTags($content, $tags, '[[', ']]')) {
  5.  
  6.         $dom = new DOMDocument();
  7.         // Disable libxml errors occur when parsing tags modx
  8.         libxml_use_internal_errors(true);
  9.         $dom->loadHTML($content);
  10.         $xpath = new DOMXpath($dom);
  11.  
  12.         foreach ($tags as $tag) {
  13.             /* Avoid all processing for comment tags, e.g. [[- comments here]] */
  14.             if (substr($tag[1], 0, 1) === '-') {
  15.                 continue;
  16.             }
  17.  
  18.             $tagParts= xPDO :: escSplit('?', $tag[1], '`', 2);
  19.             $tagName= trim($tagParts[0]);
  20.  
  21.             /* String of parameters. Not yet used. */
  22.             //$tagPropString= null;
  23.             //if (isset ($tagParts[1])) {
  24.             //    $tagPropString= trim($tagParts[1]);
  25.             //}
  26.  
  27.             $token = substr($tagName, 0, 1);
  28.             $tokenOffset = 0;
  29.             if ($token === '!') {
  30.                 $tokenOffset++;
  31.                 $token = substr($tagName, $tokenOffset, 1);
  32.             }
  33.  
  34.             unset($elementPS);
  35.             if (strpos($tagName, '@') !== false) {
  36.                 $split = xPDO :: escSplit('@', $tagName);
  37.                 if ($split && isset($split[1])) {
  38.                     $psName = $split[1];
  39.                     $filters = xPDO :: escSplit(':', $psName);
  40.                     if ($filters && isset($filters[0]) && !empty($filters[0])) {
  41.                         $ps = $this->modx->getObject('modPropertySet', array('name' => $filters[0] ));
  42.                         $elementPS = array(
  43.                             'type' => 'propertyset',
  44.                             'identifier' => $ps->get('id'),
  45.                             'name' => $filters[0]
  46.                         );
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             switch ($token) {
  52.                 case '%':
  53.                 case '~':
  54.                     /* skip links and lexicons */
  55.                     continue 2;
  56.                     break;
  57.                 case '+':
  58.                     if(substr($tagName, 0, 2) == '++') {
  59.                         $tagName = substr($tagName, 2 + $tokenOffset);
  60.                         $tagName = $this->modx->parser->realname($tagName);
  61.                         $element = array(
  62.                             'type' => 'system',
  63.                             'identifier' => $tagName
  64.                         );
  65.                     } else {
  66.                         /* skip placeholders */
  67.                         continue 2;
  68.                     }
  69.                     break;
  70.                 case '$':
  71.                     $tagName = substr($tagName, 1 + $tokenOffset);
  72.                     $tagName = $this->modx->parser->realname($tagName);
  73.                     $element = array(
  74.                         'type' => 'chunk',
  75.                         'identifier' => $this->modx->getObject('modChunk',array('name' => $tagName ))->get('id'),
  76.                         'name' => $tagName
  77.                     );
  78.                     break;
  79.                 case '#': // TODO: fastfield in pdoParser
  80.                     continue 2;
  81.                 case '*':
  82.                     $tagName = substr($tagName, 1 + $tokenOffset);
  83.                     $tagName = $this->modx->parser->realname($tagName);
  84.                     if (is_array($this->modx->resource->_fieldMeta) && in_array($tagName, array_keys($this->modx->resource->_fieldMeta))) {
  85.                         $element = array(
  86.                             'type' => 'field',
  87.                             'identifier' => $tagName,
  88.                             'resource' => $this->modx->resourceIdentifier
  89.                         );
  90.                     } else {
  91.                         $element = array(
  92.                             'type' => 'tv',
  93.                             'identifier' => $this->modx->getObject('modTemplateVar', array('name'=>$tagName))->get('id'),
  94.                             'resource' => $this->modx->resourceIdentifier
  95.                         );
  96.                     }
  97.                     break;
  98.                 default:
  99.                     if($this->config['snippets_editable']) {
  100.                         $tagName = substr($tagName, $tokenOffset);
  101.                         $tagName = $this->modx->parser->realname($tagName);
  102.  
  103.                         if($snippet = $this->modx->getObject('modSnippet', array('name' => $tagName))) {
  104.                             $element = array(
  105.                                 'type' => 'snippet',
  106.                                 'identifier' => $snippet->get('id'),
  107.                                 'name' => $tagName
  108.                             );
  109.                         }
  110.                     }
  111.             }
  112.  
  113.             if(isset($element)) {
  114.                 $key = implode('-', $element);
  115.                 $attr = array(
  116.                     $key => $element
  117.                 );
  118.  
  119.                 if(isset($elementPS)) {
  120.                     $attr['propertyset-'.$elementPS['identifier']] = $elementPS;
  121.                 }
  122.  
  123.                 /** @var DOMNodeList $NodeList */
  124.                 $NodeList = $xpath->query('/html/body//text()[contains(.,"' . $tag[0] . '")]/..|/html/body//attribute::*[contains(.,"' . $tag[0] . '")]/..');
  125.  
  126.                 /** @var DOMNode $Node */
  127.                 foreach ($NodeList as $Node) {
  128.                     if($Node->hasAttribute('frontmanager')) {
  129.                         $attr = array_merge(
  130.                             $this->modx->fromJSON($Node->getAttribute('frontmanager')),
  131.                             $attr
  132.                         );
  133.                     }
  134.  
  135.                     $Node->setAttribute(
  136.                         "frontmanager",
  137.                         $this->modx->toJSON($attr)
  138.                     );
  139.                 }
  140.  
  141.                 $content = $dom->saveHTML();
  142.             }
  143.         }
  144.  
  145.         /* fix percent-encoding for "[" and "]" */
  146.         $content = str_replace("%5D", "]",str_replace("%5B", "[", $content));
  147.  
  148.         $this->modx->documentOutput = $content;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement