private function MergeOtherDocumentContent(&$content) { // Parses (yams_data:{docId:}tv{:phx}) placeholders. // Returns true if the content has been changed. $documentVariableNullValues = array('id' => 'sc.id', 'type' => '\'\'', 'contentType' => '\'\'', 'pagetitle' => '\'\'', 'longtitle' => '\'\'', 'description' => '\'\'', 'alias' => '\'\'', 'link_attributes' => '\'\'', 'published' => 0, 'pub_date' => 0, 'unpub_date' => 0, 'parent' => 0, 'isfolder' => 0, 'introtext' => '\'\'', 'content' => '\'\'', 'richtext' => 0, 'template' => 0, 'menuindex' => 0, 'searchable' => 0, 'cacheable' => 0, 'createdby' => 0, 'createdon' => 0, 'editedby' => 0, 'editedon' => 0, 'deleted' => 0, 'deletedon' => 0, 'deletedby' => 0, 'publishedon' => 0, 'publishedby' => 0, 'menutitle' => '\'\'', 'donthit' => 0, 'haskeywords' => 0, 'hasmetatags' => 0, 'privateweb' => 0, 'privatemgr' => 0, 'content_dispo' => 0, 'hidemenu' => 0); // First search out all the placeholders... // Place them in cache arrays which have the following structure... // // For document variables... // // $dvInfo = array( // docId1 => array( // 'dv1' => array( // 'match1' => 'phx1' // , 'match2' => 'phx2' // , ... // ) // 'dv2' => array( // 'match1' => 'phx1' // , 'match2' => 'phx2' // , ... // ) // , ... // ) // ); // // or for template variables // // $tvInfo = array( // docId1 => array( // 'tv1' => array( // 'match1' => 'phx1' // , 'match2' => 'phx2' // , ... // ) // 'tv2' => array( // 'match1' => 'phx1' // , 'match2' => 'phx2' // , ... // ) // , ... // ) // ); $contentChanged = FALSE; $tvInfo = array(); $dvInfo = array(); $nMatches = preg_match_all('/\\(\\(yams_data:(([0-9]{0,13}):)?([^:]+)((:[.*])?)\\)\\)/U' . $this->itsUTF8Modifier, $content, $matches); // Loop over the placeholders and fill in the dv and tv caches $basepath = $this->itsMODx->config['base_path'] . 'manager/includes'; for ($i = 0; $i < $nMatches; $i++) { $docId = $matches[2][$i]; if ($docId == '') { $docId = $this->itsMODx->documentIdentifier; } $name = $matches[3][$i]; if (array_key_exists($name, $documentVariableNullValues)) { $info =& $dvInfo; } else { $info =& $tvInfo; } if (!array_key_exists($docId, $info)) { $info[$docId] = array(); } $docCache =& $info[$docId]; $phx = $matches[4][$i]; $match = '/' . preg_quote($matches[0][$i], '/') . '/' . $this->itsUTF8Modifier; if (!array_key_exists($name, $docCache)) { $docCache[$name] = array(); } $docCache[$name][$match] = $phx; } // Now loop over the tv array cache and write an SQL statement // that will grab the information from the database // a maximum of YAMS_DOC_LIMIT docs at a time $docIds = array_keys($tvInfo); $nDocs = count($docIds); $sc = $this->itsMODx->getFullTableName('site_content'); $st = $this->itsMODx->getFullTableName('site_tmplvars'); $stt = $this->itsMODx->getFullTableName('site_tmplvar_templates'); $stc = $this->itsMODx->getFullTableName('site_tmplvar_contentvalues'); for ($i = 0; $i < $nDocs; $i = $i + YAMS_DOC_LIMIT) { $sqlArray = array(); $jMax = min($nDocs, $i + YAMS_DOC_LIMIT); for ($j = $i; $j < $jMax; $j++) { $docId = $docIds[$j]; $docCache =& $tvInfo[$docId]; $inArray = array_keys($docCache); foreach ($inArray as &$tvName) { $tvName = '\'' . $this->itsMODx->db->escape($tvName) . '\''; } $inList = implode(',', $inArray); $sqlArray[] = '(SELECT' . ' sc.id AS docid' . ', st.name AS name' . ', IF(stc.value != \'\',stc.value,st.default_text) AS value' . ', st.display AS display' . ', st.display_params AS display_params' . ', st.type AS type' . ' FROM ' . $st . ' st' . ' LEFT JOIN ' . $stc . ' stc' . ' ON st.id = stc.tmplvarid' . ' AND stc.contentid = ' . $docId . ' INNER JOIN ' . $stt . ' stt' . ' ON stt.tmplvarid = st.id' . ' INNER JOIN ' . $sc . ' sc' . ' ON sc.id = ' . $docId . ' WHERE' . ' st.name IN (' . $inList . ')' . ' AND sc.deleted = 0' . ')'; } if (count($sqlArray) == 0) { continue; } $sql = implode(' UNION ', $sqlArray) . ';'; // Grab the data from the database... $result = $this->itsMODx->db->query($sql); $count = $this->itsMODx->recordCount($result); // Set up find and replace arrays... $find = array(); $replace = array(); for ($j = 0; $j < $count; $j++) { $row = $this->itsMODx->db->getRow($result); $docId =& $row['docid']; $tvName =& $row['name']; $tvValue =& $row['value']; $tvDisplay =& $row['display']; $tvDisplayParams =& $row['display_params']; $tvType =& $row['type']; $matches =& $tvInfo[$docId][$tvName]; include_once $basepath . '/tmplvars.format.inc.php'; include_once $basepath . '/tmplvars.commands.inc.php'; $w = '100%'; $h = '300'; $escapedvalue = YamsUtils::PregQuoteReplacement(getTVDisplayFormat($tvName, $tvValue, $tvDisplay, $tvDisplayParams, $tvType)); foreach ($matches as $match => $phx) { $find[] = $match; $replace[] = $escapedvalue; // TO DO: PHx stuff... } } if (count($find) == 0) { continue; } $content = preg_replace($find, $replace, $content, -1, $nReplacements); if ($nReplacements > 0) { $contentChanged = TRUE; } } // release the tv cache... unset($tvInfo); // Now loop over the dv array cache and write an SQL statement // that will grab the information from the database // a maximum of YAMS_DOC_LIMIT docs at a time $docIds = array_keys($dvInfo); $nDocs = count($docIds); $sc = $this->itsMODx->getFullTableName('site_content'); $dg = $this->itsMODx->getFullTableName('document_groups'); // get document groups for current user $docgrp = $this->itsMODx->getUserDocGroups(); if (is_array($docgrp)) { $docgrp = implode(',', $docgrp); } // get document if ($this->itsMODx->isFrontend()) { $access = 'sc.privateweb=0'; } else { $access = '\'' . $this->itsMODx->db->escape($_SESSION['mgrRole']) . '\'=1 OR sc.privatemgr=0'; if ($docgrp) { $access .= ' OR dg.document_group IN (' . $docgrp . ')'; } } for ($i = 0; $i < $nDocs; $i = $i + YAMS_DOC_LIMIT) { $sqlArray = array(); $jMax = min($nDocs, $i + YAMS_DOC_LIMIT); for ($j = $i; $j < $jMax; $j++) { $docId = $docIds[$j]; $docCache =& $dvInfo[$docId]; $colsArray = array(); $inArray = array_keys($docCache); if (count($inArray) == 0) { continue; } foreach ($documentVariableNullValues as $dvName => $dvNullValue) { if (array_key_exists($dvName, $docCache)) { // We need to grab the data for this column from the database $colsArray[] = 'sc.' . $dvName . ' as ' . $dvName; } else { // Just grab a null value instead $colsArray[] = $dvNullValue . ' as ' . $dvName; } } $cols = implode(', ', $colsArray); unset($colsArray); $sqlArray[] = '(SELECT' . ' ' . $cols . ' FROM ' . $sc . ' sc' . ' LEFT JOIN ' . $dg . ' dg ON dg.document = sc.id' . ' WHERE sc.id = ' . $docId . ' AND (' . $access . ')' . ' LIMIT 1)'; } if (count($sqlArray) == 0) { continue; } $sql = implode(' UNION ', $sqlArray) . ';'; // Grab the data from the database... $result = $this->itsMODx->db->query($sql); $count = $this->itsMODx->recordCount($result); // Set up find and replace arrays... $find = array(); $replace = array(); for ($j = 0; $j < $count; $j++) { $row = $this->itsMODx->db->getRow($result); $docId =& $row['id']; foreach ($dvInfo[$docId] as $dvName => $matches) { $escapedvalue = YamsUtils::PregQuoteReplacement($row[$dvName]); foreach ($matches as $match => $phx) { $find[] = $match; $replace[] = $escapedvalue; // TO DO: PHx stuff... } } // Delete the rows we have dealt with from the cache... unset($dvInfo[$docId]); } // Loop over the rows which haven't been dealt with. // These may not be accessible to the user, in which case, output // nothing foreach ($dvInfo as $docId => $docIdInfo) { foreach ($docIdInfo as $dvName => $matches) { foreach ($matches as $match => $phx) { $find[] = $match; $replace[] = ''; // TO DO: PHx stuff... } } } if (count($find) == 0) { continue; } $content = preg_replace($find, $replace, $content, -1, $nReplacements); if ($nReplacements > 0) { $contentChanged = TRUE; } } unset($dvInfo); return $contentChanged; }
/** * getTemplateVarOutput * @version 1.0.1 (2014-02-19) * * @desc Returns an associative array containing TV rendered output values. * * @param $idnames {array; '*'} - Which TVs to fetch - Can relate to the TV ids in the db (array elements should be numeric only) or the TV names (array elements should be names only). @required * @param $docid {integer; ''} - Id of a document to get. Default: an empty string which indicates the current document. * @param $published {0; 1; 'all'} - Document publication status. Once the parameter equals 'all', the result will be returned regardless of whether the ducuments are published or they are not. Default: 1. * @param $sep {string} - Separator that is used while concatenating in getTVDisplayFormat(). Default: ''. * * @return {array; false} - Result array, or false. */ function getTemplateVarOutput($idnames = array(), $docid = '', $published = 1, $sep = '') { if (count($idnames) == 0) { return false; } else { $output = array(); $vars = $idnames == '*' || is_array($idnames) ? $idnames : array($idnames); $docid = intval($docid) ? intval($docid) : $this->documentIdentifier; // remove sort for speed $result = $this->getTemplateVars($vars, '*', $docid, $published, '', ''); if ($result == false) { return false; } else { $baspath = MODX_MANAGER_PATH . 'includes'; include_once $baspath . '/tmplvars.format.inc.php'; include_once $baspath . '/tmplvars.commands.inc.php'; for ($i = 0; $i < count($result); $i++) { $row = $result[$i]; if (!$row['id']) { $output[$row['name']] = $row['value']; } else { $output[$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $docid, $sep); } } return $output; } } }
function appendTV($tvname, $docIDs) { global $modx; $baspath = MODX_MANAGER_PATH . "includes"; include_once $baspath . "/tmplvars.format.inc.php"; include_once $baspath . "/tmplvars.commands.inc.php"; $tbl_site_tmplvar_contentvalues = $modx->getFullTableName('site_tmplvar_contentvalues'); $tbl_site_tmplvars = $modx->getFullTableName('site_tmplvars'); $rs = $modx->db->select("stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value", "{$tbl_site_tmplvar_contentvalues} stc LEFT JOIN {$tbl_site_tmplvars} stv ON stv.id=stc.tmplvarid ", "stv.name='{$tvname}' AND stc.contentid IN (" . implode($docIDs, ",") . ")", "stc.contentid ASC"); $resourceArray = array(); while ($row = $modx->db->getRow($rs)) { $resourceArray["#{$row['contentid']}"][$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $row['contentid']); } if (count($resourceArray) != count($docIDs)) { $rs = $modx->db->select('name,type,display,display_params,default_text', $tbl_site_tmplvars, "name='{$tvname}'", 1); $row = $modx->db->getRow($rs); if (strtoupper($row['default_text']) == '@INHERIT') { foreach ($docIDs as $id) { $output = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'], $id); if (!isset($resourceArray["#{$id}"])) { $resourceArray["#{$id}"][$tvname] = $output; } } } else { $output = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type'], $row['contentid']); foreach ($docIDs as $id) { if (!isset($resourceArray["#{$id}"])) { $resourceArray["#{$id}"][$tvname] = $output; } } } } return $resourceArray; }
public function renderTV($tvname) { $out = null; if ($this->getID() > 0) { include_once MODX_MANAGER_PATH . "includes/tmplvars.format.inc.php"; include_once MODX_MANAGER_PATH . "includes/tmplvars.commands.inc.php"; $tvval = $this->get($tvname); $param = APIHelpers::getkey($this->tvd, $tvname, array()); $display = APIHelpers::getkey($param, 'display', ''); $display_params = APIHelpers::getkey($param, 'display_params', ''); $type = APIHelpers::getkey($param, 'type', ''); $out = getTVDisplayFormat($tvname, $tvval, $display, $display_params, $type, $this->getID(), ''); } return $out; }
function getTemplateVarOutput($idnames = array(), $docid = "", $published = 1, $sep = '') { if (count($idnames) == 0) { return false; } else { $output = array(); $vars = $idnames == '*' || is_array($idnames) ? $idnames : array($idnames); $docid = intval($docid) ? intval($docid) : $this->documentIdentifier; $result = $this->getTemplateVars($vars, "*", $docid, $published, "", "", $sep); // remove sort for speed if ($result == false) { return false; } else { $baspath = $this->config["base_path"] . "manager/includes"; include_once $baspath . "/tmplvars.format.inc.php"; include_once $baspath . "/tmplvars.commands.inc.php"; for ($i = 0; $i < count($result); $i++) { $row = $result[$i]; if (!$row['id']) { $output[$row['name']] = $row['value']; } else { $output[$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $docid, $sep); } } return $output; } } }
function appendTV($tvname, $docIDs) { global $modx; $baspath = $modx->config["base_path"] . "manager/includes"; include_once $baspath . "/tmplvars.format.inc.php"; include_once $baspath . "/tmplvars.commands.inc.php"; $tb1 = $modx->getFullTableName("site_tmplvar_contentvalues"); $tb2 = $modx->getFullTableName("site_tmplvars"); $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value"; $query .= " FROM " . $tb1 . " stc LEFT JOIN " . $tb2 . " stv ON stv.id=stc.tmplvarid "; $query .= " WHERE stv.name='" . $tvname . "' AND stc.contentid IN (" . implode($docIDs, ",") . ") ORDER BY stc.contentid ASC;"; $rs = $modx->db->query($query); $tot = $modx->db->getRecordCount($rs); $resourceArray = array(); for ($i = 0; $i < $tot; $i++) { $row = @$modx->fetchRow($rs); $resourceArray["#{$row['contentid']}"][$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $row['contentid']); } if ($tot != count($docIDs)) { $query = "SELECT name,type,display,display_params,default_text"; $query .= " FROM {$tb2}"; $query .= " WHERE name='" . $tvname . "' LIMIT 1"; $rs = $modx->db->query($query); $row = @$modx->fetchRow($rs); $defaultOutput = getTVDisplayFormat($row['name'], $row['default_text'], $row['display'], $row['display_params'], $row['type']); foreach ($docIDs as $id) { if (!isset($resourceArray["#{$id}"])) { $resourceArray["#{$id}"][$tvname] = $defaultOutput; } } } return $resourceArray; }
/** * * @param array $contentIds * @return array */ function getTmplVars($contentIds) { $templateVars = array(); if ($this->config['renderTVDisplayFormat']) { include_once MODX_MANAGER_PATH . "includes/tmplvars.format.inc.php"; include_once MODX_MANAGER_PATH . "includes/tmplvars.commands.inc.php"; } if (count($contentIds) > 0) { $query1 = "\r\n SELECT tv.name FROM " . $this->modx->getFullTableName('site_tmplvars') . " tv\r\n LEFT JOIN " . $this->modx->getFullTableName('site_tmplvar_templates') . " stvt ON stvt.tmplvarid = tv.id\r\n WHERE stvt.templateid IN (SELECT DISTINCT template FROM " . $this->config['tbl_catalog'] . " WHERE id IN (" . implode(',', $contentIds) . "))\r\n "; $tv_names_res = $this->modx->db->query($query1); $tv_names = array(); if ($this->modx->db->getRecordCount($tv_names_res) > 0) { $query2 = "\r\n SELECT tv.name, tvc.value, tvc.contentid, tv.display, tv.display_params, tv.type\r\n FROM " . $this->modx->getFullTableName('site_tmplvars') . " tv, " . $this->config['tbl_catalog_tv'] . " tvc\r\n WHERE tv.id = tvc.tmplvarid AND tvc.contentid IN (" . implode(',', $contentIds) . ")\r\n "; $tv_values_res = $this->modx->db->query($query2); if ($this->modx->db->getRecordCount($tv_values_res) === 0) { return $templateVars; } //get tv names while ($row = $this->modx->db->getRow($tv_names_res)) { $tv_names[] = $row['name']; } unset($row); //get tv values while ($row = $this->modx->db->getRow($tv_values_res)) { if ($this->config['renderTVDisplayFormat']) { $tvout = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $row['contentid']); $templateVars[$row['contentid']] = !isset($templateVars[$row['contentid']]) ? array($row['name'] => $tvout) : array_merge($templateVars[$row['contentid']], array($row['name'] => $tvout)); } else { $templateVars[$row['contentid']] = !isset($templateVars[$row['contentid']]) ? array($row['name'] => $row['value']) : array_merge($templateVars[$row['contentid']], array($row['name'] => $row['value'])); } } unset($row); //fill holes foreach ($templateVars as $key => &$val) { $hole = array_diff($tv_names, array_keys($val)); foreach ($hole as $k => $v) { $val[$v] = ''; } } } } return $templateVars; }
function getTemplateVarOutput($idnames = array(), $docid = '', $published = 1, $sep = '') { if (is_array($idnames) && count($idnames) == 0) { return false; } else { $output = array(); $vars = $idnames == '*' || is_array($idnames) ? $idnames : array($idnames); $docid = intval($docid) ? intval($docid) : $this->documentIdentifier; $result = $this->getTemplateVars($vars, '*', $docid, $published, '', ''); // remove sort for speed if ($result == false) { return false; } else { $core_path = $this->config['core_path']; include_once "{$core_path}tmplvars.format.inc.php"; include_once "{$core_path}tmplvars.commands.inc.php"; foreach ($result as $row) { if (!$row['id']) { $output[$row['name']] = $row['value']; } else { $output[$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $docid, $sep); } } return $output; } } }
/** * getTemplateVarOutput * @version 1.0 (2013-03-16) * * @desc Returns the associative array of fields and TVs of a document. * * @note * Differences from the native method: * — $published parameter can be set as false, and if it is then document publication status does not matter. * * @param $idnames {array; '*'} - Id, TVs names, or documents fields to get. @required * @param $docid {integer; ''} - Id of a document to get. Default: Current document. * @param $published {false; 0; 1} - Document publication status which does not matter if published === false. Default: false. * @param $sep {string} - Separator that is used while concatenating in getTVDisplayFormat(). Default: ''. * * @return {mixed} - Массив TV или false, если что-то не так. */ public static function getTemplateVarOutput($idnames = array(), $docid = "", $published = false, $sep = '') { global $modx; if (count($idnames) == 0) { return false; } else { $output = array(); $vars = $idnames == '*' || is_array($idnames) ? $idnames : array($idnames); $docid = intval($docid) ? intval($docid) : $modx->documentIdentifier; $result = self::getTemplateVars($vars, '*', $docid, $published, '', ''); // remove sort for speed if ($result == false) { return false; } else { $baspath = $modx->config['base_path'] . 'manager/includes'; include_once $baspath . '/tmplvars.format.inc.php'; include_once $baspath . '/tmplvars.commands.inc.php'; for ($i = 0; $i < count($result); $i++) { $row = $result[$i]; if (!$row['id']) { $output[$row['name']] = $row['value']; } else { $output[$row['name']] = getTVDisplayFormat($row['name'], $row['value'], $row['display'], $row['display_params'], $row['type'], $docid, $sep); } } return $output; } } }