PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/wayfinder/wayfinder.inc.php

https://github.com/HenrikNielsen/evolution
PHP | 660 lines | 520 code | 42 blank | 98 comment | 178 complexity | 2d108a4eac67ac26ce2008649b3f2416 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-1.0
  1. <?php
  2. /*
  3. ::::::::::::::::::::::::::::::::::::::::
  4. Snippet name: Wayfinder
  5. Short Desc: builds site navigation
  6. Version: 2.0
  7. Authors:
  8. Kyle Jaebker (muddydogpaws.com)
  9. Ryan Thrash (vertexworks.com)
  10. Date: February 27, 2006
  11. ::::::::::::::::::::::::::::::::::::::::
  12. */
  13. class Wayfinder {
  14. var $_config;
  15. var $_templates;
  16. var $_css;
  17. var $docs = array();
  18. var $parentTree = array();
  19. var $hasChildren = array();
  20. var $placeHolders = array(
  21. 'rowLevel' => array('[+wf.wrapper+]','[+wf.classes+]','[+wf.classnames+]','[+wf.link+]','[+wf.title+]','[+wf.linktext+]','[+wf.id+]','[+wf.alias+]','[+wf.attributes+]','[+wf.docid+]','[+wf.introtext+]','[+wf.description+]','[+wf.subitemcount+]'),
  22. 'wrapperLevel' => array('[+wf.wrapper+]','[+wf.classes+]','[+wf.classnames+]'),
  23. 'tvs' => array(),
  24. );
  25. var $tvList = array();
  26. var $debugInfo = array();
  27. function run() {
  28. global $modx;
  29. //setup here checking array
  30. $this->parentTree = $modx->getParentIds($modx->documentIdentifier);
  31. $this->parentTree[] = $modx->documentIdentifier;
  32. if ($this->_config['debug']) {
  33. $this->addDebugInfo("settings","Settings","Settings","Settings used to create this menu.",$this->_config);
  34. $this->addDebugInfo("settings","CSS","CSS Settings","Available CSS options.",$this->_css);
  35. }
  36. //Load the templates
  37. $this->checkTemplates();
  38. //Register any scripts
  39. if ($this->_config['cssTpl'] || $this->_config['jsTpl']) {
  40. $this->regJsCss();
  41. }
  42. //Get all of the documents
  43. $this->docs = $this->getData();
  44. if (!empty($this->docs)) {
  45. //Sort documents by level for proper wrapper substitution
  46. ksort($this->docs);
  47. //build the menu
  48. return $this->buildMenu();
  49. } else {
  50. $noneReturn = $this->_config['debug'] ? '<p style="color:red">No documents found for menu.</p>' : '';
  51. return $noneReturn;
  52. }
  53. }
  54. function buildMenu() {
  55. global $modx;
  56. //Loop through all of the menu levels
  57. foreach ($this->docs as $level => $subDocs) {
  58. //Loop through each document group (grouped by parent doc)
  59. foreach ($subDocs as $parentId => $docs) {
  60. //only process document group, if starting at root, hidesubmenus is off, or is in current parenttree
  61. if (!$this->_config['hideSubMenus'] || $this->isHere($parentId) || $level <= 1) {
  62. //Build the output for the group of documents
  63. $menuPart = $this->buildSubMenu($docs,$level);
  64. //If we are at the top of the menu start the output, otherwise replace the wrapper with the submenu
  65. if (($level == 1 && (!$this->_config['displayStart'] || $this->_config['id'] == 0)) || ($level == 0 && $this->_config['displayStart'])) {
  66. $output = $menuPart;
  67. } else {
  68. $output = str_replace("[+wf.wrapper.{$parentId}+]",$menuPart,$output);
  69. }
  70. }
  71. }
  72. }
  73. //Return the final Menu
  74. return $output;
  75. }
  76. function buildSubMenu($menuDocs,$level) {
  77. global $modx;
  78. $subMenuOutput = '';
  79. $firstItem = 1;
  80. $counter = 1;
  81. $numSubItems = count($menuDocs);
  82. //Loop through each document to render output
  83. foreach ($menuDocs as $docId => $docInfo) {
  84. $docInfo['level'] = $level;
  85. $docInfo['first'] = $firstItem;
  86. $firstItem = 0;
  87. //Determine if last item in group
  88. if ($counter == ($numSubItems) && $numSubItems > 1) {
  89. $docInfo['last'] = 1;
  90. } else {
  91. $docInfo['last'] = 0;
  92. }
  93. //Determine if document has children
  94. $docInfo['hasChildren'] = in_array($docInfo['id'],$this->hasChildren) ? 1 : 0;
  95. $numChildren = $docInfo['hasChildren'] ? count($this->docs[$level+1][$docInfo['id']]) : 0;
  96. //Render the row output
  97. $subMenuOutput .= $this->renderRow($docInfo,$numChildren);
  98. //Update counter for last check
  99. $counter++;
  100. }
  101. if ($level > 0) {
  102. //Determine which wrapper template to use
  103. if ($this->_templates['innerTpl'] && $level > 1) {
  104. $useChunk = $this->_templates['innerTpl'];
  105. $usedTemplate = 'innerTpl';
  106. } else {
  107. $useChunk = $this->_templates['outerTpl'];
  108. $usedTemplate = 'outerTpl';
  109. }
  110. //Determine wrapper class
  111. if ($level > 1) {
  112. $wrapperClass = 'innercls';
  113. } else {
  114. $wrapperClass = 'outercls';
  115. }
  116. //Get the class names for the wrapper
  117. $classNames = $this->setItemClass($wrapperClass);
  118. if ($classNames) $useClass = ' class="' . $classNames . '"';
  119. $phArray = array($subMenuOutput,$useClass,$classNames);
  120. //Process the wrapper
  121. $subMenuOutput = str_replace($this->placeHolders['wrapperLevel'],$phArray,$useChunk);
  122. //Debug
  123. if ($this->_config['debug']) {
  124. $debugParent = $docInfo['parent'];
  125. $debugDocInfo = array();
  126. $debugDocInfo['template'] = $usedTemplate;
  127. foreach ($this->placeHolders['wrapperLevel'] as $n => $v) {
  128. if ($v !== '[+wf.wrapper+]')
  129. $debugDocInfo[$v] = $phArray[$n];
  130. }
  131. $this->addDebugInfo("wrapper","{$debugParent}","Wrapper for items with parent {$debugParent}.","These fields were used when processing the wrapper for the following documents.",$debugDocInfo);
  132. }
  133. }
  134. //Return the submenu
  135. return $subMenuOutput;
  136. }
  137. //render each rows output
  138. function renderRow(&$resource,$numChildren) {
  139. global $modx;
  140. $output = '';
  141. //Determine which template to use
  142. if ($this->_config['displayStart'] && $resource['level'] == 0) {
  143. $usedTemplate = 'startItemTpl';
  144. } elseif ($resource['id'] == $modx->documentObject['id'] && $resource['isfolder'] && $this->_templates['parentRowHereTpl'] && ($resource['level'] < $this->_config['level'] || $this->_config['level'] == 0) && $numChildren) {
  145. $usedTemplate = 'parentRowHereTpl';
  146. } elseif ($resource['id'] == $modx->documentObject['id'] && $this->_templates['innerHereTpl'] && $resource['level'] > 1) {
  147. $usedTemplate = 'innerHereTpl';
  148. } elseif ($resource['id'] == $modx->documentObject['id'] && $this->_templates['hereTpl']) {
  149. $usedTemplate = 'hereTpl';
  150. } elseif ($resource['isfolder'] && $this->_templates['activeParentRowTpl'] && ($resource['level'] < $this->_config['level'] || $this->_config['level'] == 0) && $this->isHere($resource['id'])) {
  151. $usedTemplate = 'activeParentRowTpl';
  152. } elseif ($resource['isfolder'] && ($resource['template']=="0" || is_numeric(strpos($resource['link_attributes'],'rel="category"'))) && $this->_templates['categoryFoldersTpl'] && ($resource['level'] < $this->_config['level'] || $this->_config['level'] == 0)) {
  153. $usedTemplate = 'categoryFoldersTpl';
  154. } elseif ($resource['isfolder'] && $this->_templates['parentRowTpl'] && ($resource['level'] < $this->_config['level'] || $this->_config['level'] == 0) && $numChildren) {
  155. $usedTemplate = 'parentRowTpl';
  156. } elseif ($resource['level'] > 1 && $this->_templates['innerRowTpl']) {
  157. $usedTemplate = 'innerRowTpl';
  158. } else {
  159. $usedTemplate = 'rowTpl';
  160. }
  161. //Get the template
  162. $useChunk = $this->_templates[$usedTemplate];
  163. //Setup the new wrapper name and get the class names
  164. $useSub = $resource['hasChildren'] ? "[+wf.wrapper.{$resource['id']}+]" : "";
  165. $classNames = $this->setItemClass('rowcls',$resource['id'],$resource['first'],$resource['last'],$resource['level'],$resource['isfolder'],$resource['type']);
  166. if ($classNames) $useClass = ' class="' . $classNames . '"';
  167. //Setup the row id if a prefix is specified
  168. if ($this->_config['rowIdPrefix']) {
  169. $useId = ' id="' . $this->_config['rowIdPrefix'] . $resource['id'] . '"';
  170. } else {
  171. $useId = '';
  172. }
  173. //Load row values into placholder array
  174. $phArray = array($useSub,$useClass,$classNames,$resource['link'],$resource['title'],$resource['linktext'],$useId,$resource['alias'],$resource['link_attributes'],$resource['id'],$resource['introtext'],$resource['description'],$numChildren);
  175. //If tvs are used add them to the placeholder array
  176. if (!empty($this->tvList)) {
  177. $usePlaceholders = array_merge($this->placeHolders['rowLevel'],$this->placeHolders['tvs']);
  178. foreach ($this->tvList as $tvName) {
  179. $phArray[] = $resource[$tvName];
  180. }
  181. } else {
  182. $usePlaceholders = $this->placeHolders['rowLevel'];
  183. }
  184. //Debug
  185. if ($this->_config['debug']) {
  186. $debugDocInfo = array();
  187. $debugDocInfo['template'] = $usedTemplate;
  188. foreach ($usePlaceholders as $n => $v) {
  189. $debugDocInfo[$v] = $phArray[$n];
  190. }
  191. $this->addDebugInfo("row","{$resource['parent']}:{$resource['id']}","Doc: #{$resource['id']}","The following fields were used when processing this document.",$debugDocInfo);
  192. $this->addDebugInfo("rowdata","{$resource['parent']}:{$resource['id']}","Doc: #{$resource['id']}","The following fields were retrieved from the database for this document.",$resource);
  193. }
  194. //Process the row
  195. $output .= str_replace($usePlaceholders,$phArray,$useChunk);
  196. //Return the row
  197. return $output . $this->_config['nl'];
  198. }
  199. //determine style class for current item being processed
  200. function setItemClass($classType, $docId = 0, $first = 0, $last = 0, $level = 0, $isFolder = 0, $type = 'document') {
  201. global $modx;
  202. $returnClass = '';
  203. $hasClass = 0;
  204. if ($classType === 'outercls' && !empty($this->_css['outer'])) {
  205. //Set outer class if specified
  206. $returnClass .= $this->_css['outer'];
  207. $hasClass = 1;
  208. } elseif ($classType === 'innercls' && !empty($this->_css['inner'])) {
  209. //Set inner class if specified
  210. $returnClass .= $this->_css['inner'];
  211. $hasClass = 1;
  212. } elseif ($classType === 'rowcls') {
  213. //Set row class if specified
  214. if (!empty($this->_css['row'])) {
  215. $returnClass .= $this->_css['row'];
  216. $hasClass = 1;
  217. }
  218. //Set first class if specified
  219. if ($first && !empty($this->_css['first'])) {
  220. $returnClass .= $hasClass ? ' ' . $this->_css['first'] : $this->_css['first'];
  221. $hasClass = 1;
  222. }
  223. //Set last class if specified
  224. if ($last && !empty($this->_css['last'])) {
  225. $returnClass .= $hasClass ? ' ' . $this->_css['last'] : $this->_css['last'];
  226. $hasClass = 1;
  227. }
  228. //Set level class if specified
  229. if (!empty($this->_css['level'])) {
  230. $returnClass .= $hasClass ? ' ' . $this->_css['level'] . $level : $this->_css['level'] . $level;
  231. $hasClass = 1;
  232. }
  233. //Set parentFolder class if specified
  234. if ($isFolder && !empty($this->_css['parent']) && ($level < $this->_config['level'] || $this->_config['level'] == 0)) {
  235. $returnClass .= $hasClass ? ' ' . $this->_css['parent'] : $this->_css['parent'];
  236. $hasClass = 1;
  237. }
  238. //Set here class if specified
  239. if (!empty($this->_css['here']) && $this->isHere($docId)) {
  240. $returnClass .= $hasClass ? ' ' . $this->_css['here'] : $this->_css['here'];
  241. $hasClass = 1;
  242. }
  243. //Set self class if specified
  244. if (!empty($this->_css['self']) && $docId == $modx->documentIdentifier) {
  245. $returnClass .= $hasClass ? ' ' . $this->_css['self'] : $this->_css['self'];
  246. $hasClass = 1;
  247. }
  248. //Set class for weblink
  249. if (!empty($this->_css['weblink']) && $type == 'reference') {
  250. $returnClass .= $hasClass ? ' ' . $this->_css['weblink'] : $this->_css['weblink'];
  251. $hasClass = 1;
  252. }
  253. }
  254. return $returnClass;
  255. }
  256. //determine "you are here"
  257. function isHere($did) {
  258. return in_array($did,$this->parentTree);
  259. }
  260. //Add the specified css & javascript chunks to the page
  261. function regJsCss() {
  262. global $modx;
  263. //Debug
  264. if ($this->_config['debug']) {
  265. $jsCssDebug = array('js' => 'None Specified.', 'css' => 'None Specified.');
  266. }
  267. //Check and load the CSS
  268. if ($this->_config['cssTpl']) {
  269. $cssChunk = $this->fetch($this->_config['cssTpl']);
  270. if ($cssChunk) {
  271. $modx->regClientCSS($cssChunk);
  272. if ($this->_config['debug']) {$jsCssDebug['css'] = "The CSS in {$this->_config['cssTpl']} was registered.";}
  273. } else {
  274. if ($this->_config['debug']) {$jsCssDebug['css'] = "The CSS in {$this->_config['cssTpl']} was not found.";}
  275. }
  276. }
  277. //Check and load the Javascript
  278. if ($this->_config['jsTpl']) {
  279. $jsChunk = $this->fetch($this->_config['jsTpl']);
  280. if ($jsChunk) {
  281. $modx->regClientStartupScript($jsChunk);
  282. if ($this->_config['debug']) {$jsCssDebug['js'] = "The Javascript in {$this->_config['jsTpl']} was registered.";}
  283. } else {
  284. if ($this->_config['debug']) {$jsCssDebug['js'] = "The Javascript in {$this->_config['jsTpl']} was not found.";}
  285. }
  286. }
  287. //Debug
  288. if ($this->_config['debug']) {$this->addDebugInfo("settings","JSCSS","JS/CSS Includes","Results of CSS & Javascript includes.",$jsCssDebug);}
  289. }
  290. //Get all of the documents from the database
  291. function getData() {
  292. global $modx;
  293. $ids = array();
  294. $ids = $modx->getChildIds($this->_config['id'],$this->_config['level']);
  295. //Get all of the ids for processing
  296. if ($this->_config['displayStart'] && $this->_config['id'] !== 0) {
  297. $ids[] = $this->_config['id'];
  298. }
  299. if (!empty($ids)) {
  300. //Setup the fields for the query
  301. $fields = "sc.id, sc.menutitle, sc.pagetitle, sc.introtext, sc.menuindex, sc.published, sc.hidemenu, sc.parent, sc.isfolder, sc.description, sc.alias, sc.longtitle, sc.type,if(sc.type='reference',sc.content,'') as content, sc.template, sc.link_attributes";
  302. //Get the table names
  303. $tblsc = $modx->getFullTableName("site_content");
  304. $tbldg = $modx->getFullTableName("document_groups");
  305. //Add the ignore hidden option to the where clause
  306. if ($this->_config['ignoreHidden']) {
  307. $menuWhere = '';
  308. } else {
  309. $menuWhere = ' AND sc.hidemenu=0';
  310. }
  311. //add the include docs to the where clause
  312. if ($this->_config['includeDocs']) {
  313. $menuWhere .= " AND sc.id IN ({$this->_config['includeDocs']})";
  314. }
  315. //add the exclude docs to the where clause
  316. if ($this->_config['excludeDocs']) {
  317. $menuWhere .= " AND (sc.id NOT IN ({$this->_config['excludeDocs']}))";
  318. }
  319. //add the limit to the query
  320. if ($this->_config['limit']) {
  321. $sqlLimit = " LIMIT 0, {$this->_config['limit']}";
  322. } else {
  323. $sqlLimit = '';
  324. }
  325. //Determine sorting
  326. if (strtolower($this->_config['sortBy']) == 'random') {
  327. $sort = 'rand()';
  328. $dir = '';
  329. } else {
  330. // modify field names to use sc. table reference
  331. $sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$this->_config['sortBy'])));
  332. }
  333. // get document groups for current user
  334. if($docgrp = $modx->getUserDocGroups()) $docgrp = implode(",",$docgrp);
  335. // build query
  336. $access = ($modx->isFrontend() ? "sc.privateweb=0" : "1='{$_SESSION['mgrRole']}' OR sc.privatemgr=0").(!$docgrp ? "" : " OR dg.document_group IN ({$docgrp})");
  337. $sql = "SELECT DISTINCT {$fields} FROM {$tblsc} sc LEFT JOIN {$tbldg} dg ON dg.document = sc.id WHERE sc.published=1 AND sc.deleted=0 AND ({$access}){$menuWhere} AND sc.id IN (".implode(',',$ids).") GROUP BY sc.id ORDER BY {$sort} {$this->_config['sortOrder']} {$sqlLimit};";
  338. //run the query
  339. $result = $modx->dbQuery($sql);
  340. $resourceArray = array();
  341. $numResults = @$modx->recordCount($result);
  342. $level = 1;
  343. $prevParent = -1;
  344. //Setup startlevel for determining each items level
  345. if ($this->_config['id'] == 0) {
  346. $startLevel = 0;
  347. } else {
  348. $startLevel = count($modx->getParentIds($this->_config['id']));
  349. $startLevel = $startLevel ? $startLevel+1 : 1;
  350. }
  351. $resultIds = array();
  352. //loop through the results
  353. for($i=0;$i<$numResults;$i++) {
  354. $tempDocInfo = $modx->fetchRow($result);
  355. $resultIds[] = $tempDocInfo['id'];
  356. //Create the link
  357. $linkScheme = $this->_config['fullLink'] ? 'full' : '';
  358. if ($this->_config['useWeblinkUrl'] !== 'FALSE' && $tempDocInfo['type'] == 'reference') {
  359. if (is_numeric($tempDocInfo['content'])) {
  360. $tempDocInfo['link'] = $modx->makeUrl(intval($tempDocInfo['content']),'','',$linkScheme);
  361. } else {
  362. $tempDocInfo['link'] = $tempDocInfo['content'];
  363. }
  364. } elseif ($tempDocInfo['id'] == $modx->config['site_start']) {
  365. $tempDocInfo['link'] = $modx->config['site_url'];
  366. } else {
  367. $tempDocInfo['link'] = $modx->makeUrl($tempDocInfo['id'],'','',$linkScheme);
  368. }
  369. //determine the level, if parent has changed
  370. if ($prevParent !== $tempDocInfo['parent']) {
  371. $level = count($modx->getParentIds($tempDocInfo['id'])) + 1 - $startLevel;
  372. }
  373. //add parent to hasChildren array for later processing
  374. if (($level > 1 || $this->_config['displayStart']) && !in_array($tempDocInfo['parent'],$this->hasChildren)) {
  375. $this->hasChildren[] = $tempDocInfo['parent'];
  376. }
  377. //set the level
  378. $tempDocInfo['level'] = $level;
  379. $prevParent = $tempDocInfo['parent'];
  380. //determine other output options
  381. $useTextField = (empty($tempDocInfo[$this->_config['textOfLinks']])) ? 'pagetitle' : $this->_config['textOfLinks'];
  382. $tempDocInfo['linktext'] = $tempDocInfo[$useTextField];
  383. $tempDocInfo['title'] = $tempDocInfo[$this->_config['titleOfLinks']];
  384. //If tvs were specified keep array flat otherwise array becomes level->parent->doc
  385. if (!empty($this->tvList)) {
  386. $tempResults[] = $tempDocInfo;
  387. } else {
  388. $resourceArray[$tempDocInfo['level']][$tempDocInfo['parent']][] = $tempDocInfo;
  389. }
  390. }
  391. //Process the tvs
  392. if (!empty($this->tvList) && !empty($resultIds)) {
  393. $tvValues = array();
  394. //loop through all tvs and get their values for each document
  395. foreach ($this->tvList as $tvName) {
  396. $tvValues = array_merge_recursive($this->appendTV($tvName,$resultIds),$tvValues);
  397. }
  398. //loop through the document array and add the tvar values to each document
  399. foreach ($tempResults as $tempDocInfo) {
  400. if (array_key_exists("#{$tempDocInfo['id']}",$tvValues)) {
  401. foreach ($tvValues["#{$tempDocInfo['id']}"] as $tvName => $tvValue) {
  402. $tempDocInfo[$tvName] = $tvValue;
  403. }
  404. }
  405. $resourceArray[$tempDocInfo['level']][$tempDocInfo['parent']][] = $tempDocInfo;
  406. }
  407. }
  408. }
  409. //return final docs
  410. return $resourceArray;
  411. }
  412. // ---------------------------------------------------
  413. // Function: appendTV taken from Ditto (thanks Mark)
  414. // Apeend a TV to the documents array
  415. // ---------------------------------------------------
  416. function appendTV($tvname,$docIDs){
  417. global $modx;
  418. $baspath= $modx->config["base_path"] . "manager/includes";
  419. include_once $baspath . "/tmplvars.format.inc.php";
  420. include_once $baspath . "/tmplvars.commands.inc.php";
  421. $tb1 = $modx->getFullTableName("site_tmplvar_contentvalues");
  422. $tb2 = $modx->getFullTableName("site_tmplvars");
  423. $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
  424. $query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
  425. $query .= " WHERE stv.name='".$tvname."' AND stc.contentid IN (".implode($docIDs,",").") ORDER BY stc.contentid ASC;";
  426. $rs = $modx->db->query($query);
  427. $tot = $modx->db->getRecordCount($rs);
  428. $resourceArray = array();
  429. for($i=0;$i<$tot;$i++) {
  430. $row = @$modx->fetchRow($rs);
  431. $resourceArray["#{$row['contentid']}"][$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);
  432. }
  433. if ($tot != count($docIDs)) {
  434. $query = "SELECT name,type,display,display_params,default_text";
  435. $query .= " FROM $tb2";
  436. $query .= " WHERE name='".$tvname."' LIMIT 1";
  437. $rs = $modx->db->query($query);
  438. $row = @$modx->fetchRow($rs);
  439. $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type']);
  440. foreach ($docIDs as $id) {
  441. if (!isset($resourceArray["#{$id}"])) {
  442. $resourceArray["#{$id}"][$tvname] = $defaultOutput;
  443. }
  444. }
  445. }
  446. return $resourceArray;
  447. }
  448. // ---------------------------------------------------
  449. // Get a list of all available TVs
  450. // ---------------------------------------------------
  451. function getTVList() {
  452. global $modx;
  453. $table = $modx->getFullTableName("site_tmplvars");
  454. $tvs = $modx->db->select("name", $table);
  455. // TODO: make it so that it only pulls those that apply to the current template
  456. $dbfields = array();
  457. while ($dbfield = $modx->db->getRow($tvs))
  458. $dbfields[] = $dbfield['name'];
  459. return $dbfields;
  460. }
  461. //debugging to check for valid chunks
  462. function checkTemplates() {
  463. global $modx;
  464. $nonWayfinderFields = array();
  465. foreach ($this->_templates as $n => $v) {
  466. $templateCheck = $this->fetch($v);
  467. if (empty($v) || !$templateCheck) {
  468. if ($n === 'outerTpl') {
  469. $this->_templates[$n] = '<ul[+wf.classes+]>[+wf.wrapper+]</ul>';
  470. } elseif ($n === 'rowTpl') {
  471. $this->_templates[$n] = '<li[+wf.id+][+wf.classes+]><a href="[+wf.link+]" title="[+wf.title+]" [+wf.attributes+]>[+wf.linktext+]</a>[+wf.wrapper+]</li>';
  472. } elseif ($n === 'startItemTpl') {
  473. $this->_templates[$n] = '<h2[+wf.id+][+wf.classes+]>[+wf.linktext+]</h2>[+wf.wrapper+]';
  474. } else {
  475. $this->_templates[$n] = FALSE;
  476. }
  477. if ($this->_config['debug']) { $this->addDebugInfo('template',$n,$n,"No template found, using default.",array($n => $this->_templates[$n])); }
  478. } else {
  479. $this->_templates[$n] = $templateCheck;
  480. $check = $this->findTemplateVars($templateCheck);
  481. if (is_array($check)) {
  482. $nonWayfinderFields = array_merge($check, $nonWayfinderFields);
  483. }
  484. if ($this->_config['debug']) { $this->addDebugInfo('template',$n,$n,"Template Found.",array($n => $this->_templates[$n])); }
  485. }
  486. }
  487. if (!empty($nonWayfinderFields)) {
  488. $nonWayfinderFields = array_unique($nonWayfinderFields);
  489. $allTvars = $this->getTVList();
  490. foreach ($nonWayfinderFields as $field) {
  491. if (in_array($field, $allTvars)) {
  492. $this->placeHolders['tvs'][] = "[+{$field}+]";
  493. $this->tvList[] = $field;
  494. }
  495. }
  496. if ($this->_config['debug']) { $this->addDebugInfo('tvars','tvs','Template Variables',"The following template variables were found in your templates.",$this->tvList); }
  497. }
  498. }
  499. function fetch($tpl){
  500. // based on version by Doze at http://modxcms.com/forums/index.php/topic,5344.msg41096.html#msg41096
  501. global $modx;
  502. $template = "";
  503. if ($modx->getChunk($tpl) != "") {
  504. $template = $modx->getChunk($tpl);
  505. } else if(substr($tpl, 0, 6) == "@FILE:") {
  506. $template = $this->get_file_contents(substr($tpl, 6));
  507. } else if(substr($tpl, 0, 6) == "@CODE:") {
  508. $template = substr($tpl, 6);
  509. } else {
  510. $template = FALSE;
  511. }
  512. return $template;
  513. }
  514. function get_file_contents($filename) {
  515. // Function written at http://www.nutt.net/2006/07/08/file_get_contents-function-for-php-4/#more-210
  516. // Returns the contents of file name passed
  517. if (!function_exists('file_get_contents')) {
  518. $fhandle = fopen($filename, "r");
  519. $fcontents = fread($fhandle, filesize($filename));
  520. fclose($fhandle);
  521. } else {
  522. $fcontents = file_get_contents($filename);
  523. }
  524. return $fcontents;
  525. }
  526. function findTemplateVars($tpl) {
  527. preg_match_all('~\[\+(.*?)\+\]~', $tpl, $matches);
  528. $cnt = count($matches[1]);
  529. $tvnames = array ();
  530. for ($i = 0; $i < $cnt; $i++) {
  531. if (strpos($matches[1][$i], "wf.") === FALSE) {
  532. $tvnames[] = $matches[1][$i];
  533. }
  534. }
  535. if (count($tvnames) >= 1) {
  536. return array_unique($tvnames);
  537. } else {
  538. return false;
  539. }
  540. }
  541. function addDebugInfo($group,$groupkey,$header,$message,$info) {
  542. $infoString = '<table border="1" cellpadding="3px">';
  543. $numInfo = count($info);
  544. $count = 0;
  545. foreach ($info as $key => $value) {
  546. $key = $this->modxPrep($key);
  547. if ($value === TRUE || $value === FALSE) {
  548. $value = $value ? 'TRUE' : 'FALSE';
  549. } else {
  550. $value = $this->modxPrep($value);
  551. }
  552. if ($count == 2) { $infoString .= '</tr>'; $count = 0; }
  553. if ($count == 0) { $infoString .= '<tr>'; }
  554. $value = empty($value) ? '&nbsp;' : $value;
  555. $infoString .= "<td><strong>{$key}</strong></td><td>{$value}</td>";
  556. $count++;
  557. }
  558. $infoString .= '</tr></table>';
  559. $this->debugInfo[$group][$groupkey] = array(
  560. 'header' => $this->modxPrep($header),
  561. 'message' => $this->modxPrep($message),
  562. 'info' => $infoString,
  563. );
  564. }
  565. function renderDebugOutput() {
  566. $output = '<table border="1" cellpadding="3px" width="100%">';
  567. foreach ($this->debugInfo as $group => $item) {
  568. switch ($group) {
  569. case 'template':
  570. $output .= "<tr><th style=\"background:#C3D9FF;font-size:200%;\">Template Processing</th></tr>";
  571. foreach ($item as $parentId => $info) {
  572. $output .= "
  573. <tr style=\"background:#336699;color:#fff;\"><th>{$info['header']} - <span style=\"font-weight:normal;\">{$info['message']}</span></th></tr>
  574. <tr><td>{$info['info']}</td></tr>";
  575. }
  576. break;
  577. case 'wrapper':
  578. $output .= "<tr><th style=\"background:#C3D9FF;font-size:200%;\">Document Processing</th></tr>";
  579. foreach ($item as $parentId => $info) {
  580. $output .= "<tr><table border=\"1\" cellpadding=\"3px\" style=\"margin-bottom: 10px;\" width=\"100%\">
  581. <tr style=\"background:#336699;color:#fff;\"><th>{$info['header']} - <span style=\"font-weight:normal;\">{$info['message']}</span></th></tr>
  582. <tr><td>{$info['info']}</td></tr>
  583. <tr style=\"background:#336699;color:#fff;\"><th>Documents included in this wrapper:</th></tr>";
  584. foreach ($this->debugInfo['row'] as $key => $value) {
  585. $keyParts = explode(':',$key);
  586. if ($parentId == $keyParts[0]) {
  587. $output .= "<tr style=\"background:#eee;\"><th>{$value['header']}</th></tr>
  588. <tr><td><div style=\"float:left;margin-right:1%;\">{$value['message']}<br />{$value['info']}</div><div style=\"float:left;\">{$this->debugInfo['rowdata'][$key]['message']}<br />{$this->debugInfo['rowdata'][$key]['info']}</div></td></tr>";
  589. }
  590. }
  591. $output .= '</table></tr>';
  592. }
  593. break;
  594. case 'settings':
  595. $output .= "<tr><th style=\"background:#C3D9FF;font-size:200%;\">Settings</th></tr>";
  596. foreach ($item as $parentId => $info) {
  597. $output .= "
  598. <tr style=\"background:#336699;color:#fff;\"><th>{$info['header']} - <span style=\"font-weight:normal;\">{$info['message']}</span></th></tr>
  599. <tr><td>{$info['info']}</td></tr>";
  600. }
  601. break;
  602. default:
  603. break;
  604. }
  605. }
  606. $output .= '</table>';
  607. return $output;
  608. }
  609. function modxPrep($value) {
  610. $value = (strpos($value,"<") !== FALSE) ? htmlentities($value) : $value;
  611. $value = str_replace("[","&#091;",$value);
  612. $value = str_replace("]","&#093;",$value);
  613. $value = str_replace("{","&#123;",$value);
  614. $value = str_replace("}","&#125;",$value);
  615. return $value;
  616. }
  617. }
  618. ?>