versionx.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /**
  3. * VersionX
  4. *
  5. * Copyright 2011 by Mark Hamstra <hello@markhamstra.com>
  6. *
  7. * This file is part of VersionX, a real estate property listings component
  8. * for MODX Revolution.
  9. *
  10. * VersionX is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * VersionX is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  17. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * VersionX; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  21. * Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * @package versionx
  24. */
  25. class VersionX {
  26. public $modx;
  27. private $chunks;
  28. private $tvs = array();
  29. public $config = array();
  30. public $categoryCache = array();
  31. public $debug = false;
  32. public $charset;
  33. /**
  34. * @param \modX $modx
  35. * @param array $config
  36. */
  37. function __construct(modX $modx,array $config = array()) {
  38. $this->modx =& $modx;
  39. $basePath = $this->modx->getOption('versionx.core_path',$config,$this->modx->getOption('core_path').'components/versionx/');
  40. $assetsUrl = $this->modx->getOption('versionx.assets_url',$config,$this->modx->getOption('assets_url').'components/versionx/');
  41. $assetsPath = $this->modx->getOption('versionx.assets_path',$config,$this->modx->getOption('assets_path').'components/versionx/');
  42. $this->config = array_merge(array(
  43. 'base_bath' => $basePath,
  44. 'core_path' => $basePath,
  45. 'model_path' => $basePath.'model/',
  46. 'processors_path' => $basePath.'processors/',
  47. 'elements_path' => $basePath.'elements/',
  48. 'templates_path' => $basePath.'templates/',
  49. 'assets_path' => $assetsPath,
  50. 'js_url' => $assetsUrl.'js/',
  51. 'css_url' => $assetsUrl.'css/',
  52. 'assets_url' => $assetsUrl,
  53. 'connector_url' => $assetsUrl.'connector.php',
  54. 'has_users_permission' => $this->modx->hasPermission('view_user'),
  55. ),$config);
  56. require_once dirname(__DIR__) . '/docs/version.inc.php';
  57. if (defined('VERSIONX_FULLVERSION')) {
  58. $this->config['version'] = VERSIONX_FULLVERSION;
  59. }
  60. $modelPath = $this->config['model_path'];
  61. $this->modx->addPackage('versionx', $modelPath);
  62. $this->modx->lexicon->load('versionx:default');
  63. $this->debug = $this->modx->getOption('versionx.debug',null,false);
  64. }
  65. /**
  66. * Gets a Chunk and caches it; also falls back to file-based templates
  67. * for easier debugging.
  68. *
  69. * @access public
  70. * @param string $name The name of the Chunk
  71. * @param array $properties The properties for the Chunk
  72. * @return string The processed content of the Chunk
  73. * @author Shaun "splittingred" McCormick
  74. */
  75. public function getChunk($name,$properties = array()) {
  76. $chunk = null;
  77. if (!isset($this->chunks[$name])) {
  78. $chunk = $this->_getTplChunk($name);
  79. if (empty($chunk)) {
  80. $chunk = $this->modx->getObject('modChunk',array('name' => $name),true);
  81. if ($chunk == false) return false;
  82. }
  83. $this->chunks[$name] = $chunk->getContent();
  84. } else {
  85. $o = $this->chunks[$name];
  86. $chunk = $this->modx->newObject('modChunk');
  87. $chunk->setContent($o);
  88. }
  89. $chunk->setCacheable(false);
  90. return $chunk->process($properties);
  91. }
  92. /**
  93. * Returns a modChunk object from a template file.
  94. *
  95. * @access private
  96. * @param string $name The name of the Chunk. Will parse to name.chunk.tpl
  97. * @param string $postFix The postfix to append to the name
  98. * @return modChunk/boolean Returns the modChunk object if found, otherwise false.
  99. * @author Shaun "splittingred" McCormick
  100. */
  101. private function _getTplChunk($name,$postFix = '.tpl') {
  102. $chunk = false;
  103. $f = $this->config['elements_path'].'chunks/'.strtolower($name).$postFix;
  104. if (file_exists($f)) {
  105. $o = file_get_contents($f);
  106. /* @var modChunk $chunk */
  107. $chunk = $this->modx->newObject('modChunk');
  108. $chunk->set('name',$name);
  109. $chunk->setContent($o);
  110. }
  111. return $chunk;
  112. }
  113. public function newVersionFor($class, $contentId, $mode)
  114. {
  115. switch ($class) {
  116. case 'vxResource':
  117. return $this->newResourceVersion($contentId, $mode);
  118. case 'vxTemplate':
  119. return $this->newTemplateVersion($contentId, $mode);
  120. case 'vxChunk':
  121. return $this->newChunkVersion($contentId, $mode);
  122. case 'vxSnippet':
  123. return $this->newSnippetVersion($contentId, $mode);
  124. case 'vxPlugin':
  125. return $this->newPluginVersion($contentId, $mode);
  126. case 'vxTemplateVar':
  127. return $this->newTemplateVarVersion($contentId, $mode);
  128. }
  129. $this->modx->log(modX::LOG_LEVEL_ERROR, 'Call to ' . __METHOD__ . ' with unrecognised class ' . $class);
  130. return false;
  131. }
  132. /**
  133. * Creates a new version of a Resource.
  134. *
  135. * @param int|modResource|modStaticResource $resource
  136. * @param string $mode
  137. * @return bool
  138. *
  139. */
  140. public function newResourceVersion($resource, $mode = 'upd') {
  141. if ($resource instanceof modResource) {
  142. // We're retrieving the resource again to clean up raw post data we don't want.
  143. $resource = $this->modx->getObject('modResource',$resource->get('id'));
  144. } else {
  145. $resource = $this->modx->getObject('modResource',(int)$resource);
  146. }
  147. $rArray = $resource->toArray();
  148. /* @var vxResource $version */
  149. $version = $this->modx->newObject('vxResource');
  150. $v = array(
  151. 'content_id' => $rArray['id'],
  152. 'user' => $this->modx->user->get('id'),
  153. 'mode' => $mode,
  154. 'title' => $rArray[$this->modx->getOption('resource_tree_node_name',null,'pagetitle')],
  155. 'context_key' => $rArray['context_key'],
  156. 'class' => $rArray['class_key'],
  157. 'content' => $resource->get('content'),
  158. );
  159. $version->fromArray($v);
  160. unset ($rArray['id'],$rArray['content']);
  161. $version->set('fields',$rArray);
  162. $tvs = $resource->getTemplateVars();
  163. $tvArray = array();
  164. /* @var modTemplateVar $tv */
  165. foreach ($tvs as $tv) {
  166. $tvArray[] = $tv->get(array('id','value'));
  167. }
  168. $version->set('tvs',$tvArray);
  169. if($this->checkLastVersion('vxResource', $version)) {
  170. return $version->save();
  171. }
  172. return true;
  173. }
  174. /**
  175. * Creates a new version of a Template.
  176. *
  177. * @param int|\modTemplate $template
  178. * @param string $mode
  179. * @return bool
  180. *
  181. */
  182. public function newTemplateVersion($template, $mode = 'upd') {
  183. if ($template instanceof modTemplate) {
  184. /* Fetch it again to prevent getting stuck with raw post data */
  185. $template = $this->modx->getObject('modTemplate', $template->get('id'));
  186. } else {
  187. $template = $this->modx->getObject('modTemplate', (int)$template);
  188. }
  189. $tArray = $template->toArray();
  190. /* @var vxTemplate $version */
  191. $version = $this->modx->newObject('vxTemplate');
  192. $v = array(
  193. 'content_id' => $tArray['id'],
  194. 'user' => $this->modx->user->get('id'),
  195. 'mode' => $mode,
  196. );
  197. $version->fromArray(array_merge($v,$tArray));
  198. if($this->checkLastVersion('vxTemplate', $version)) {
  199. return $version->save();
  200. }
  201. return true;
  202. }
  203. /**
  204. * Creates a new version of a Template Variable.
  205. *
  206. * @param int|\modTemplateVar $tv
  207. * @param string $mode
  208. * @return bool
  209. *
  210. */
  211. public function newTemplateVarVersion($tv, $mode = 'upd') {
  212. if ($tv instanceof modTemplateVar) {
  213. /* Fetch it again to prevent getting stuck with raw post data */
  214. $tv = $this->modx->getObject('modTemplateVar', $tv->get('id'));
  215. } else {
  216. $tv = $this->modx->getObject('modTemplateVar', (int)$tv);
  217. }
  218. $tArray = $tv->toArray();
  219. /* @var modTemplateVar $version */
  220. $version = $this->modx->newObject('vxTemplateVar');
  221. $v = array(
  222. 'content_id' => $tArray['id'],
  223. 'user' => $this->modx->user->get('id'),
  224. 'mode' => $mode,
  225. );
  226. $version->fromArray(array_merge($v,$tArray));
  227. if($this->checkLastVersion('vxTemplateVar', $version)) {
  228. return $version->save();
  229. }
  230. return true;
  231. }
  232. /**
  233. * Create a new version of a Chunk.
  234. *
  235. * @param int|\modChunk $chunk
  236. * @param string $mode
  237. * @return bool
  238. */
  239. public function newChunkVersion($chunk, $mode = 'upd') {
  240. if ($chunk instanceof modChunk) {
  241. /* Fetch it again to prevent getting stuck with raw post data */
  242. $chunk = $this->modx->getObject('modChunk', $chunk->get('id'));
  243. } else {
  244. $chunk = $this->modx->getObject('modChunk', (int)$chunk);
  245. }
  246. // prevents resource groups from failing in MODX versions prior to 2.2.14 (see github #8992 + fix)
  247. if (!($chunk instanceof modChunk)) {
  248. return false;
  249. }
  250. $cArray = $chunk->toArray();
  251. /* @var vxChunk $version */
  252. $version = $this->modx->newObject('vxChunk');
  253. $v = array(
  254. 'content_id' => $cArray['id'],
  255. 'user' => $this->modx->user->get('id'),
  256. 'mode' => $mode,
  257. );
  258. $version->fromArray(array_merge($v,$cArray));
  259. if($this->checkLastVersion('vxChunk', $version)) {
  260. return $version->save();
  261. }
  262. return true;
  263. }
  264. /**
  265. * Creates a new version of a Snippet.
  266. *
  267. * @param int|\modSnippet $snippet
  268. * @param string $mode
  269. * @return bool
  270. */
  271. public function newSnippetVersion($snippet, $mode = 'upd') {
  272. if ($snippet instanceof modSnippet) {
  273. /* Fetch it again to prevent getting stuck with raw post data */
  274. $snippet = $this->modx->getObject('modSnippet', $snippet->get('id'));
  275. } else {
  276. $snippet = $this->modx->getObject('modSnippet', (int)$snippet);
  277. }
  278. $sArray = $snippet->toArray();
  279. /* @var vxSnippet $version */
  280. $version = $this->modx->newObject('vxSnippet');
  281. $v = array(
  282. 'content_id' => $sArray['id'],
  283. 'user' => $this->modx->user->get('id'),
  284. 'mode' => $mode,
  285. );
  286. $version->fromArray(array_merge($v,$sArray));
  287. if($this->checkLastVersion('vxSnippet', $version)) {
  288. return $version->save();
  289. }
  290. return true;
  291. }
  292. /**
  293. * Creates a new version of a Plugin.
  294. *
  295. * @param int|\modPlugin $plugin
  296. * @param string $mode
  297. * @return bool
  298. */
  299. public function newPluginVersion($plugin, $mode = 'upd') {
  300. if ($plugin instanceof modPlugin) {
  301. /* Fetch it again to prevent getting stuck with raw post data */
  302. $plugin = $this->modx->getObject('modPlugin', $plugin->get('id'));
  303. } else {
  304. $plugin = $this->modx->getObject('modPlugin', (int)$plugin);
  305. }
  306. $pArray = $plugin->toArray();
  307. /* @var vxPlugin $version */
  308. $version = $this->modx->newObject('vxPlugin');
  309. $v = array(
  310. 'content_id' => $pArray['id'],
  311. 'user' => $this->modx->user->get('id'),
  312. 'mode' => $mode,
  313. );
  314. $version->fromArray(array_merge($v,$pArray));
  315. if($this->checkLastVersion('vxPlugin', $version)) {
  316. return $version->save();
  317. }
  318. return true;
  319. }
  320. /**
  321. * Gets & prepares version details for output.
  322. *
  323. * @param string $class
  324. * @param int $id
  325. * @param bool $json
  326. * @param string $prefix
  327. * @return bool|array
  328. */
  329. public function getVersionDetails($class = 'vxResource',$id = 0, $json = false, $prefix = '') {
  330. $v = $this->modx->getObject($class, ['version_id' => $id]);
  331. /* @var xPDOObject $v */
  332. if ($v instanceof $class) {
  333. $vArray = $v->toArray();
  334. $vArray['mode'] = $this->modx->lexicon('versionx.mode.'.$vArray['mode']);
  335. /* Class specific processing */
  336. switch ($class) {
  337. case 'vxResource':
  338. $vArray = array_merge($vArray,$vArray['fields']);
  339. if ($vArray['parent'] != 0) {
  340. /* @var modResource $parent */
  341. $parent = $this->modx->getObject('modResource',$vArray['parent']);
  342. if ($parent instanceof modResource) $vArray['parent'] = $parent->get('pagetitle') .' ('.$vArray['parent'].')';
  343. }
  344. /* Process content type */
  345. /* @var modContentType $ct */
  346. $ct = $this->modx->getObject('modContentType',$vArray['content_type']);
  347. if ($ct instanceof modContentType)
  348. $vArray['content_type'] = $ct->get('name');
  349. $vArray['content'] = $this->_prepareCodeView($vArray['content']);
  350. if ($vArray['content_dispo'] == 1) $vArray['content_dispo'] = $this->modx->lexicon('attachment');
  351. else $vArray['content_dispo'] = $this->modx->lexicon('inline');
  352. /* Process boolean values */
  353. $vArray['published'] = (intval($vArray['published'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  354. $vArray['hidemenu'] = (intval($vArray['hidemenu'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  355. $vArray['isfolder'] = (intval($vArray['isfolder'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  356. $vArray['richtext'] = (intval($vArray['richtext'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  357. $vArray['searchable'] = (intval($vArray['searchable'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  358. $vArray['cacheable'] = (intval($vArray['cacheable'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  359. $vArray['deleted'] = (intval($vArray['deleted'])) ? $this->modx->lexicon('yes') : $this->modx->lexicon('no');
  360. /* Process time stamps */
  361. $df = $this->modx->config['manager_date_format'].' '.$this->modx->config['manager_time_format'];
  362. $vArray['saved'] = ($vArray['saved'] != 0) ? date($df,strtotime($vArray['saved'])) : '';
  363. $vArray['publishedon'] = ($vArray['publishedon'] != 0) ? date($df,strtotime($vArray['publishedon'])) : '';
  364. $vArray['pub_date'] = ($vArray['pub_date'] != 0) ? date($df,strtotime($vArray['pub_date'])) : '';
  365. $vArray['unpub_date'] = ($vArray['unpub_date'] != 0) ? date($df,strtotime($vArray['unpub_date'])) : '';
  366. /* Get TV captions */
  367. $tvArray = array();
  368. foreach ($vArray['tvs'] as $tv) {
  369. if (!isset($this->tvs[$tv['id']]) || empty($this->tvs[$tv['id']])) {
  370. /* @var modTemplateVar $tvObj */
  371. $tvObj = $this->modx->getObject('modTemplateVar',$tv['id']);
  372. if ($tvObj instanceof modTemplateVar) {
  373. $caption = $tvObj->get('caption');
  374. if (empty($caption)) $caption = $tvObj->get('name');
  375. $this->tvs[$tv['id']] = $caption;
  376. } else {
  377. $this->tvs[$tv['id']] = 'tv'.$tv['id'];
  378. }
  379. }
  380. $tvArray[] = array_merge($tv,array('caption' => $this->tvs[$tv['id']]));
  381. }
  382. $vArray['tvs'] = $tvArray;
  383. break;
  384. case 'vxTemplateVar':
  385. $vArray['category'] = $this->getCategory($vArray['category']);
  386. if (is_array($vArray['input_properties'])) {
  387. foreach ($vArray['input_properties'] as $key => $value) {
  388. if ($decoded = $this->modx->fromJSON($value)) {
  389. $vArray['input_properties'][$key] = $decoded;
  390. }
  391. }
  392. }
  393. if (is_array($vArray['output_properties'])) {
  394. foreach ($vArray['output_properties'] as $key => $value) {
  395. if ($decoded = $this->modx->fromJSON($value)) {
  396. $vArray['output_properties'][$key] = $decoded;
  397. }
  398. }
  399. }
  400. break;
  401. case 'vxTemplate':
  402. $vArray['content'] = $this->_prepareCodeView($vArray['content']);
  403. $vArray['category'] = $this->getCategory($vArray['category']);
  404. break;
  405. case 'vxChunk':
  406. $vArray['snippet'] = $this->_prepareCodeView($vArray['snippet']);
  407. $vArray['category'] = $this->getCategory($vArray['category']);
  408. break;
  409. case 'vxSnippet':
  410. $vArray['snippet'] = $this->_prepareCodeView($vArray['snippet']);
  411. $vArray['category'] = $this->getCategory($vArray['category']);
  412. break;
  413. case 'vxPlugin':
  414. $vArray['plugincode'] = $this->_prepareCodeView($vArray['plugincode']);
  415. $vArray['category'] = $this->getCategory($vArray['category']);
  416. break;
  417. }
  418. /* @var modUserProfile $up */
  419. $up = $this->modx->getObject('modUserProfile',array('internalKey' => $vArray['user']));
  420. if ($up instanceof modUserProfile) $vArray['user'] = $up->get('fullname');
  421. if (!empty($prefix)) {
  422. $ta = array();
  423. foreach ($vArray as $tk => $tv) {
  424. $ta[$prefix.$tk] = $tv;
  425. }
  426. $vArray = $ta;
  427. }
  428. if ($json) return $this->modx->toJSON($vArray);
  429. return $vArray;
  430. }
  431. return false;
  432. }
  433. /**
  434. * @param $string
  435. *
  436. * @return string
  437. */
  438. private function _prepareCodeView($string) {
  439. $lines = explode("\n",$string);
  440. foreach ($lines as $idx => $line) {
  441. $pos = 0;
  442. while( substr($line, $pos, 1) == ' ') {
  443. $pos++;
  444. }
  445. $lines[$idx] = str_repeat('&nbsp;', $pos) . $this->htmlent(substr($line, $pos));
  446. }
  447. $lines = implode("<br />\n", $lines);
  448. return $lines;
  449. }
  450. /**
  451. * Checks the last saved version (if any).
  452. * Returns true if there is no earlier version, or something is different.
  453. * So if this returns true: go ahead and save the version.
  454. * If this returns false: nothing changed, don't bother.
  455. *
  456. * @param string $class
  457. * @param \xPDOObject $version
  458. *
  459. * @return bool
  460. */
  461. protected function checkLastVersion($class = 'vxResource', xPDOObject $version) {
  462. /* Get last version to make sure we've got some changes to save */
  463. $c = $this->modx->newQuery($class);
  464. $c->where(array('content_id' => $version->get('content_id')));
  465. $c->sortby('version_id','DESC');
  466. $c->limit(1);
  467. $lastVersion = $this->modx->getCollection($class,$c);
  468. $lastVersion = !empty($lastVersion) ? array_shift($lastVersion) : array();
  469. /* @var vxResource $lastVersion */
  470. /* If there's no earlier version, we can go ahead and
  471. return true to indicate we need to save the version */
  472. if (!($lastVersion instanceof $class)) {
  473. if ($this->debug) $this->modx->log(xPDO::LOG_LEVEL_ERROR,"[VersionX] Saving a {$class} for ID {$version->get('content_id')}: No earlier version found.");
  474. return true;
  475. }
  476. $newVersionArray = $version->toArray();
  477. $lastVersionArray = $lastVersion->toArray();
  478. /* Get rid of excluded vars for the specific object. */
  479. $exclude = call_user_func(array($class,'getExcludeFields'));
  480. if ($this->debug) $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX checkLastVersion] Exclude fields: ' . print_r($exclude, true));
  481. foreach ($exclude as $key => $value) {
  482. if (is_array($value)) {
  483. foreach ($value as $subfield) {
  484. if (isset($newVersionArray[$key]) && isset($newVersionArray[$key][$subfield])) {
  485. unset ($newVersionArray[$key][$subfield]);
  486. }
  487. if (isset($lastVersionArray[$key]) && isset($lastVersionArray[$key][$subfield])) {
  488. unset ($lastVersionArray[$key][$subfield]);
  489. }
  490. }
  491. } else {
  492. if (isset($lastVersionArray[$value])) { unset($lastVersionArray[$value]); }
  493. if (isset($newVersionArray[$value])) { unset($newVersionArray[$value]); }
  494. }
  495. }
  496. $newVersionFlat = $this->flattenArray($newVersionArray);
  497. $lastVersionFlat = $this->flattenArray($lastVersionArray);
  498. if ($this->debug) {
  499. $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX checkLastVersion] New: ' . print_r($newVersionArray, true));
  500. $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX checkLastVersion] New Flattened: ' . $newVersionFlat);
  501. $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX checkLastVersion] Last: ' . print_r($lastVersionArray, true));
  502. $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX checkLastVersion] Last Flattened: ' . $newVersionFlat);
  503. }
  504. /* If the flattened arrays don't match there's a difference and we return true to indicate we need to save. */
  505. if ($newVersionFlat != $lastVersionFlat) {
  506. return true;
  507. }
  508. if ($this->debug) $this->modx->log(xPDO::LOG_LEVEL_ERROR,"[VersionX] Not saving a {$class} for ID {$version->get('content_id')}: No changes found.");
  509. /* If we got here, there was a last version but it seemed nothing changes.
  510. Return false to indicate to NOT save a new version. */
  511. return false;
  512. }
  513. /**
  514. * Flattens an array recursively.
  515. * @param array $array
  516. *
  517. * @return array|string
  518. */
  519. public function flattenArray(array $array = array()) {
  520. if (!is_array($array)) return (string)$array;
  521. $string = array();
  522. foreach ($array as $key => $value) {
  523. if (is_array($value)) {
  524. $value = '{' . $this->flattenArray($value) .'}';
  525. }
  526. if (!empty($value)) {
  527. $string[] = $key . ':' . $value;
  528. }
  529. }
  530. $string = implode(',',$string);
  531. return $string;
  532. }
  533. /**
  534. * Outputs the JavaScript needed to add a tab to the panels.
  535. *
  536. * @param string $class
  537. */
  538. public function outputVersionsTab ($class = 'vxResource') {
  539. if (!class_exists($class)) {
  540. $path = $this->config['model_path'].'versionx/'.strtolower($class).'.class.php';
  541. if (file_exists($path)) {
  542. require_once ($path);
  543. }
  544. if (!class_exists($class)) {
  545. $this->modx->log(modX::LOG_LEVEL_ERROR,'[VersionX::outputVersionsTab] Error loading class '.$class);
  546. return;
  547. }
  548. }
  549. $langs = $this->_getLangs();
  550. $jsurl = $this->config['js_url'].'mgr/';
  551. /* Load class & set inVersion to true, indicating we're not looking at the VersionX controller. */
  552. $this->modx->regClientStartupScript($jsurl.'versionx.class.js');
  553. $this->modx->regClientStartupHTMLBlock('
  554. <script type="text/javascript">
  555. VersionX.config = '.$this->modx->toJSON($this->config).';
  556. VersionX.inVersion = true;
  557. '.$langs.'
  558. </script>
  559. ');
  560. /* Get the different individual JS to add to the page */
  561. $tabjs = call_user_func(array($class,'getTabJavascript'));
  562. if (is_array($tabjs)) {
  563. foreach ($tabjs as $js) {
  564. $this->modx->regClientStartupScript($jsurl.$js);
  565. }
  566. }
  567. /* Get the template and register it */
  568. $tplName = call_user_func(array($class,'getTabTpl'));
  569. $tplFile = $this->config['templates_path'] . $tplName . '.tpl';
  570. if (file_exists($tplFile)) {
  571. $tpl = file_get_contents($tplFile);
  572. if (!empty($tpl)) {
  573. $this->modx->regClientStartupHTMLBlock($tpl);
  574. }
  575. }
  576. }
  577. /**
  578. * Gets language strings for use on non-VersionX controllers.
  579. * @return string
  580. */
  581. public function _getLangs() {
  582. $entries = $this->modx->lexicon->loadCache('versionx');
  583. $langs = 'Ext.applyIf(MODx.lang,' . $this->modx->toJSON($entries) . ');';
  584. return $langs;
  585. }
  586. /**
  587. * @param $id
  588. *
  589. * @return string
  590. */
  591. public function getCategory($id) {
  592. if (!$id || $id == 0) return '';
  593. if (isset($this->categoryCache[$id])) {
  594. return $this->categoryCache[$id];
  595. }
  596. /* @var modCategory $category */
  597. $category = $this->modx->getObject('modCategory',(int)$id);
  598. if ($category) {
  599. return $this->categoryCache[$id] = $category->get('category') . " ($id)";
  600. } else {
  601. return (string)$id;
  602. }
  603. }
  604. /**
  605. * Runs htmlentities() on the string with the proper character encoding.
  606. * @param string $string
  607. * @return string
  608. */
  609. public function htmlent($string = '') {
  610. if ($this->charset === null) {
  611. $this->charset = $this->modx->getOption('modx_charset', null, 'UTF-8');
  612. }
  613. return htmlentities($string, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset);
  614. }
  615. }