PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/model/doctrine/PluginDmPageTable.class.php

https://github.com/xdade/diem
PHP | 404 lines | 329 code | 55 blank | 20 comment | 23 complexity | 54c780c5c5fda3daeaa215c655997f29 MD5 | raw file
  1. <?php
  2. /**
  3. */
  4. class PluginDmPageTable extends myDoctrineTable
  5. {
  6. protected
  7. $recordPageCache = array(),
  8. $findByStringCache = array();
  9. /**
  10. * Check that basic pages exist
  11. * ( main/root, main/error404, main/signin )
  12. * and, if they don't, will create them
  13. */
  14. public function checkBasicPages()
  15. {
  16. // check root page
  17. if (!$root = $this->getTree()->fetchRoot())
  18. {
  19. $root = $this->create(array(
  20. 'module' => 'main',
  21. 'action' => 'root',
  22. 'name' => $this->tryToTranslate('Home'),
  23. 'title' => $this->tryToTranslate('Home'),
  24. 'slug' => ''
  25. ));
  26. $this->getTree()->createRoot($root);
  27. if ($layout = dmDb::table('DmLayout')->findOneByName('Home'))
  28. {
  29. $root->PageView->Layout = $layout;
  30. $root->PageView->save();
  31. }
  32. }
  33. // check error404 page
  34. if (!$this->createQuery('p')->where('p.module = ? AND p.action = ?', array('main', 'error404'))->exists())
  35. {
  36. $page404 = $this->create(array(
  37. 'module' => 'main',
  38. 'action' => 'error404',
  39. 'name' => $this->tryToTranslate('Page not found'),
  40. 'title' => $this->tryToTranslate('Page not found'),
  41. 'slug' => 'error404'
  42. ));
  43. $page404->getNode()->insertAsLastChildOf($root);
  44. dmDb::table('DmWidget')->createInZone(
  45. $page404->PageView->Area->Zones[0],
  46. 'dmWidgetContent/title',
  47. array('text' => 'Page not found', 'tag' => 'h1')
  48. )->save();
  49. }
  50. // check signin page
  51. if (!$this->createQuery('p')->where('p.module = ? AND p.action = ?', array('main', 'signin'))->exists())
  52. {
  53. $signinPage = $this->create(array(
  54. 'module' => 'main',
  55. 'action' => 'signin',
  56. 'name' => $this->tryToTranslate('Signin'),
  57. 'title' => $this->tryToTranslate('Signin'),
  58. 'slug' => 'security/signin'
  59. ));
  60. $signinPage->getNode()->insertAsLastChildOf($root);
  61. dmDb::table('DmWidget')->createInZone(
  62. $signinPage->PageView->Area->Zones[0],
  63. 'dmUser/signin'
  64. )->save();
  65. }
  66. }
  67. protected function tryToTranslate($message)
  68. {
  69. if($i18n = $this->getService('i18n'))
  70. {
  71. return $i18n->__($message);
  72. }
  73. return $message;
  74. }
  75. /**
  76. * Check that search page exist
  77. * and, if doesn't, will create it
  78. */
  79. public function checkSearchPage()
  80. {
  81. if (!$this->createQuery('p')->where('p.module = ? AND p.action = ?', array('main', 'search'))->exists())
  82. {
  83. $searchResultsPage = $this->create(array(
  84. 'name' => $this->tryToTranslate('Search results'),
  85. 'title' => $this->tryToTranslate('Search results'),
  86. 'module' => 'main',
  87. 'action' => 'search',
  88. 'slug' => 'search'
  89. ));
  90. $searchResultsPage->getNode()->insertAsLastChildOf($this->getTree()->fetchRoot());
  91. dmDb::table('DmWidget')->createInZone(
  92. $searchResultsPage->PageView->Area->Zones[0],
  93. 'dmWidgetSearch/results'
  94. )->save();
  95. }
  96. }
  97. public function preloadPagesForRecords($records)
  98. {
  99. if ($records instanceof Doctrine_Collection)
  100. {
  101. $records = $records->getData();
  102. }
  103. foreach($records as $index => $record)
  104. {
  105. if (!$record instanceof dmDoctrineRecord)
  106. {
  107. unset($records[$index]);
  108. }
  109. }
  110. if (!empty($records))
  111. {
  112. if (($module = dmArray::first($records)->getDmModule()) && $module->hasPage())
  113. {
  114. $ids = array();
  115. foreach($records as $record)
  116. {
  117. $ids[] = $record->get('id');
  118. }
  119. $this->prepareRecordPageCache($module->getKey(), array_unique($ids));
  120. }
  121. }
  122. }
  123. public function prepareRecordPageCache($module, array $ids, $culture = null)
  124. {
  125. if(!empty($this->recordPageCache[$module]))
  126. {
  127. foreach($ids as $index => $id)
  128. {
  129. if (isset($this->recordPageCache[$module][$id]))
  130. {
  131. unset($ids[$index]);
  132. }
  133. }
  134. }
  135. else
  136. {
  137. $this->recordPageCache[$module] = array();
  138. }
  139. $pages = $this->createQuery('p')
  140. ->select('p.id, p.module, p.action, p.record_id, pTranslation.is_secure, p.lft, p.rgt, pTranslation.slug, pTranslation.name, pTranslation.title, pTranslation.is_active')
  141. ->where('p.module = ?', $module)
  142. ->andWhere('p.action = ?', 'show')
  143. ->andWhereIn('p.record_id', $ids)
  144. ->withI18n($culture, null, 'p')
  145. ->fetchRecords()
  146. ->getData();
  147. foreach($pages as $page)
  148. {
  149. $this->recordPageCache[$module][$page->get('record_id')] = $page;
  150. }
  151. unset($pages);
  152. }
  153. public function isSlugUnique($slug, $id)
  154. {
  155. return !$this->getI18nTable()->createQuery('pt')
  156. ->where('pt.lang = ?', dmDoctrineRecord::getDefaultCulture())
  157. ->andwhere('pt.id != ?', $id ? $id : 0)
  158. ->andWhere('pt.slug = ?', $slug)
  159. ->exists();
  160. }
  161. public function createUniqueSlug($slug, $id, $parentSlug = null)
  162. {
  163. if(null === $parentSlug)
  164. {
  165. $parentSlug = $this->getI18nTable()->createQuery('pt')
  166. ->where('pt.id = ?', $this->findOneById($id)->getNodeParentId())
  167. ->andWhere('pt.lang = ?', dmDoctrineRecord::getDefaultCulture())
  168. ->select('pt.slug')
  169. ->fetchValue();
  170. }
  171. if($slug == $parentSlug)
  172. {
  173. $slug .= '/'.$id;
  174. }
  175. else
  176. {
  177. $slug .= '-'.$id;
  178. }
  179. return $slug;
  180. }
  181. /**
  182. * Queries
  183. */
  184. public function queryByModuleAndAction($module, $action)
  185. {
  186. return $this->createQuery('p')
  187. ->where('p.module = ? AND p.action = ?', array($module, $action));
  188. }
  189. public function findAllForCulture($culture, $hydrationMode = Doctrine_Core::HYDRATE_ARRAY)
  190. {
  191. return $this->createQuery('p')
  192. ->withI18n($culture, null, 'p')
  193. ->execute(array(), $hydrationMode);
  194. }
  195. /**
  196. * Performance finder shortcuts
  197. */
  198. public function findOneBySource($source)
  199. {
  200. if ($source instanceof DmPage)
  201. {
  202. return $source;
  203. }
  204. elseif($source instanceof myDoctrineRecord)
  205. {
  206. return $source->getDmPage();
  207. }
  208. if (!isset($this->findByStringCache[$source]))
  209. {
  210. if(null === $source)
  211. {
  212. $this->findByStringCache[$source] = $this->getTree()->fetchRoot();
  213. }
  214. elseif(is_string($source))
  215. {
  216. if ($anchorPos = strpos($source, '#'))
  217. {
  218. $source = substr($source, 0, $anchorPos);
  219. }
  220. if ($spacePos = strpos($source, ' '))
  221. {
  222. $source = substr($source, 0, $spacePos);
  223. }
  224. if (strncmp($source, 'page:', 5) === 0)
  225. {
  226. $this->findByStringCache[$source] = $this->findOneByIdWithI18n((int)substr($source, 5));
  227. }
  228. elseif(substr_count($source, '/') === 1)
  229. {
  230. $parts = explode('/', $source);
  231. $this->findByStringCache[$source] = $this->findOneByModuleAndActionWithI18n($parts[0], $parts[1]);
  232. }
  233. else
  234. {
  235. $this->findByStringCache[$source] = null;
  236. }
  237. }
  238. else
  239. {
  240. $this->findByStringCache[$source] = null;
  241. }
  242. }
  243. return $this->findByStringCache[$source];
  244. }
  245. public function findByAction($action)
  246. {
  247. return $this->createQuery('p')->where('p.action = ?', $action)->fetchRecords();
  248. }
  249. public function findByModule($module)
  250. {
  251. return $this->createQuery('p')->where('p.module = ?', $module)->fetchRecords();
  252. }
  253. public function findOneByRecord(myDoctrineRecord $record)
  254. {
  255. return $this->createQuery('p')
  256. ->where('p.module = ?', $record->getDmModule()->getKey())
  257. ->andWhere('p.action = ?', 'show')
  258. ->andWhere('p.record_id = ?', $record->get('id'))
  259. ->fetchRecord();
  260. }
  261. public function findOneBySlug($slug, $culture = null)
  262. {
  263. return $this->createQuery('p')
  264. ->withI18n($culture, null, 'p', 'inner')
  265. ->where('pTranslation.slug = ?', $slug)
  266. ->fetchOne();
  267. }
  268. public function findByLevelWithI18n($level, $culture = null)
  269. {
  270. return $this->createQuery('p')
  271. ->where('p.level = ?', $level)
  272. ->withI18n($culture, null, 'p')
  273. ->fetchRecords();
  274. }
  275. public function fetchError404()
  276. {
  277. $this->checkBasicPages();
  278. return $this->findOneByModuleAndActionWithI18n('main', 'error404');
  279. }
  280. public function fetchSignin()
  281. {
  282. $this->checkBasicPages();
  283. return $this->findOneByModuleAndActionWithI18n('main', 'signin');
  284. }
  285. public function findOneById($id)
  286. {
  287. return $this->createQuery('p')
  288. ->where('p.id = ?', $id)
  289. ->fetchOne();
  290. }
  291. public function findOneByIdWithI18n($id, $culture = null)
  292. {
  293. return $this->createQuery('p')
  294. ->where('p.id = ?', $id)
  295. ->withI18n($culture, null, 'p')
  296. ->fetchOne();
  297. }
  298. public function fetchRootWithI18n($culture = null)
  299. {
  300. return $this->createQuery('p')
  301. ->where('p.lft = ?', 1)
  302. ->withI18n($culture, null, 'p')
  303. ->fetchOne();
  304. }
  305. public function findOneByRecordWithI18n(dmDoctrineRecord $record)
  306. {
  307. $module = $record->getDmModule()->getKey();
  308. if (isset($this->recordPageCache[$module][$record->get('id')]))
  309. {
  310. return $this->recordPageCache[$module][$record->get('id')];
  311. }
  312. return $this->createQuery('p')
  313. ->where('p.module = ?', $module)
  314. ->andWhere('p.action = ?', 'show')
  315. ->andWhere('p.record_id = ?', $record->get('id'))
  316. ->withI18n(null, null, 'p')
  317. ->fetchOne();
  318. }
  319. public function findByModuleAndAction($module, $action)
  320. {
  321. return $this->createQuery('p')
  322. ->where('p.module = ?', $module)
  323. ->andWhere('p.action = ?', $action)
  324. ->fetchRecords();
  325. }
  326. public function findByModuleAndActionWithI18n($module, $action)
  327. {
  328. return $this->createQuery('p')
  329. ->withI18n()
  330. ->where('p.module = ?', $module)
  331. ->andWhere('p.action = ?', $action)
  332. ->fetchRecords();
  333. }
  334. public function findOneByModuleAndAction($module, $action)
  335. {
  336. return $this->createQuery('p')
  337. ->where('p.module = ?', $module)
  338. ->andWhere('p.action = ?', $action)
  339. ->fetchRecord();
  340. }
  341. public function findOneByModuleAndActionWithI18n($module, $action, $culture = null)
  342. {
  343. return $this->createQuery('p')
  344. ->where('p.module = ?', $module)
  345. ->andWhere('p.action = ?', $action)
  346. ->withI18n($culture, null, 'p')
  347. ->fetchOne();
  348. }
  349. }