PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/snippets/ditto/classes/ditto.class.inc.php

https://github.com/dreeman/modx106
PHP | 1179 lines | 873 code | 128 blank | 178 comment | 236 complexity | 88c1796dc92778a0499bb5bba30681fc MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * Title: Main Class
  4. * Purpose:
  5. * The Ditto class contains all functions relating to Ditto's
  6. * functionality and any supporting functions they need
  7. */
  8. class ditto {
  9. var $template,$resource,$format,$debug,$advSort,$sqlOrderBy,$customReset,$fields,$constantFields,$prefetch,$sortOrder,$customPlaceholdersMap;
  10. function ditto($dittoID,$format,$language,$debug) {
  11. $this->format = $format;
  12. $GLOBALS["ditto_lang"] = $language;
  13. $this->prefetch = false;
  14. $this->advSort = false;
  15. $this->sqlOrderBy = array();
  16. $this->customReset = array();
  17. $this->constantFields[] = array("db","tv");
  18. $this->constantFields["db"] = array("id","type","contentType","pagetitle","longtitle","description","alias","link_attributes","published","pub_date","unpub_date","parent","isfolder","introtext","content","richtext","template","menuindex","searchable","cacheable","createdby","createdon","editedby","editedon","deleted","deletedon","deletedby","publishedon","publishedby","menutitle","donthit","haskeywords","hasmetatags","privateweb","privatemgr","content_dispo","hidemenu");
  19. $this->constantFields["tv"] = $this->getTVList();
  20. $GLOBALS["ditto_constantFields"] = $this->constantFields;
  21. $this->fields = array("display"=>array(),"backend"=>array("tv"=>array(),"db"=>array("id", "published")));
  22. $this->sortOrder = false;
  23. $this->customPlaceholdersMap = array();
  24. $this->template = new template();
  25. if (!is_null($debug)) {$this->debug = new debug($debug);}
  26. }
  27. // ---------------------------------------------------
  28. // Function: getTVList
  29. // Get a list of all available TVs
  30. // ---------------------------------------------------
  31. function getTVList() {
  32. global $modx;
  33. $table = $modx->getFullTableName("site_tmplvars");
  34. $tvs = $modx->db->select("name", $table);
  35. // TODO: make it so that it only pulls those that apply to the current template
  36. $dbfields = array();
  37. while ($dbfield = $modx->db->getRow($tvs))
  38. $dbfields[] = $dbfield['name'];
  39. return $dbfields;
  40. }
  41. // ---------------------------------------------------
  42. // Function: addField
  43. // Add a field to the internal field detection system
  44. // ---------------------------------------------------
  45. function addField($name,$location,$type=false) {
  46. if ($type === false) {
  47. $type = $this->getDocVarType($name);
  48. }
  49. if ($type == "tv:prefix") {
  50. $type = "tv";
  51. $name = substr($name, 2);
  52. }
  53. if ($location == "*") {
  54. $this->fields["backend"][$type][] = $name;
  55. $this->fields["display"][$type][] = $name;
  56. } else {
  57. $this->fields[$location][$type][] = $name;
  58. }
  59. }
  60. // ---------------------------------------------------
  61. // Function: addFields
  62. // Add a field to the internal field detection system
  63. // from an array or delimited string
  64. // ---------------------------------------------------
  65. function addFields($fields,$location='*',$delimiter=',',$callback=false) {
  66. if (empty($fields)) return false;
  67. if (!is_array($fields)) {
  68. if (strpos($fields,$delimiter) !== false) {
  69. $fields = explode($delimiter,$fields);
  70. } else {
  71. $fields = array($fields);
  72. }
  73. }
  74. foreach ($fields as $field) {
  75. if (is_array($field)) {
  76. $type = isset($field[2]) ? $field[2] : false;
  77. $name = $field[0];
  78. } else {
  79. $name = $field;
  80. $type = false;
  81. }
  82. $this->addField($name,$location,$type);
  83. if ($callback !== false) {
  84. call_user_func_array($callback, array($name));
  85. }
  86. }
  87. return true;
  88. }
  89. // ---------------------------------------------------
  90. // Function: removeField
  91. // Remove a field to the internal field detection system
  92. // ---------------------------------------------------
  93. function removeField($name,$location,$type) {
  94. $key = array_search ($name, $this->fields[$location][$type]);
  95. if ($key !== false) {
  96. unset($this->fields[$location][$type][$key]);
  97. }
  98. }
  99. // ---------------------------------------------------
  100. // Function: setDisplayFields
  101. // Move the detected fields into the Ditto fields array
  102. // ---------------------------------------------------
  103. function setDisplayFields($fields,$hiddenFields) {
  104. $this->fields["display"] = $fields;
  105. if (count($this->fields["display"]['qe']) > 0) {
  106. $this->addField("pagetitle","display","db");
  107. }
  108. if ($hiddenFields) {
  109. $this->addFields($hiddenFields,"display");
  110. }
  111. }
  112. // ---------------------------------------------------
  113. // Function: getDocVarType
  114. // Determine if the provided field is a tv, a database field, or something else
  115. // ---------------------------------------------------
  116. function getDocVarType($field) {
  117. global $ditto_constantFields;
  118. $tvFields = $ditto_constantFields["tv"];
  119. $dbFields = $ditto_constantFields["db"];
  120. if(in_array($field, $tvFields)){
  121. return "tv";
  122. }else if(in_array(substr($field,2), $tvFields)) {
  123. return "tv:prefix";
  124. // TODO: Remove TV Prefix support
  125. } else if(in_array($field, $dbFields)){
  126. return "db";
  127. } else {
  128. return "unknown";
  129. }
  130. }
  131. // ---------------------------------------------------
  132. // Function: parseOrderBy
  133. // Parse out orderBy parameter string
  134. // ---------------------------------------------------
  135. function parseOrderBy($orderBy,$randomize) {
  136. if ($randomize != 0) {return false;}
  137. $orderBy['sql'] = array();
  138. foreach ($orderBy['parsed'] as $item) {
  139. $this->addFields($item[0],'backend');
  140. $this->checkAdvSort($item[0],$item[1]);
  141. }
  142. foreach ($orderBy['custom'] as $item) {
  143. $this->addFields($item[0],'backend');
  144. $this->checkAdvSort($item[0]);
  145. }
  146. if (!is_null($orderBy['unparsed'])) {
  147. $inputs = explode(',',$orderBy['unparsed']);
  148. foreach ($inputs as $input) {
  149. $input = trim($input);
  150. $position = strrpos($input,' ');
  151. // find last space
  152. $sortBy = substr($input,0,$position);
  153. $sortBy = !empty($sortBy) ? $sortBy : 'id';
  154. $sortDir = substr($input,$position);
  155. $sortDir = !empty($sortDir) ? trim($sortDir) : 'asc';
  156. $sortBy = $this->checkAdvSort($sortBy,$sortDir);
  157. $this->addField($sortBy,"backend");
  158. $orderBy['parsed'][] = array($sortBy,strtoupper($sortDir));
  159. }
  160. }
  161. $orderBy['sql'] = implode(', ',$this->sqlOrderBy);
  162. unset($orderBy['unparsed']);
  163. return $orderBy;
  164. }
  165. // ---------------------------------------------------
  166. // Function: checkAdvSort
  167. // Check the advSortString
  168. // ---------------------------------------------------
  169. function checkAdvSort($sortBy,$sortDir='asc') {
  170. $advSort = array ("pub_date","unpub_date","editedon","deletedon","publishedon");
  171. $type = $this->getDocVarType($sortBy);
  172. switch($type) {
  173. case "tv:prefix":
  174. $sortBy = substr($sortBy, 2);
  175. $this->advSort = true;
  176. break;
  177. case "tv":
  178. $this->advSort = true;
  179. break;
  180. case "db":
  181. if (in_array($sortBy, $advSort)) {
  182. $this->advSort = true;
  183. $this->customReset[] = $sortBy;
  184. } else {
  185. $this->sqlOrderBy[] = 'sc.'.$sortBy.' '.$sortDir;
  186. }
  187. break;
  188. }
  189. return $sortBy;
  190. }
  191. // ---------------------------------------------------
  192. // Function: parseFilters
  193. // Split up the filters into an array and add the required fields to the fields array
  194. // ---------------------------------------------------
  195. function parseFilters($filter=false,$cFilters=false,$pFilters = false,$globalDelimiter,$localDelimiter) {
  196. $parsedFilters = array("basic"=>array(),"custom"=>array());
  197. $filters = explode($globalDelimiter, $filter);
  198. if ($filter && count($filters) > 0) {
  199. foreach ($filters AS $filter) {
  200. if (!empty($filter)) {
  201. $filterArray = explode($localDelimiter, $filter);
  202. $source = $filterArray[0];
  203. $this->addField($source,"backend");
  204. $value = $filterArray[1];
  205. $mode = (isset ($filterArray[2])) ? $filterArray[2] : 1;
  206. $parsedFilters["basic"][] = array("source"=>$source,"value"=>$value,"mode"=>$mode);
  207. }
  208. }
  209. }
  210. if ($cFilters) {
  211. foreach ($cFilters as $name=>$value) {
  212. if (!empty($name) && !empty($value)) {
  213. $parsedFilters["custom"][$name] = $value[1];
  214. $this->addFields($value[0],"backend");
  215. }
  216. } // TODO: Replace addField with addFields with callback
  217. }
  218. if($pFilters) {
  219. foreach ($pFilters as $filter) {
  220. foreach ($filter as $name=>$value) {
  221. $parsedFilters["basic"][] = $value;
  222. $this->addFields($value["source"],"backend");
  223. }
  224. } // TODO: Replace addField with addFields with callback
  225. }
  226. return $parsedFilters;
  227. }
  228. // ---------------------------------------------------
  229. // Function: render
  230. // Render the document output
  231. // ---------------------------------------------------
  232. function render($resource, $template, $removeChunk,$dateSource,$dateFormat,$ph=array(),$phx=1,$x=0) {
  233. global $modx,$ditto_lang;
  234. if (!is_array($resource)) {
  235. return $ditto_lang["resource_array_error"];
  236. }
  237. $placeholders = array();
  238. $contentVars = array();
  239. foreach ($resource as $name=>$value) {
  240. $placeholders["$name"] = $value;
  241. $contentVars["[*$name*]"] = $value;
  242. }
  243. // set author placeholder
  244. if (in_array("author",$this->fields["display"]["custom"])) {
  245. $placeholders['author'] = $this->getAuthor($resource['createdby']);
  246. }
  247. // set title placeholder
  248. if (in_array("title",$this->fields["display"]["custom"])) {
  249. $placeholders['title'] = $resource['pagetitle'];
  250. }
  251. // set sequence placeholder
  252. if (in_array("ditto_iteration",$this->fields["display"]["custom"])) {
  253. $placeholders['ditto_iteration'] = $x;
  254. }
  255. // set url placeholder
  256. if (in_array("url",$this->fields["display"]["custom"])) {
  257. $placeholders['url'] = $modx->makeURL($resource['id'],'','','full');
  258. }
  259. if (in_array("date",$this->fields["display"]["custom"])) {
  260. $timestamp = ($resource[$dateSource] != "0") ? $resource[$dateSource] : $resource["createdon"];
  261. if (is_array($timestamp)) {
  262. $timestamp[1] = is_int($timestamp[1]) ? $timestamp[1] : strtotime($timestamp[1]);
  263. $timestamp = $timestamp[1] + $timestamp[0];
  264. }
  265. $placeholders['date'] = strftime($dateFormat,$timestamp);
  266. }
  267. if (in_array("content",$this->fields["display"]["db"]) && $this->format != "html") {
  268. $resource['content'] = $this->relToAbs($resource['content'], $modx->config['site_url']);
  269. }
  270. if (in_array("introtext",$this->fields["display"]["db"]) && $this->format != "html") {
  271. $resource['introtext'] = $this->relToAbs($resource['introtext'], $modx->config['site_url']);
  272. }
  273. $customPlaceholders = $ph;
  274. // set custom placeholder
  275. foreach ($ph as $name=>$value) {
  276. if ($name != "*") {
  277. $placeholders[$name] = call_user_func($value[1],$resource);
  278. unset($customPlaceholders[$name]);
  279. }
  280. }
  281. foreach ($customPlaceholders as $name=>$value) {
  282. $placeholders = call_user_func($value,$placeholders);
  283. }
  284. if (count($this->fields["display"]['qe']) > 0) {
  285. $placeholders = $this->renderQELinks($this->template->fields['qe'],$resource,$ditto_lang["edit"]." : ".$resource['pagetitle']." : ",$placeholders);
  286. // set QE Placeholders
  287. }
  288. if ($phx == 1) {
  289. $PHs = $placeholders;
  290. foreach($PHs as $key=>$output) {
  291. $placeholders[$key] = str_replace( array_keys( $contentVars ), array_values( $contentVars ), $output );
  292. }
  293. unset($PHs);
  294. $phx = new prePHx($template);
  295. $phx->setPlaceholders($placeholders);
  296. $output = $phx->output();
  297. } else {
  298. $output = $this->template->replace($placeholders,$template);
  299. $output = $this->template->replace($contentVars,$output);
  300. }
  301. if ($removeChunk) {
  302. foreach ($removeChunk as $chunk) {
  303. $output = str_replace('{{'.$chunk.'}}',"",$output);
  304. $output = str_replace($modx->getChunk($chunk),"",$output);
  305. // remove chunk that is not wanted
  306. }
  307. }
  308. return $output;
  309. }
  310. // ---------------------------------------------------
  311. // Function: parseFields
  312. // Find the fields that are contained in the custom placeholders or those that are needed in other functions
  313. // ---------------------------------------------------
  314. function parseFields($placeholders,$seeThruUnpub,$dateSource,$randomize) {
  315. $this->parseCustomPlaceholders($placeholders);
  316. $this->parseDBFields($seeThruUnpub);
  317. if ($randomize != 0) {
  318. $this->addField($randomize,"backend");
  319. }
  320. $this->addField("id","display","db");
  321. $this->addField("pagetitle","display","db");
  322. $checkOptions = array("pub_date","unpub_date","editedon","deletedon","publishedon");
  323. if (in_array($dateSource,$checkOptions)) {
  324. $this->addField("createdon","display");
  325. }
  326. if (in_array("date",$this->fields["display"]["custom"])) {
  327. $this->addField($dateSource,"display");
  328. }
  329. $this->fields = $this->arrayUnique($this->fields);
  330. }
  331. // ---------------------------------------------------
  332. // Function: arrayUnique
  333. // Make fields array unique
  334. // ---------------------------------------------------
  335. function arrayUnique($array) {
  336. foreach($array as $u => $a) {
  337. foreach ($a as $n => $b) {
  338. $array[$u][$n] = array_unique($b);
  339. }
  340. }
  341. return $array;
  342. }
  343. // ---------------------------------------------------
  344. // Function: parseCustomPlaceholders
  345. // Parse the required fields out of the custom placeholders
  346. // ---------------------------------------------------
  347. function parseCustomPlaceholders($placeholders) {
  348. foreach ($placeholders as $name=>$value) {
  349. $this->addField($name,"display","custom");
  350. $this->removeField($name,"display","unknown");
  351. $source = $value[0];
  352. $qe = $value[2];
  353. if(is_array($source)) {
  354. if(strpos($source[0],",")!==false){
  355. $fields = explode(",",$source[0]);
  356. foreach ($fields as $field) {
  357. if (!empty($field)) {
  358. $this->addField($field,$source[1]);
  359. $this->customPlaceholdersMap[$name] = $field;
  360. }
  361. }
  362. } else {
  363. $this->addField($source[0],$source[1]);
  364. $this->customPlaceholdersMap[$name] = $source[0];
  365. } // TODO: Replace addField with addFields with callback
  366. } else if(is_array($value)) {
  367. $fields = explode(",",$source);
  368. foreach ($fields as $field) {
  369. if (!empty($field)) {
  370. $this->addField($field,"display");
  371. $this->customPlaceholdersMap[$name] = $field;
  372. }
  373. }
  374. }
  375. if (!is_null($qe)) {
  376. $this->customPlaceholdersMap[$name] = array('qe',$qe);
  377. }
  378. }
  379. }
  380. // ---------------------------------------------------
  381. // Function: parseDBFields
  382. // Parse out the fields required for each state
  383. // ---------------------------------------------------
  384. function parseDBFields($seeThruUnpub) {
  385. if (!$seeThruUnpub) {
  386. $this->addField("parent","backend","db");
  387. }
  388. if (in_array("author",$this->fields["display"]["custom"])) {
  389. $this->fields["display"]["db"][] = "createdby";
  390. }
  391. if (count($this->fields["display"]["tv"]) >= 0) {
  392. $this->addField("published","display","db");
  393. }
  394. }
  395. // ---------------------------------------------------
  396. // Function: renderQELinks
  397. // Render QE links when needed
  398. // ---------------------------------------------------
  399. function renderQELinks($fields, $resource, $QEPrefix,$placeholders) {
  400. global $modx,$dittoID;
  401. $table = $modx->getFullTableName("site_modules");
  402. $idResult = $modx->db->select("id", $table,"name='QuickEdit'","id","1");
  403. $id = $modx->db->getRow($idResult);
  404. $id = $id["id"];
  405. $custom = array("author","date","url","title");
  406. $set = $modx->hasPermission('exec_module');
  407. foreach ($fields as $dv) {
  408. $ds = $dv;
  409. if ($dv == "title") {
  410. $ds = "pagetitle";
  411. }
  412. if (!in_array($dv,$custom) && in_array($dv,$this->fields["display"]["custom"])) {
  413. $value = $this->customPlaceholdersMap[$dv];
  414. $ds = $value;
  415. if (is_array($value) && $value[0] == "qe") {
  416. $value = $value[1];
  417. if (substr($value,0,7) == "@GLOBAL") {
  418. $key = trim(substr($value,7));
  419. $ds = $GLOBALS[$key];
  420. }
  421. }
  422. }
  423. $js = "window.open('".$modx->config["site_url"]."manager/index.php?a=112&id=".$id."&doc=".$resource["id"]."&var=".$ds."', 'QuickEditor', 'width=525, height=300, toolbar=0, menubar=0, status=0, alwaysRaised=1, dependent=1');";
  424. $url = $this->buildURL("qe_open=true",$modx->documentIdentifier,$dittoID);
  425. unset($custom[3]);
  426. if ($set && !in_array($dv,$custom)) {
  427. $placeholders["#$dv"] = $placeholders["$dv"].'<a href="'.$url.'#" onclick="javascript: '.$js.'" title="'.$QEPrefix.$dv.'" class="QE_Link">&laquo; '.$QEPrefix.$dv.'</a>';
  428. } else {
  429. $placeholders["#$dv"] = $placeholders["$dv"];
  430. }
  431. }
  432. return $placeholders;
  433. }
  434. // ---------------------------------------------------
  435. // Function: getAuthor
  436. // Get the author name, or if not available the username
  437. // ---------------------------------------------------
  438. function getAuthor($createdby) {
  439. global $modx;
  440. $user = false;
  441. if ($createdby > 0) {
  442. $user = $modx->getUserInfo($createdby);
  443. } else {
  444. $user = $modx->getWebUserInfo(abs($createdby));
  445. }
  446. if ($user === false) {
  447. // get admin user name
  448. $user = $modx->getUserInfo(1);
  449. }
  450. return ($user['fullname'] != "") ? $user['fullname'] : $user['username'];
  451. }
  452. // ---------------------------------------------------
  453. // Function: customSort
  454. // Sort resource array if advanced sorting is needed
  455. // ---------------------------------------------------
  456. function customSort($data, $fields, $order) {
  457. // Covert $fields string to array
  458. // user contributed
  459. foreach (explode(',', $fields) as $s)
  460. $sortfields[] = trim($s);
  461. $code = "";
  462. for ($c = 0; $c < count($sortfields); $c++)
  463. $code .= "\$retval = strnatcmp(\$a['$sortfields[$c]'], \$b['$sortfields[$c]']); if(\$retval) return \$retval; ";
  464. $code .= "return \$retval;";
  465. $params = ($order == 'ASC') ? '$a,$b' : '$b,$a';
  466. uasort($data, create_function($params, $code));
  467. return $data;
  468. }
  469. // ---------------------------------------------------
  470. // Function: userSort
  471. // Sort the resource array by a user defined function
  472. // ---------------------------------------------------
  473. function userSort($resource,$sort) {
  474. foreach ($sort['custom'] as $item) {
  475. usort($resource,$item[1]);
  476. }
  477. return $resource;
  478. }
  479. // ---------------------------------------------------
  480. // Function: multiSort
  481. // Sort the resource array by multiple fields
  482. // Rows->Columns portion by Jon L. -- intel352@gmail.com
  483. // Link: http://de3.php.net/manual/en/function.array-multisort.php#73498
  484. // ---------------------------------------------------
  485. function multiSort($resource,$orderBy) {
  486. $sort_arr = array();
  487. foreach($resource AS $uniqid => $row){
  488. foreach($row AS $key=>$value){
  489. $sort_arr[$key][$uniqid] = $value;
  490. }
  491. }
  492. $array_multisort = 'return array_multisort(';
  493. foreach ($orderBy['parsed'] as $sort) {
  494. $array_multisort .= '$sort_arr["'.$sort[0].'"], SORT_'.$sort[1].', ';
  495. }
  496. $array_multisort .= '$resource);';
  497. eval($array_multisort);
  498. return $resource;
  499. }
  500. // ---------------------------------------------------
  501. // Function: determineIDs
  502. // Get Document IDs for future use
  503. // ---------------------------------------------------
  504. function determineIDs($IDs, $IDType, $TVs, $orderBy, $depth, $showPublishedOnly, $seeThruUnpub, $hideFolders, $hidePrivate, $showInMenuOnly, $myWhere, $keywords, $dateSource, $limit, $summarize, $filter, $paginate, $randomize) {
  505. global $modx;
  506. if (($summarize == 0 && $summarize != "all") || count($IDs) == 0 || ($IDs == false && $IDs != "0")) {
  507. return array();
  508. }
  509. // Get starting IDs;
  510. switch($IDType) {
  511. case "parents":
  512. $IDs = explode(",",$IDs);
  513. $documentIDs = $this->getChildIDs($IDs, $depth);
  514. break;
  515. case "documents":
  516. $documentIDs = explode(",",$IDs);
  517. break;
  518. }
  519. if ($this->advSort == false && $hideFolders==0 && $showInMenuOnly==0 && $myWhere == "" && $filter == false && $hidePrivate == 1 && $keywords==0) {
  520. $this->prefetch = false;
  521. $documents = $this->getDocumentsIDs($documentIDs, $showPublishedOnly);
  522. $documentIDs = array();
  523. if ($documents) {
  524. foreach ($documents as $null=>$doc) {
  525. $documentIDs[] = $doc["id"];
  526. }
  527. }
  528. return $documentIDs;
  529. } else {
  530. $this->prefetch = true;
  531. }
  532. // Create where clause
  533. $where = array ();
  534. if ($hideFolders) {
  535. $where[] = 'isfolder = 0';
  536. }
  537. if ($showInMenuOnly) {
  538. $where[] = 'hidemenu = 0';
  539. }
  540. if ($myWhere != '') {
  541. $where[] = $myWhere;
  542. }
  543. $where = implode(" AND ", $where);
  544. $limit = ($limit == 0) ? "" : $limit;
  545. // set limit
  546. $customReset = $this->customReset;
  547. if ($keywords) {$this->addField("haskeywords","*","db");$this->addField("hasmetatags","*","db");}
  548. if ($this->debug) {$this->addField("pagetitle","backend","db");}
  549. if (count($customReset) > 0) {$this->addField("createdon","backend","db");}
  550. $resource = $this->getDocuments($documentIDs,$this->fields["backend"]["db"],$TVs,$orderBy,$showPublishedOnly,0,$hidePrivate,$where,$limit,$keywords,$randomize,$dateSource);
  551. if ($resource !== false) {
  552. $resource = array_values($resource);
  553. // remove #'s from keys
  554. $recordCount = count($resource);
  555. // count number of records
  556. if (!$seeThruUnpub) {
  557. $parentList = $this->getParentList();
  558. // get parent list
  559. }
  560. for ($i = 0; $i < $recordCount; $i++) {
  561. if (!$seeThruUnpub) {
  562. $published = $parentList[$resource[$i]["parent"]];
  563. if ($published == "0")
  564. unset ($resource[$i]);
  565. }
  566. if (count($customReset) > 0) {
  567. foreach ($customReset as $field) {
  568. if ($resource[$i][$field] === "0") {
  569. $resource[$i][$field] = $resource[$i]["createdon"];
  570. }
  571. }
  572. }
  573. }
  574. if ($this->debug) {
  575. $dbg_resource = $resource;
  576. }
  577. if ($filter != false) {
  578. $filterObj = new filter();
  579. $resource = $filterObj->execute($resource, $filter);
  580. }
  581. if (count($resource) < 1) return array();
  582. if ($this->advSort == true && $randomize==0) {
  583. $resource = $this->multiSort($resource,$orderBy);
  584. }
  585. if (count($orderBy['custom']) > 0) {
  586. $resource = $this->userSort($resource,$orderBy);
  587. }
  588. $fields = (array_intersect($this->fields["backend"],$this->fields["display"]));
  589. $readyFields = array();
  590. foreach ($fields as $field) {
  591. $readyFields = array_merge($readyFields,$field);
  592. }
  593. $processedIDs = array ();
  594. $keep = array();
  595. foreach ($resource as $key => $value) {
  596. $processedIDs[] = $value['id'];
  597. $iKey = '#'.$value['id'];
  598. foreach ($value as $key=>$v) {
  599. if (in_array($key,$readyFields)) {
  600. $keep[$iKey][$key] = $v;
  601. }
  602. if ($this->getDocVarType($key) == "tv:prefix") {
  603. if (in_array(substr($key,2),$readyFields)) {
  604. $keep[$iKey][$key] = $v;
  605. }
  606. }
  607. }
  608. }
  609. $this->prefetch = array("resource"=>$keep,"fields"=>$fields);
  610. if ($this->debug) {
  611. $this->prefetch["dbg_resource"] = $dbg_resource;
  612. $this->prefetch["dbg_IDs_pre"] = $documentIDs;
  613. $this->prefetch["dbg_IDs_post"] = $processedIDs;
  614. }
  615. if (count($processedIDs) > 0) {
  616. if ($randomize != 0) {shuffle($processedIDs);}
  617. $this->sortOrder = array_flip($processedIDs);
  618. // saves the order of the documents for use later
  619. }
  620. return $processedIDs;
  621. } else {
  622. return array();
  623. }
  624. }
  625. // ---------------------------------------------------
  626. // Function: weightedRandom
  627. // Execute a random order sort
  628. // ---------------------------------------------------
  629. function weightedRandom($resource,$field,$show) {
  630. $type = $this->getDocVarType($field);
  631. if ($type == "unknown") {
  632. return $resource;
  633. // handle vad field passed
  634. }
  635. $random = new random();
  636. foreach ($resource as $document) {
  637. $doc = $document;
  638. $random->add($doc,abs(intval($document[$field])));
  639. }
  640. $resource = $random->select_weighted_unique($show);
  641. shuffle($resource);
  642. return $resource;
  643. }
  644. // ---------------------------------------------------
  645. // Function: getParentList
  646. // Get a list of all available parents
  647. // ---------------------------------------------------
  648. function getParentList() {
  649. global $modx;
  650. $kids = array();
  651. foreach ($modx->documentMap as $null => $document) {
  652. foreach ($document as $parent => $id) {
  653. $kids[$parent][] = $id;
  654. }
  655. }
  656. $parents = array();
  657. foreach ($kids as $item => $value) {
  658. if ($item != 0) {
  659. $pInfo = $modx->getPageInfo($item,0,"published");
  660. } else {
  661. $pInfo["published"] = "1";
  662. }
  663. $parents[$item] = $pInfo["published"];
  664. }
  665. return $parents;
  666. }
  667. // ---------------------------------------------------
  668. // Function: appendTV
  669. // Apeend a TV to the documents array
  670. // ---------------------------------------------------
  671. function appendTV($tvname="",$docIDs){
  672. global $modx;
  673. $baspath= $modx->config["base_path"] . "manager/includes";
  674. include_once $baspath . "/tmplvars.format.inc.php";
  675. include_once $baspath . "/tmplvars.commands.inc.php";
  676. $tb1 = $modx->getFullTableName("site_tmplvar_contentvalues");
  677. $tb2 = $modx->getFullTableName("site_tmplvars");
  678. $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
  679. $query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
  680. $query .= " WHERE stv.name='".$tvname."' AND stc.contentid IN (".implode($docIDs,",").") ORDER BY stc.contentid ASC;";
  681. $rs = $modx->db->query($query);
  682. $tot = $modx->db->getRecordCount($rs);
  683. $resourceArray = array();
  684. for($i=0;$i<$tot;$i++) {
  685. $row = @$modx->fetchRow($rs);
  686. $resourceArray["#".$row['contentid']][$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);
  687. $resourceArray["#".$row['contentid']]["tv".$row['name']] = $resourceArray["#".$row['contentid']][$row['name']];
  688. }
  689. if ($tot != count($docIDs)) {
  690. $query = "SELECT name,type,display,display_params,default_text";
  691. $query .= " FROM $tb2";
  692. $query .= " WHERE name='".$tvname."' LIMIT 1";
  693. $rs = $modx->db->query($query);
  694. $row = @$modx->fetchRow($rs);
  695. if (strtoupper($row['default_text']) == '@INHERIT') {
  696. foreach ($docIDs as $id) {
  697. $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'], $id);
  698. if (!isset($resourceArray["#".$id])) {
  699. $resourceArray["#$id"][$tvname] = $defaultOutput;
  700. $resourceArray["#$id"]["tv".$tvname] = $resourceArray["#$id"][$tvname];
  701. }
  702. }
  703. } else {
  704. $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'],$row['contentid']);
  705. foreach ($docIDs as $id) {
  706. if (!isset($resourceArray["#".$id])) {
  707. $resourceArray["#$id"][$tvname] = $defaultOutput;
  708. $resourceArray["#$id"]["tv".$tvname] = $resourceArray["#$id"][$tvname];
  709. }
  710. }
  711. }
  712. }
  713. return $resourceArray;
  714. }
  715. // ---------------------------------------------------
  716. // Function: appendKeywords
  717. // Append keywords's to the resource array
  718. // ---------------------------------------------------
  719. function appendKeywords($resource) {
  720. $keys = $this->fetchKeywords($resource);
  721. $resource["keywords"] = "$keys";
  722. return $resource;
  723. }
  724. // ---------------------------------------------------
  725. // Function: fetchKeywords
  726. // Helper function to <appendKeywords>
  727. // ---------------------------------------------------
  728. function fetchKeywords($resource) {
  729. global $modx;
  730. if($resource['haskeywords']==1) {
  731. // insert keywords
  732. $metas = implode(",",$modx->getKeywords($resource["id"]));
  733. }
  734. if($resource['hasmetatags']==1){
  735. // insert meta tags
  736. $tags = $modx->getMETATags($resource["id"]);
  737. foreach ($tags as $n=>$col) {
  738. $tag = strtolower($col['tag']);
  739. $metas.= ",".$col['tagvalue'];
  740. }
  741. }
  742. return $metas;
  743. }
  744. // ---------------------------------------------------
  745. // Function: getChildIDs
  746. // Get the IDs ready to be processed
  747. // Similar to the modx version by the same name but much faster
  748. // ---------------------------------------------------
  749. function getChildIDs($IDs, $depth) {
  750. global $modx;
  751. $depth = intval($depth);
  752. $kids = array();
  753. $docIDs = array();
  754. if ($depth == 0 && $IDs[0] == 0 && count($IDs) == 1) {
  755. foreach ($modx->documentMap as $null => $document) {
  756. foreach ($document as $parent => $id) {
  757. $kids[] = $id;
  758. }
  759. }
  760. return $kids;
  761. } else if ($depth == 0) {
  762. $depth = 10000;
  763. // Impliment unlimited depth...
  764. }
  765. foreach ($modx->documentMap as $null => $document) {
  766. foreach ($document as $parent => $id) {
  767. $kids[$parent][] = $id;
  768. }
  769. }
  770. foreach ($IDs AS $seed) {
  771. if (!empty($kids[intval($seed)])) {
  772. $docIDs = array_merge($docIDs,$kids[intval($seed)]);
  773. unset($kids[intval($seed)]);
  774. }
  775. }
  776. $depth--;
  777. while($depth != 0) {
  778. $valid = $docIDs;
  779. foreach ($docIDs as $child=>$id) {
  780. if (!empty($kids[intval($id)])) {
  781. $docIDs = array_merge($docIDs,$kids[intval($id)]);
  782. unset($kids[intval($id)]);
  783. }
  784. }
  785. $depth--;
  786. if ($valid == $docIDs) $depth = 0;
  787. }
  788. return array_unique($docIDs);
  789. }
  790. // ---------------------------------------------------
  791. // Function: getDocuments
  792. // Get documents and append TVs + Prefetch Data, and sort
  793. // ---------------------------------------------------
  794. function getDocuments($ids= array (), $fields, $TVs, $orderBy, $published= 1, $deleted= 0, $public= 1, $where= '', $limit= "",$keywords=0,$randomize=0,$dateSource=false) {
  795. global $modx;
  796. if (count($ids) == 0) {
  797. return false;
  798. } else {
  799. sort($ids);
  800. $limit= ($limit != "") ? "LIMIT $limit" : ""; // LIMIT capabilities - rad14701
  801. $tblsc= $modx->getFullTableName("site_content");
  802. $tbldg= $modx->getFullTableName("document_groups");
  803. // modify field names to use sc. table reference
  804. $fields= "sc.".implode(",sc.",$fields);
  805. if ($randomize != 0) {
  806. $sort = "RAND()";
  807. } else {
  808. $sort= $orderBy['sql'];
  809. }
  810. $where= ($where == "") ? "" : 'AND sc.' . implode('AND sc.', preg_replace("/^\s/i", "", explode('AND', $where)));
  811. if ($public) {
  812. // get document groups for current user
  813. if ($docgrp= $modx->getUserDocGroups())
  814. $docgrp= implode(",", $docgrp);
  815. $access= ($modx->isFrontend() ? "sc.privateweb=0" : "1='" . $_SESSION['mgrRole'] . "' OR sc.privatemgr=0") .
  816. (!$docgrp ? "" : " OR dg.document_group IN ($docgrp)");
  817. }
  818. $published = ($published) ? "AND sc.published=1" : "";
  819. $sql = "SELECT DISTINCT $fields FROM $tblsc sc
  820. LEFT JOIN $tbldg dg on dg.document = sc.id
  821. WHERE sc.id IN (" . join($ids, ",") . ") $published AND sc.deleted=$deleted $where
  822. ".($public ? 'AND ('.$access.')' : '')." GROUP BY sc.id" .
  823. ($sort ? " ORDER BY $sort" : "") . " $limit ";
  824. $result= $modx->db->query($sql);
  825. $resourceArray= array ();
  826. $cnt = @$modx->db->getRecordCount($result);
  827. $TVData = array();
  828. $TVIDs = array();
  829. if ($cnt) {
  830. for ($i= 0; $i < $cnt; $i++) {
  831. $resource = $modx->fetchRow($result);
  832. if ($modx->config["server_offset_time"] != 0 && $dateSource !== false) {
  833. $dateValue = (is_int($resource[$dateSource]) !== true) ? $resource[$dateSource] : strtotime($resource[$dateSource]);
  834. $resource[$dateSource] = $dateValue + $modx->config["server_offset_time"];
  835. }
  836. if($keywords) {
  837. $resource = $this->appendKeywords($resource);
  838. }
  839. if ($this->prefetch == true && $this->sortOrder !== false) $resource["ditto_sort"] = $this->sortOrder[$resource["id"]];
  840. $TVIDs[] = $resource["id"];
  841. $resourceArray["#".$resource["id"]] = $resource;
  842. if (count($this->prefetch["resource"]) > 0) {
  843. $x = "#".$resource["id"];
  844. $resourceArray[$x] = array_merge($resource,$this->prefetch["resource"][$x]);
  845. // merge the prefetch array and the normal array
  846. }
  847. }
  848. $TVs = array_unique($TVs);
  849. if (count($TVs) > 0) {
  850. foreach($TVs as $tv){
  851. $TVData = array_merge_recursive($this->appendTV($tv,$TVIDs),$TVData);
  852. }
  853. }
  854. $resourceArray = array_merge_recursive($resourceArray,$TVData);
  855. if ($this->prefetch == true && $this->sortOrder !== false) {
  856. $resourceArray = $this->customSort($resourceArray,"ditto_sort","ASC");
  857. }
  858. return $resourceArray;
  859. } else {
  860. return false;
  861. }
  862. }
  863. }
  864. // ---------------------------------------------------
  865. // Function: getDocumentsLite
  866. // Get an array of documents
  867. // ---------------------------------------------------
  868. function getDocumentsIDs($ids= array (), $published= 1) {
  869. global $modx;
  870. if (count($ids) == 0) {
  871. return false;
  872. } else {
  873. $tblsc= $modx->getFullTableName("site_content");
  874. $tbldg= $modx->getFullTableName("document_groups");
  875. if ($docgrp= $modx->getUserDocGroups())
  876. $docgrp= implode(",", $docgrp);
  877. $access= ($modx->isFrontend() ? "sc.privateweb=0" : "1='" . $_SESSION['mgrRole'] . "' OR sc.privatemgr=0") .
  878. (!$docgrp ? "" : " OR dg.document_group IN ($docgrp)");
  879. $published = ($published) ? "AND sc.published=1" : "";
  880. $sql= "SELECT DISTINCT sc.id FROM $tblsc sc
  881. LEFT JOIN $tbldg dg on dg.document = sc.id
  882. WHERE (sc.id IN (" . join($ids, ",") . ") $published AND sc.deleted=0)
  883. AND ($access)
  884. GROUP BY sc.id ";
  885. $result= $modx->dbQuery($sql);
  886. $resourceArray= array ();
  887. for ($i= 0; $i < @ $modx->recordCount($result); $i++) {
  888. array_push($resourceArray, @ $modx->fetchRow($result));
  889. }
  890. return $resourceArray;
  891. }
  892. }
  893. // ---------------------------------------------------
  894. // Function: cleanIDs
  895. // Clean the IDs of any dangerous characters
  896. // ---------------------------------------------------
  897. function cleanIDs($IDs) {
  898. //Define the pattern to search for
  899. $pattern = array (
  900. '`(,)+`', //Multiple commas
  901. '`^(,)`', //Comma on first position
  902. '`(,)$`' //Comma on last position
  903. );
  904. //Define replacement parameters
  905. $replace = array (
  906. ',',
  907. '',
  908. ''
  909. );
  910. //Clean startID (all chars except commas and numbers are removed)
  911. $IDs = preg_replace($pattern, $replace, $IDs);
  912. return $IDs;
  913. }
  914. // ---------------------------------------------------
  915. // Function: buildURL
  916. // Build a URL with regard to Ditto ID
  917. // ---------------------------------------------------
  918. function buildURL($args,$id=false,$dittoIdentifier=false) {
  919. global $modx, $dittoID;
  920. $dittoID = ($dittoIdentifier !== false) ? $dittoIdentifier : $dittoID;
  921. $query = array();
  922. foreach ($_GET as $param=>$value) {
  923. if ($param != 'id' && $param != 'q') {
  924. $query[htmlspecialchars($param, ENT_QUOTES)] = htmlspecialchars($value, ENT_QUOTES);
  925. }
  926. }
  927. if (!is_array($args)) {
  928. $args = explode("&",$args);
  929. foreach ($args as $arg) {
  930. $arg = explode("=",$arg);
  931. $query[$dittoID.$arg[0]] = urlencode(trim($arg[1]));
  932. }
  933. } else {
  934. foreach ($args as $name=>$value) {
  935. $query[$dittoID.$name] = urlencode(trim($value));
  936. }
  937. }
  938. $queryString = "";
  939. foreach ($query as $param=>$value) {
  940. $queryString .= '&'.$param.'='.(is_array($value) ? implode(",",$value) : $value);
  941. }
  942. $cID = ($id !== false) ? $id : $modx->documentObject['id'];
  943. $url = $modx->makeURL(trim($cID), '', $queryString);
  944. return ($modx->config['xhtml_urls']) ? $url : str_replace("&","&amp;",$url);
  945. }
  946. // ---------------------------------------------------
  947. // Function: getParam
  948. // Get a parameter or use the default language value
  949. // ---------------------------------------------------
  950. function getParam($param,$langString){
  951. // get a parameter value and if it is not set get the default language string value
  952. global $modx,$ditto_lang;
  953. $out = "";
  954. if ($this->template->fetch($param) != "") {
  955. return $modx->getChunk($param);
  956. } else if(!empty($param)) {
  957. return $param;
  958. }else{
  959. return $ditto_lang[$langString];
  960. }
  961. }
  962. // ---------------------------------------------------
  963. // Function: paginate
  964. // Paginate the documents
  965. // ---------------------------------------------------
  966. function paginate($start, $stop, $total, $summarize, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter) {
  967. global $modx, $dittoID,$ditto_lang;
  968. if ($stop == 0 || $total == 0 || $summarize==0) {
  969. return false;
  970. }
  971. $next = $start + $summarize;
  972. $rNext = $this->template->replace(array('url'=>$this->buildURL("start=$next"),'lang:next'=>$ditto_lang['next']),$tplPaginateNext);
  973. $previous = $start - $summarize;
  974. $rPrevious = $this->template->replace(array('url'=>$this->buildURL("start=$previous"),'lang:previous'=>$ditto_lang['prev']),$tplPaginatePrevious);
  975. $limten = $summarize + $start;
  976. if ($paginateAlwaysShowLinks == 1) {
  977. $previousplaceholder = $this->template->replace(array('lang:previous'=>$ditto_lang['prev']),$tplPaginatePreviousOff);
  978. $nextplaceholder = $this->template->replace(array('lang:next'=>$ditto_lang['next']),$tplPaginateNextOff);
  979. } else {
  980. $previousplaceholder = "";
  981. $nextplaceholder = "";
  982. }
  983. $split = "";
  984. if ($previous > -1 && $next < $total)
  985. $split = $paginateSplitterCharacter;
  986. if ($previous > -1)
  987. $previousplaceholder = $rPrevious;
  988. if ($next < $total)
  989. $nextplaceholder = $rNext;
  990. if ($start < $total)
  991. $stop = $limten;
  992. if ($limten > $total) {
  993. $limiter = $total;
  994. } else {
  995. $limiter = $limten;
  996. }
  997. $totalpages = ceil($total / $summarize);
  998. for ($x = 0; $x <= $totalpages -1; $x++) {
  999. $inc = $x * $summarize;
  1000. $display = $x +1;
  1001. if ($inc != $start) {
  1002. $pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
  1003. } else {
  1004. $modx->setPlaceholder($dittoID."currentPage", $display);
  1005. $pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
  1006. }
  1007. }
  1008. $modx->setPlaceholder($dittoID."next", $nextplaceholder);
  1009. $modx->setPlaceholder($dittoID."previous", $previousplaceholder);
  1010. $modx->setPlaceholder($dittoID."splitter", $split);
  1011. $modx->setPlaceholder($dittoID."start", $start +1);
  1012. $modx->setPlaceholder($dittoID."urlStart", $start);
  1013. $modx->setPlaceholder($dittoID."stop", $limiter);
  1014. $modx->setPlaceholder($dittoID."total", $total);
  1015. $modx->setPlaceholder($dittoID."pages", $pages);
  1016. $modx->setPlaceholder($dittoID."perPage", $summarize);
  1017. $modx->setPlaceholder($dittoID."totalPages", $totalpages);
  1018. $modx->setPlaceholder($dittoID."ditto_pagination_set", true);
  1019. }
  1020. // ---------------------------------------------------
  1021. // Function: noResults
  1022. // Render the noResults output
  1023. // ---------------------------------------------------
  1024. function noResults($text,$paginate) {
  1025. global $modx, $dittoID;
  1026. $set = $modx->getPlaceholder($dittoID."ditto_pagination_set");
  1027. if ($paginate && $set !== true) {
  1028. $modx->setPlaceholder($dittoID."next", "");
  1029. $modx->setPlaceholder($dittoID."previous", "");
  1030. $modx->setPlaceholder($dittoID."splitter", "");
  1031. $modx->setPlaceholder($dittoID."start", 0);
  1032. $modx->setPlaceholder($dittoID."urlStart", "#start");
  1033. $modx->setPlaceholder($dittoID."stop", 0);
  1034. $modx->setPlaceholder($dittoID."total", 0);
  1035. $modx->setPlaceholder($dittoID."pages", "");
  1036. $modx->setPlaceholder($dittoID."perPage", 0);
  1037. $modx->setPlaceholder($dittoID."totalPages", 0);
  1038. $modx->setPlaceholder($dittoID."currentPage", 0);
  1039. }
  1040. return $text;
  1041. }
  1042. // ---------------------------------------------------
  1043. // Function: relToAbs
  1044. // Convert relative urls to absolute URLs
  1045. // Based on script from http://wintermute.com.au/bits/2005-09/php-relative-absolute-links/
  1046. // ---------------------------------------------------
  1047. function relToAbs($text, $base) {
  1048. return preg_replace('#(href|src)="([^:"]*)(?:")#','$1="'.$base.'$2"',$text);
  1049. }
  1050. }
  1051. ?>