API:getTemplateVars

From MODx Wiki

Jump to: navigation, search

Overview

Returns an array of TV records.

$idnames - an array of template variable IDs or names that belong to the template the current document is using.

Source Code

File: manager/includes/document.parser.class.inc.php
Line: 1824
  1. function getTemplateVars($idnames= array (), $fields= "*", $docid= "", $published= 1, $sort= "rank", $dir= "ASC") {
  2. if (($idnames != '*' && !is_array($idnames)) || count($idnames) == 0) {
  3. return false;
  4. } else {
  5. $result= array ();
  6.  
  7. // get document record
  8. if ($docid == "") {
  9. $docid= $this->documentIdentifier;
  10. $docRow= $this->documentObject;
  11. } else {
  12. $docRow= $this->getDocument($docid, '*', $published);
  13. if (!$docRow)
  14. return false;
  15. }
  16.  
  17. // get user defined template variables
  18. $fields= ($fields == "") ? "tv.*" : 'tv.' . implode(',tv.', preg_replace("/^\s/i", "", explode(',', $fields)));
  19. $sort= ($sort == "") ? "" : 'tv.' . implode(',tv.', preg_replace("/^\s/i", "", explode(',', $sort)));
  20. if ($idnames == "*")
  21. $query= "tv.id<>0";
  22. else
  23. $query= (is_numeric($idnames[0]) ? "tv.id" : "tv.name") . " IN ('" . implode("','", $idnames) . "')";
  24. if ($docgrp= $this->getUserDocGroups())
  25. $docgrp= implode(",", $docgrp);
  26. $sql= "SELECT $fields, IF(tvc.value!='',tvc.value,tv.default_text) as value ";
  27. $sql .= "FROM " . $this->getFullTableName('site_tmplvars')." tv ";
  28. $sql .= "INNER JOIN " . $this->getFullTableName('site_tmplvar_templates')." tvtpl ON tvtpl.tmplvarid = tv.id ";
  29. $sql .= "LEFT JOIN " . $this->getFullTableName('site_tmplvar_contentvalues')." tvc ON tvc.tmplvarid=tv.id AND tvc.contentid = '" . $docid . "' ";
  30. $sql .= "WHERE " . $query . " AND tvtpl.templateid = " . $docRow['template'];
  31. if ($sort)
  32. $sql .= " ORDER BY $sort $dir ";
  33. $rs= $this->dbQuery($sql);
  34. for ($i= 0; $i < @ $this->recordCount($rs); $i++) {
  35. array_push($result, @ $this->fetchRow($rs));
  36. }
  37.  
  38. // get default/built-in template variables
  39. ksort($docRow);
  40. foreach ($docRow as $key => $value) {
  41. if ($idnames == "*" || in_array($key, $idnames))
  42. array_push($result, array (
  43. "name" => $key,
  44. "value" => $value
  45. ));
  46. }
  47.  
  48. return $result;
  49. }
  50. }
Personal tools