Catalyz.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. /**
  3. * Used to make ORM objects interact efficiently with the CMS
  4. */
  5. interface ICatalyzContentProvider {
  6. public function updateFromRequest($node, $request, $previewMode);
  7. public function getValidSubClasses();
  8. public static function getNodeClassCaption();
  9. public static function getNodeClassDescription();
  10. static public function getLayoutAreas();
  11. }
  12. /**
  13. * Used to add more customization capabilities to the CMS from objects
  14. */
  15. interface ICatalyzAdvancedContentProvider extends ICatalyzContentProvider
  16. {
  17. public function needsLayout($node, $request);
  18. public function getTemplateName($node, $request);
  19. }
  20. /**
  21. * Used to define that a node is always hidden from menu.
  22. *
  23. * Example:
  24. * - News
  25. */
  26. interface ICatalyzHiddenNode extends ICatalyzContentProvider
  27. {
  28. }
  29. interface ICatalyzSubNode extends ICatalyzContentProvider {
  30. }
  31. interface ICatalyzSlotProvider extends ICatalyzContentProvider {
  32. public function getSlot($name);
  33. }
  34. interface ICatalyzTranslatable extends ICatalyzContentProvider
  35. {
  36. public function removeTranslation($language);
  37. public function createTranslation($newLanguage);
  38. }
  39. interface ICatalyzRedirectToParentNodeOnceCreated extends ICatalyzContentProvider
  40. {
  41. }
  42. interface ICatalyzInsertOnTop extends ICatalyzContentProvider {
  43. }
  44. /**
  45. * Allow nodes to control action permissions
  46. */
  47. interface ICatalyzToolbarActionManager extends ICatalyzContentProvider {
  48. public function canAddChild($default);
  49. public function canEdit();
  50. public function canReorderChildren($default);
  51. public function canDelete();
  52. }
  53. /**
  54. * Allow nodes to add new actions in Catalyz Toolbar
  55. */
  56. interface ICatalyzToolbarActionProvider extends ICatalyzContentProvider
  57. {
  58. /**
  59. * Return all actions to include in Catalyz Toolbar
  60. *
  61. * @return array With the following keys:
  62. * - key: unique action identifier (used to add another action near it)
  63. * - url: when to redirect user to
  64. * - caption: caption to display in the toolbar
  65. * - before/after: name of action where to insert the action
  66. *
  67. * Example: array(array(
  68. * 'key' => 'foo',
  69. * 'caption' => 'Foo',
  70. * 'url' => 'http:///www.foobar.com',
  71. * 'after' => 'edit'
  72. *
  73. * Standard actions keys are:
  74. * - add
  75. * - edit
  76. * - translation
  77. * - reorder
  78. * - delete
  79. */
  80. public function getToolbarActions();
  81. }
  82. /**
  83. * Allow nodes to tweak action names
  84. *
  85. * Actions can return false to use default value
  86. */
  87. interface ICatalyzToolbarActionStringManager extends ICatalyzContentProvider
  88. {
  89. public function getCatalyzToolbarAddCaption();
  90. public function getCatalyzToolbarReorderCaption();
  91. }
  92. /**
  93. * Allow nodes to define new actions in the Edit page
  94. */
  95. interface ICatalyzEditActionManager extends ICatalyzContentProvider
  96. {
  97. public function getEditActions();
  98. public function handleEditActionIfAny($page, $node, $request);
  99. }
  100. interface ICatalyzLayoutProvider extends ICatalyzContentProvider
  101. {
  102. public function getAvailableLayouts();
  103. }
  104. interface ICatalyzContentEditStringManager extends ICatalyzContentProvider
  105. {
  106. public function getCatalyzContentTitleCaption();
  107. }
  108. interface ICatalyzDefaultContentProvider extends ICatalyzContentProvider
  109. {
  110. public function getDefaultContent($field);
  111. }
  112. interface ICatalyzContentSearchable extends ICatalyzContentProvider
  113. {
  114. public function getNodeTitle();
  115. public static function createSearchService();
  116. }
  117. /**
  118. * Used to define that node can handle different contexts based on request
  119. * content, using the initializeContext method object will be able to
  120. * initialize itself and display the right content.
  121. *
  122. * Exemple:
  123. * - NewsList can offer last X news by default and achive of a specific month on request
  124. */
  125. interface ICatalyzContextualNode extends ICatalyzContentProvider
  126. {
  127. const VIEW = 1;
  128. const EDIT = 2;
  129. public function initializeContext($request, $mode);
  130. }
  131. interface ICatalyzUrlProvider
  132. {
  133. static public function getUrl($ContentTreeNode, $defaultUrl);
  134. }
  135. interface ICatalyzOpenTargetController
  136. {
  137. static public function getOpenTarget($ContentTreeNode);
  138. }
  139. class catalyzPageActions extends sfActions
  140. {
  141. public function executeIndex($request)
  142. {
  143. $this->forward404();
  144. }
  145. public function executeView($request)
  146. {
  147. $this->page = sfContext::getInstance()->get('page');
  148. $this->setLayout($this->page->getNode()->getCurrentLayoutNameForAction());
  149. $this->getResponse()->setTitle($this->page->getNode()->getBrowserTitle());
  150. return sfView::SUCCESS;
  151. }
  152. }
  153. class ContentTreeNodeIterator {
  154. /**
  155. *
  156. * @var ContentTree
  157. */
  158. var $ContentTree;
  159. /**
  160. *
  161. * @var ContentTreeNode
  162. */
  163. var $current;
  164. var $started = false;
  165. function __construct($ContentTree)
  166. {
  167. $this->ContentTree = $ContentTree;
  168. $this->rewind();
  169. }
  170. public function key()
  171. {
  172. return $this->current->getId();
  173. }
  174. public function valid()
  175. {
  176. return !empty($this->current);
  177. }
  178. protected function getNextNode($node)
  179. {
  180. if (!$node) {
  181. return null;
  182. }
  183. if (count($node->children) > 0) {
  184. return $node->children[0];
  185. }
  186. if ($node->nextSibling) {
  187. return $node->nextSibling;
  188. }
  189. $parentNode = $node->parent;
  190. while ($parentNode) {
  191. if ($parentNode->nextSibling) {
  192. return $parentNode->nextSibling;
  193. }
  194. $parentNode = $parentNode->parent;
  195. }
  196. return null;
  197. }
  198. public function next()
  199. {
  200. if($this->started){
  201. $this->current = $this->getNextNode($this->current);
  202. }else{
  203. $this->started = true;
  204. }
  205. return $this->current;
  206. }
  207. public function rewind()
  208. {
  209. $this->started = false;
  210. $this->current = $this->ContentTree->getRoot();
  211. }
  212. public function current()
  213. {
  214. return $this->current;
  215. }
  216. }
  217. class FilteredContentTreeNodeIterator extends ContentTreeNodeIterator {
  218. function __construct($ContentTree, $validClasses = array())
  219. {
  220. $this->validClasses = $validClasses;
  221. parent::__construct($ContentTree);
  222. }
  223. var $validClasses;
  224. protected function getNextNode($node)
  225. {
  226. $result = $node;
  227. do {
  228. $result = parent::getNextNode($result);
  229. } while ($result && !$this->isNodeValid($result));
  230. return $result;
  231. }
  232. protected function isNodeValid($node){
  233. //var_dump($node->getNodeClass());
  234. return $node && in_array($node->getNodeClass(), $this->validClasses);
  235. }
  236. public function rewind()
  237. {
  238. $this->current = $this->ContentTree->getRoot();
  239. if(!$this->isNodeValid($this->current)){
  240. $this->current = $this->getNextNode($this->current);
  241. }
  242. $this->started = false;
  243. }
  244. }
  245. class Catalyz {
  246. static protected $pageCulture;
  247. static function getPageCulture($long_format = false)
  248. {
  249. if ($long_format) {
  250. return sprintf('%s_%s', self::$pageCulture, strtoupper(self::$pageCulture));
  251. }
  252. return self::$pageCulture;
  253. }
  254. static function setPageCulture($value)
  255. {
  256. self::$pageCulture = $value;
  257. }
  258. static public function getPageByNodeId($nodeId)
  259. {
  260. $ContentTree = ContentTree::instance();
  261. assert($ContentTree);
  262. $peerName = sprintf('Node%sPeer', $ContentTree->getContentTreeNodeById($nodeId)->getNodeClass());
  263. $peer = new $peerName();
  264. $criteria = new Criteria();
  265. $fieldName = eval(sprintf('return %s::NODE_ID;', $peerName));
  266. $criteria->add($fieldName, $nodeId);
  267. $criteria->setLimit(1);
  268. // $page = $peer->doSelectOne($criteria);
  269. $pages = $peer->doSelect($criteria); //WithI18N
  270. $page = array_shift($pages);
  271. // var_dump($peer);exit;
  272. return $page;
  273. }
  274. static $cache = array();
  275. static public function getPageByNodeIdAndClass($nodeId, $nodeClass, $culture = null)
  276. {
  277. if (!isset(self::$cache[$nodeId])) {
  278. $peerName = sprintf('Node%sPeer', $nodeClass);
  279. $peer = new $peerName();
  280. $criteria = new Criteria();
  281. $fieldName = eval(sprintf('return %s::NODE_ID;', $peerName));
  282. // var_dump($fieldName);exit;
  283. $criteria->add($fieldName, $nodeId);
  284. if (method_exists($peer, 'doSelectWithI18N')) {
  285. $criteria->setLimit(1);
  286. if (null == $culture) {
  287. $culture = sfConfig::get('app_translations_default_language');
  288. }
  289. $pages = $peer->doSelectWithI18N($criteria, $culture);
  290. $page = array_shift($pages);
  291. } else {
  292. $page = $peer->doSelectOne($criteria);
  293. }
  294. self::$cache[$nodeId] = $page;
  295. }
  296. // var_dump(method_exists($peer, 'doSelectWithI18N'));exit;
  297. return self::$cache[$nodeId];
  298. }
  299. static public function getPagesByClasses($nodeClasses)
  300. {
  301. $criteria = new Criteria();
  302. $criteria->add(NodePeer::NODE_CLASS, $nodeClasses, Criteria::IN);
  303. return NodePeer::doSelectWithI18n($criteria);
  304. }
  305. static public function cachePageByNodeIdsAndClass($nodeIds, $nodeClass)
  306. {
  307. $nodeIdsToFetch = array();
  308. foreach ($nodeIds as $nodeId) {
  309. if (!isset(self::$cache[$nodeId])) {
  310. $nodeIdsToFetch[] = $nodeId;
  311. }
  312. }
  313. if (0 < count($nodeIds)) {
  314. $peerName = sprintf('Node%sPeer', $nodeClass);
  315. $peer = new $peerName();
  316. $criteria = new Criteria();
  317. $fieldName = eval(sprintf('return %s::NODE_ID;', $peerName));
  318. $criteria->add($fieldName, $nodeIds, Criteria::IN);
  319. if (method_exists($peer, 'doSelectWithI18N')) {
  320. $pages = $peer->doSelectWithI18N($criteria, sfConfig::get('app_translations_default_language'));
  321. } else {
  322. $pages = $peer->doSelect($criteria);
  323. }
  324. foreach ($pages as $page) {
  325. self::$cache[$page->getNodeId()] = $page;
  326. }
  327. }
  328. }
  329. static public function mysqlQuery($sql, $dbHandler = null)
  330. {
  331. $elapsedTime = 0;
  332. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
  333. $sqlTimer = sfTimerManager::getTimer('Database');
  334. $timer = new sfTimer();
  335. }
  336. $retval = mysql_query($sql, $dbHandler);
  337. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
  338. $sqlTimer->addTime();
  339. $elapsedTime = $timer->getElapsedTime();
  340. }
  341. $msg = sprintf("executeQuery(): [%.2f ms] %s", $elapsedTime * 1000, $sql);
  342. $dispatcher = sfProjectConfiguration::getActive()->getEventDispatcher();
  343. if ($dispatcher && sfConfig::get('sf_logging_enabled')) {
  344. // message on one line
  345. $msg = preg_replace("/\r?\n/", ' ', $msg);
  346. $dispatcher->notify(new sfEvent(null, 'application.log', array($msg)));
  347. }
  348. return $retval;
  349. }
  350. public static function getAllTranslations()
  351. {
  352. sfLoader::loadHelpers(array('I18N'), sfContext::getInstance()->getModuleName());
  353. $current_culture = sfConfig::get('app_translations_default_language');
  354. $result = array();
  355. $allLanguages = sfConfig::get('app_translations_available_languages');
  356. if (empty($allLanguages)) {
  357. trigger_error('Unable to find supported languauges for this site in app_translations_available_languages');
  358. } else {
  359. foreach($allLanguages as $iso) {
  360. $result[$iso] = format_language($iso, $current_culture);
  361. }
  362. asort($result);
  363. }
  364. return $result;
  365. }
  366. public static function registerRoutes(sfEvent $event)
  367. {
  368. $routing = $event->getSubject();
  369. // $routing->appendRoute('login', '/catalyz/login', array('module' => 'catalyz', 'action' => 'login'));
  370. // $routing->appendRoute('logout', '/catalyz/logout', array('module' => 'catalyz', 'action' => 'logout'));
  371. $routing->appendRoute('node-add', '/catalyz/node-add/:parent_id', array('module' => 'catalyz', 'action' => 'nodeAdd'), array('parent_id' => '^\d+$'));
  372. $routing->appendRoute('node-add-class', '/catalyz/node-add/:parent_id/:page_type', array('module' => 'catalyz', 'action' => 'nodeEdit'), array('parent_id' => '^\d+$'));
  373. $routing->appendRoute('node-edit-new', '/catalyz/node-edit', array('module' => 'catalyz', 'action' => 'nodeEdit'));
  374. $routing->appendRoute('node-edit', '/catalyz/node-edit/:node_id', array('module' => 'catalyz', 'action' => 'nodeEdit'), array('node_id' => '^\d+$'));
  375. $routing->appendRoute('node-reorder', '/catalyz/node-reorder/:node_id', array('module' => 'catalyz', 'action' => 'nodeReorder'), array('node_id' => '^\d+$'));
  376. $routing->appendRoute('node-reorganize', '/catalyz/node-reorganize/:node_id', array('module' => 'catalyz', 'action' => 'nodeReorganize'), array('node_id' => '^\d+$'));
  377. $routing->appendRoute('node-reorganize-update', '/catalyz/node-reorganize-update', array('module' => 'catalyz', 'action' => 'nodeReorganizeUpdate'), array());
  378. $routing->appendRoute('node-delete', '/catalyz/node-delete/:node_id', array('module' => 'catalyz', 'action' => 'nodeDelete'), array('node_id' => '^\d+$'));
  379. $routing->appendRoute('node-translations', '/catalyz/node-translations/:node_id', array('module' => 'catalyz', 'action' => 'nodeManageLanguageVersions'), array('node_id' => '^\d+$'));
  380. $routing->appendRoute('node-translation-add', '/catalyz/node-translation-add/:node_id', array('module' => 'catalyz', 'action' => 'nodeAddLanguageVersion'), array('node_id' => '^\d+$'));
  381. $routing->appendRoute('node-translation-add-and-edit', '/catalyz/node-translation-add/:node_id/:language/:edit', array('module' => 'catalyz', 'action' => 'nodeAddLanguageVersion', 'edit' => 1), array('node_id' => '^\d+$'));
  382. $routing->appendRoute('node-translation-edit', '/catalyz/node-translation-edit/:node_id/:culture', array('module' => 'catalyz', 'action' => 'nodeEdit'), array('node_id' => '^\d+$', 'culture' => '^[a-z_]+$'));
  383. $routing->appendRoute('node-translation-delete', '/catalyz/node-translation-delete/:node_id/:culture', array('module' => 'catalyz', 'action' => 'nodeDeleteLanguageVersion'), array('node_id' => '^\d+$', 'culture' => '^[a-z_]+$'));
  384. $routing->appendRoute('node-translation-overview', '/catalyz/node-translation-overview/:node_id', array('module' => 'catalyz', 'action' => 'nodeTranslationOverview'), array('node_id' => '^\d+$'));
  385. $routing->appendRoute('cache-status', '/catalyz/cache-status', array('module' => 'catalyz', 'action' => 'cacheStatus'));
  386. $routing->appendRoute('cache-clear', '/catalyz/cache-clear', array('module' => 'catalyz', 'action' => 'cacheClear'));
  387. $routing->appendRoute('cache-refresh', '/catalyz/cache-refresh', array('module' => 'catalyz', 'action' => 'cacheRefresh'));
  388. $routing->appendRoute('cache-create', '/catalyz/cache-create', array('module' => 'catalyz', 'action' => 'cacheCreate'));
  389. $routing->appendRoute('node-cache-clear', '/catalyz/node-cache-clear/:node_id/:culture', array('module' => 'catalyz', 'action' => 'cacheClear'), array('node_id' => '^\d+$', 'culture' => '^[a-z_]+$'));
  390. $routing->appendRoute('node-cache-create', '/catalyz/node-cache-create/:node_id/:culture', array('module' => 'catalyz', 'action' => 'cacheCreate'), array('node_id' => '^\d+$', 'culture' => '^[a-z_]+$'));
  391. $routing->appendRoute('admin', '/catalyz/admin/:node_id', array('module' => 'catalyz', 'action' => 'admin'), array('node_id' => '^\d+$'));
  392. $routing->appendRoute('sitemap-xml', '/sitemap.xml', array('module' => 'catalyz', 'action' => 'xmlSitemap'));
  393. $routing->appendRoute('catalyz', '/catalyz/:action/*', array('module' => 'catalyz'));
  394. $routing->appendRoute('homepage', '/*', array('module' => 'catalyz', 'action' => 'index'));
  395. // $routing->appendRoute('catalyz', '/catalyz/:action', array('module' => 'catalyz'));
  396. // $routing->prependRoute('default_symfony', '/symfony/:action/*', array('module' => 'default'));
  397. }
  398. public static function createTranslation($node, $newLanguage, $clearCache = true)
  399. {
  400. $default_locale = sfConfig::get('app_translations_default_language');
  401. $node->setUrlType($node->getUrlType($default_locale), $newLanguage);
  402. $node->setTitle($node->getTitle($default_locale), $newLanguage);
  403. $node->setSubTitle($node->getSubTitle($default_locale), $newLanguage);
  404. //$node->setUrlIdentifier($node->getUrlIdentifier($default_locale), $newLanguage);
  405. $node->setBrowserTitleContent($node->getBrowserTitleContent($default_locale), $newLanguage);
  406. $node->setSeoKeywordsContent($node->getSeoKeywordsContent($default_locale), $newLanguage);
  407. $node->setSeoDescriptionContent($node->getSeoDescriptionContent($default_locale), $newLanguage);
  408. $node->save();
  409. $page = $node->getPage();
  410. $page->createTranslation($newLanguage);
  411. $page->save();
  412. if ($clearCache) {
  413. Catalyz::clearTranslationCache($node->getId());
  414. }
  415. }
  416. public static function clearTranslationCache($nodeId)
  417. {
  418. $cacheManager = sfContext::getInstance()->getViewCacheManager();
  419. if ($cacheManager) {
  420. $cacheManager->remove('@sf_cache_partial?module=englishuk&action=_sidenav_translations&sf_cache_key=' . $nodeId);
  421. }
  422. }
  423. public static function normalizeUrlIdentifier($text)
  424. {
  425. // convert special characters
  426. $text = str_replace('ç', 'c', $text);
  427. $text = utf8_decode($text);
  428. $text = htmlentities($text);
  429. $text = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/', '$1', $text);
  430. $text = html_entity_decode($text);
  431. // lower characters
  432. $text = strtolower($text);
  433. // strip all non word chars
  434. $text = preg_replace('/[^-\w\.]/', ' ', $text);
  435. // replace all white space sections with a separator
  436. $text = preg_replace('/\ +/', '-', $text);
  437. // trim separators
  438. $text = preg_replace('/(\-|\.)+/', '\1', $text);
  439. $text = trim($text, '-.');
  440. return $text;
  441. }
  442. public static function cleanText($text)
  443. {
  444. $text = utf8_decode($text);
  445. $text = htmlentities($text);
  446. $text = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/', '$1', $text);
  447. $text = html_entity_decode($text);
  448. return $text;
  449. }
  450. public static function isTabEnabled($name)
  451. {
  452. return !in_array($name, sfConfig::get('app_catalyz_disabled_tabs', array()));
  453. }
  454. /**
  455. * Catalyz::renderText()
  456. *
  457. * @param mixed $page
  458. * @return
  459. */
  460. public static function renderText($content)
  461. {
  462. $content = CatalyzTextFilter::renderLinksToNodes($content);
  463. $content = CatalyzTextFilter::renderLightBoxImages($content);
  464. return $content;
  465. }
  466. /**
  467. * Catalyz::renderCollapseExpand()
  468. *
  469. * @param string $content
  470. * @return string
  471. */
  472. public static function renderCollapseExpand($content, $ContentTree)
  473. {
  474. return CatalyzTextFilter::renderCollapseExpand($content, $ContentTree);
  475. }
  476. public static function publishNode(sfEvent $event)
  477. {
  478. $node =/*(Node)*/ $event->getSubject();
  479. CatalyzCache::regenerate($node->getId(), $node->getCulture());
  480. }
  481. public static function initializeAdminActions(sfEvent $event){
  482. $page = $event->getSubject();
  483. CatalyzToolbar::instance()->register(array(
  484. 'key' => 'cache',
  485. 'caption' => __('Cache', null, 'catalyz'),
  486. 'url' => sfContext::getInstance()->getController()->genUrl('@cache-status?node_id=' . $page->getNodeId())
  487. ), true);
  488. CatalyzToolbar::instance()->register(array(
  489. 'key' => 'move',
  490. 'caption' => __('Reorganize', null, 'catalyz'),
  491. 'url' => sfContext::getInstance()->getController()->genUrl('@node-reorganize?node_id=' . $page->getNodeId())
  492. ), true);
  493. }
  494. function configureWebDebugToolbar(sfEvent $event){
  495. $webDebugToolbar = $event->getSubject();
  496. $webDebugToolbar->setPanel('CatalyzCMS', new sfWebDebugPanelCatalyzCMS($webDebugToolbar));
  497. }
  498. public static function getLoginHandler(){
  499. static $login_handler = null;
  500. if (is_null($login_handler)) {
  501. $className = sfConfig::get('app_login_handler');
  502. $login_handler = new $className();
  503. }
  504. return $login_handler;
  505. }
  506. }
  507. ?>