PageRenderTime 78ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Ip/Content.php

https://gitlab.com/x33n/ImpressPages
PHP | 549 lines | 289 code | 58 blank | 202 comment | 35 complexity | c1c8838f4565ba6991d9acbc029655cc MD5 | raw file
  1. <?php
  2. /**
  3. * @package ImpressPages
  4. *
  5. *
  6. */
  7. namespace Ip;
  8. /**
  9. * Language, page, block and other content.
  10. * Can be treated as a gateway to framework content.
  11. *
  12. */
  13. class Content
  14. {
  15. /**
  16. * @var \Ip\Language[]
  17. */
  18. protected $languages;
  19. protected $blockContent;
  20. /**
  21. * @var \Ip\Language
  22. */
  23. protected $currentLanguage;
  24. /**
  25. * @var \Ip\Page
  26. */
  27. protected $currentPage;
  28. protected $currentRevision;
  29. public function __construct()
  30. {
  31. }
  32. /**
  33. * Get current language object
  34. * @return \Ip\Language
  35. */
  36. public function getCurrentLanguage()
  37. {
  38. return $this->currentLanguage;
  39. }
  40. /**
  41. * @ignore Used only for internal purposes
  42. */
  43. public function _setCurrentLanguage($currentLanguage)
  44. {
  45. $this->currentLanguage = $currentLanguage;
  46. }
  47. /**
  48. * @param $pageId
  49. * @return \Ip\Page
  50. */
  51. public function getPage($pageId)
  52. {
  53. try {
  54. $page = new \Ip\Page($pageId);
  55. } catch (\Ip\Exception $e) {
  56. return null;
  57. }
  58. return $page;
  59. }
  60. /**
  61. * @param string $alias
  62. * @param string|null $languageCode
  63. * @return \Ip\Page
  64. */
  65. public function getPageByAlias($alias, $languageCode = null)
  66. {
  67. if ($languageCode === null) {
  68. $languageCode = ipContent()->getCurrentLanguage()->getCode();
  69. }
  70. $row = ipDb()->selectRow(
  71. 'page',
  72. '*',
  73. array('alias' => $alias, 'languageCode' => $languageCode, 'isDeleted' => 0)
  74. );
  75. if (!$row) {
  76. return null;
  77. }
  78. return new \Ip\Page($row);
  79. }
  80. /**
  81. * Get current page object
  82. *
  83. * @return \Ip\Page
  84. */
  85. public function getCurrentPage()
  86. {
  87. return $this->currentPage;
  88. }
  89. /**
  90. * @ignore used only for internal purposes
  91. */
  92. public function _setCurrentPage($page)
  93. {
  94. $this->currentPage = $page;
  95. $this->currentRevision = null;
  96. }
  97. /**
  98. * Get specific language object
  99. * @param int $id Language ID
  100. * @return bool|Language
  101. */
  102. public function getLanguage($id)
  103. {
  104. $id = (int)$id;
  105. foreach ($this->getLanguages() as $language) {
  106. if ($language->getId() === $id) {
  107. return $language;
  108. }
  109. }
  110. return false;
  111. }
  112. /**
  113. * Get specific language object
  114. * @param string $code Language code
  115. * @return null|Language
  116. */
  117. public function getLanguageByCode($code)
  118. {
  119. foreach ($this->getLanguages() as $language) {
  120. if ($language->getCode() === $code) {
  121. return $language;
  122. }
  123. }
  124. return null;
  125. }
  126. /**
  127. * Get all website languages
  128. *
  129. * @return \Ip\Language[] All website languages. Each element is a Language object.
  130. *
  131. */
  132. public function getLanguages()
  133. {
  134. if ($this->languages === null) {
  135. $languages = \Ip\Internal\Languages\Service::getLanguages();
  136. $this->languages = array();
  137. foreach ($languages as $data) {
  138. $this->languages[] = \Ip\Internal\Content\Helper::createLanguage($data);
  139. }
  140. }
  141. return $this->languages;
  142. }
  143. /**
  144. * @ignore
  145. */
  146. public function _invalidateLanguages()
  147. {
  148. $this->languages = null;
  149. }
  150. /**
  151. * Get page block HTML content
  152. * @param string $block Block name
  153. * @return string HTML code
  154. */
  155. public function getBlockContent($block)
  156. {
  157. if (isset($this->blockContent[$block])) {
  158. return $this->blockContent[$block];
  159. } else {
  160. return null;
  161. }
  162. }
  163. /**
  164. * Set page block HTML content
  165. * @param string $block Block name
  166. * @param string $content HTML code
  167. */
  168. public function setBlockContent($block, $content)
  169. {
  170. $this->blockContent[$block] = $content;
  171. }
  172. /**
  173. * Generate block object
  174. *
  175. * @param string $blockName
  176. * @return Block
  177. */
  178. public function generateBlock($blockName)
  179. {
  180. return new \Ip\Block($blockName);
  181. }
  182. /**
  183. * If in management state and the last revision was published, create a new revision.
  184. * @ignore
  185. */
  186. public function getCurrentRevision()
  187. {
  188. if ($this->currentRevision !== null) {
  189. return $this->currentRevision;
  190. }
  191. if (!$this->currentPage) {
  192. return null;
  193. }
  194. $revision = null;
  195. $pageId = $this->currentPage->getId();
  196. if (ipRequest()->getQuery('_revision') && ipAdminId()) {
  197. $revisionId = ipRequest()->getQuery('_revision');
  198. $revision = \Ip\Internal\Revision::getRevision($revisionId);
  199. if ($revision['pageId'] != $pageId) {
  200. $revision = null;
  201. }
  202. }
  203. if (!$revision && ipIsManagementState()) {
  204. $revision = \Ip\Internal\Revision::getLastRevision($pageId);
  205. if ($revision['isPublished']) {
  206. $duplicatedId = \Ip\Internal\Revision::duplicateRevision($revision['revisionId']);
  207. $revision = \Ip\Internal\Revision::getRevision($duplicatedId);
  208. }
  209. }
  210. if (!$revision) {
  211. $revision = \Ip\Internal\Revision::getPublishedRevision($this->currentPage->getId());
  212. }
  213. $this->currentRevision = $revision;
  214. return $this->currentRevision;
  215. }
  216. /**
  217. * Get a breadcrumb
  218. *
  219. * Gets an array of pages representing a tree path to a current page.
  220. *
  221. * @param int $pageId
  222. * @return \Ip\Page[]
  223. */
  224. public function getBreadcrumb($pageId = null)
  225. {
  226. if ($pageId !== null) {
  227. $page = new \Ip\Page($pageId);
  228. } else {
  229. $page = ipContent()->getCurrentPage();
  230. }
  231. if ($page) {
  232. $pages[] = $page;
  233. $parentPageId = $page->getParentId();
  234. while (!empty($parentPageId)) {
  235. $parentPage = new \Ip\Page($parentPageId);
  236. $pages[] = $parentPage;
  237. $parentPageId = $parentPage->getParentId();
  238. }
  239. }
  240. $breadcrumb = array();
  241. if (!empty($pages)) {
  242. array_pop($pages);
  243. $breadcrumb = $pages;
  244. }
  245. $breadcrumb = array_reverse($breadcrumb);
  246. $breadcrumb = ipFilter('ipBreadcrumb', $breadcrumb);
  247. return $breadcrumb;
  248. }
  249. /**
  250. * Get a page title
  251. *
  252. * @return string Title of the current page
  253. *
  254. */
  255. public function getTitle()
  256. {
  257. if ($this->currentPage) {
  258. return $this->currentPage->getMetaTitle() ? $this->currentPage->getMetaTitle(
  259. ) : $this->currentPage->getTitle();
  260. }
  261. return '';
  262. }
  263. /**
  264. * Get the current page description
  265. *
  266. * @return string Description of the current page
  267. *
  268. */
  269. public function getDescription()
  270. {
  271. if ($this->currentPage) {
  272. return $this->currentPage->getDescription();
  273. }
  274. return '';
  275. }
  276. /**
  277. * Get the current page keywords
  278. *
  279. * @return string Keywords for the current page
  280. *
  281. */
  282. public function getKeywords()
  283. {
  284. if ($this->currentPage) {
  285. return $this->currentPage->getKeywords();
  286. }
  287. return '';
  288. }
  289. /**
  290. * Add website language
  291. *
  292. * @param string $title
  293. * @param string $abbreviation
  294. * @param string $code
  295. * @param string $url
  296. * @param bool $visible
  297. * @param string $textDirection
  298. * @param null $position
  299. * @return mixed
  300. */
  301. public function addLanguage($title, $abbreviation, $code, $url, $visible, $textDirection = 'ltr', $position = null)
  302. {
  303. $languageId = \Ip\Internal\Languages\Service::addLanguage(
  304. $title,
  305. $abbreviation,
  306. $code,
  307. $url,
  308. $visible,
  309. $textDirection,
  310. $position = null
  311. );
  312. return $languageId;
  313. }
  314. /**
  315. * Delete a language with specific ID
  316. *
  317. * @param $languageId
  318. */
  319. public function deleteLanguage($languageId)
  320. {
  321. \Ip\Internal\Languages\Service::delete($languageId);
  322. }
  323. /**
  324. * Update page data
  325. * @param int $pageId
  326. * @param array $data
  327. */
  328. public function updatePage($pageId, $data)
  329. {
  330. \Ip\Internal\Pages\Service::updatePage($pageId, $data);
  331. }
  332. /**
  333. * Add a new page
  334. *
  335. * @param int $parentId Parent page ID
  336. * @param string $title
  337. * @param array $data
  338. * @return mixed
  339. */
  340. public function addPage($parentId, $title, $data = array())
  341. {
  342. $newPageId = \Ip\Internal\Pages\Service::addPage($parentId, $title, $data);
  343. return $newPageId;
  344. }
  345. /**
  346. * Copy page
  347. *
  348. * @param int $pageId Source page ID
  349. * @param int $destinationParentId Target parent ID
  350. * @param int $destinationPosition
  351. * @return int New copied page ID
  352. */
  353. public function copyPage($pageId, $destinationParentId, $destinationPosition)
  354. {
  355. $pageId = \Ip\Internal\Pages\Service::copyPage($pageId, $destinationParentId, $destinationPosition);
  356. return $pageId;
  357. }
  358. /**
  359. * Move a page to a different location on a website tree
  360. * @param int $pageId Source page ID
  361. * @param int $destinationParentId Target parent ID
  362. * @param int $destinationPosition
  363. */
  364. public function movePage($pageId, $destinationParentId, $destinationPosition)
  365. {
  366. \Ip\Internal\Pages\Service::movePage($pageId, $destinationParentId, $destinationPosition);
  367. }
  368. /**
  369. * Delete a page
  370. * @param int $pageId
  371. */
  372. public function deletePage($pageId)
  373. {
  374. \Ip\Internal\Pages\Service::deletePage($pageId);
  375. }
  376. /**
  377. * Get children
  378. * @param null $parentId
  379. * @param null $from
  380. * @param null $till
  381. * @param string $orderBy
  382. * @param string $direction
  383. * @return Page[]
  384. */
  385. public function getChildren(
  386. $parentId = null,
  387. $from = null,
  388. $till = null,
  389. $orderBy = 'pageOrder',
  390. $direction = 'ASC'
  391. ) {
  392. if ($parentId === null) {
  393. $parentId = $this->getCurrentPage()->getId();
  394. }
  395. $page = $this->getPage($parentId);
  396. if (!$page) {
  397. return array();
  398. }
  399. return $page->getChildren($from, $till, $orderBy, $direction);
  400. }
  401. /**
  402. * Get menu of currently active or specified page.
  403. * This method could be used to check if page is in some of menus or not.
  404. * Eg. if (ipContent()->getPageMenu()->getAlias() == 'menu1') //if current page is one of pages in menu1
  405. * @param null $pageId
  406. * @return Page
  407. */
  408. public function getPageMenu($pageId = null)
  409. {
  410. if ($pageId === null) {
  411. $page = $this->getCurrentPage();
  412. } else {
  413. $page = $this->getPage($pageId);
  414. }
  415. if (!$page) {
  416. return false;
  417. }
  418. $rootPage = $page;
  419. while ($page = $this->getPage($page->getParentId())) {
  420. $rootPage = $page;
  421. }
  422. return $rootPage;
  423. }
  424. /**
  425. * Get menus of the website.
  426. * Menu list may be different on each language. That's why there is $languageCode parameter.
  427. * If $languageCode is omitted, current language is used by default.
  428. *
  429. * @param string $languageCode
  430. * @return \Ip\Page[]
  431. */
  432. public function getMenus($languageCode = null)
  433. {
  434. $result = \Ip\Internal\Pages\Service::getMenus($languageCode);
  435. $objectArray = array();
  436. foreach ($result as $menuData)
  437. {
  438. $objectArray[] = new \Ip\Page($menuData);
  439. }
  440. return $objectArray;
  441. }
  442. /**
  443. * Get menu.
  444. *
  445. * @param string $languageCode
  446. * @param string $alias unique menu identificator within the language
  447. * @return \Ip\Page
  448. */
  449. public static function getMenu($languageCode, $alias)
  450. {
  451. $result = \Ip\Internal\Pages\Service::getMenu($languageCode, $alias);
  452. if ($result) {
  453. return new \Ip\Page($result);
  454. }
  455. return $result;
  456. }
  457. /**
  458. * @param $languageCode
  459. * @param $alias
  460. * @param $title
  461. * @param string $type 'tree' or 'list'
  462. * @return int
  463. */
  464. public function addMenu($languageCode, $alias, $title, $type = 'tree')
  465. {
  466. return \Ip\Internal\Pages\Service::createMenu($languageCode, $alias, $title, $type);
  467. }
  468. /**
  469. * @param $id menu id
  470. */
  471. public function deleteMenu($languageCode, $alias)
  472. {
  473. $menu = $this->getMenu($languageCode, $alias);
  474. if ($menu) {
  475. $this->deletePage($menu->getId());
  476. }
  477. }
  478. /**
  479. * Get Id of the default page in current or specified language
  480. * @param string $languageCode
  481. * @return int
  482. */
  483. public function getDefaultPageId($languageCode = null)
  484. {
  485. if ($languageCode == null) {
  486. $languageCode = ipContent()->getCurrentLanguage()->getCode();
  487. }
  488. $pageId = ipJob('ipDefaultPageId', array('languageCode' => $languageCode));
  489. return $pageId;
  490. }
  491. }