PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Ip/Internal/Content/Filter.php

https://gitlab.com/x33n/ImpressPages
PHP | 267 lines | 232 code | 29 blank | 6 comment | 22 complexity | 5b1af6d605b321cd84997fd39e9de98d MD5 | raw file
  1. <?php
  2. namespace Ip\Internal\Content;
  3. class Filter
  4. {
  5. public static function ipWidgets($widgets)
  6. {
  7. $widgets['Heading'] = new \Ip\Internal\Content\Widget\Heading\Controller('Heading', 'Content', 1);
  8. $widgets['Text'] = new \Ip\Internal\Content\Widget\Text\Controller('Text', 'Content', 1);
  9. $widgets['Divider'] = new \Ip\Internal\Content\Widget\Divider\Controller('Divider', 'Content', 1);
  10. $widgets['Image'] = new \Ip\Internal\Content\Widget\Image\Controller('Image', 'Content', 1);
  11. $widgets['Gallery'] = new \Ip\Internal\Content\Widget\Gallery\Controller('Gallery', 'Content', 1);
  12. $widgets['File'] = new \Ip\Internal\Content\Widget\File\Controller('File', 'Content', 1);
  13. $widgets['Html'] = new \Ip\Internal\Content\Widget\Html\Controller('Html', 'Content', 1);
  14. $widgets['Columns'] = new \Ip\Internal\Content\Widget\Columns\Controller('Columns', 'Content', 1);
  15. $widgets['Form'] = new \Ip\Internal\Content\Widget\Form\Controller('Form', 'Content', 1);
  16. $widgets['Video'] = new \Ip\Internal\Content\Widget\Video\Controller('Video', 'Content', 1);
  17. $widgets['Map'] = new \Ip\Internal\Content\Widget\Map\Controller('Map', 'Content', 1);
  18. $widgetDirs = static::getPluginWidgetDirs();
  19. foreach ($widgetDirs as $widgetDirRecord) {
  20. $widgetKey = $widgetDirRecord['widgetKey'];
  21. $widgetClass = '\\Plugin\\' . $widgetDirRecord['module'] . '\\' . Model::WIDGET_DIR . '\\' . $widgetKey . '\\Controller';
  22. if (class_exists($widgetClass)) {
  23. $widget = new $widgetClass($widgetKey, $widgetDirRecord['module'], 0);
  24. } else {
  25. $widget = new \Ip\WidgetController($widgetKey, $widgetDirRecord['module'], 0);
  26. }
  27. $widgets[$widgetDirRecord['widgetKey']] = $widget;
  28. }
  29. return $widgets;
  30. }
  31. /**
  32. * Form widget
  33. * @param $fieldTypes
  34. * @param null $info
  35. * @return \Ip\Internal\Content\FieldType[]
  36. * @internal param array $value
  37. */
  38. public static function ipWidgetFormFieldTypes($fieldTypes, $info = null)
  39. {
  40. $typeText = __('Text', 'Ip-admin', false);
  41. $typeEmail = __('Email', 'Ip-admin', false);
  42. $typeTextarea = __('Textarea', 'Ip-admin', false);
  43. $typeSelect = __('Select', 'Ip-admin', false);
  44. $typeCheckbox = __('Checkbox', 'Ip-admin', false);
  45. $typeRadio = __('Radio', 'Ip-admin', false);
  46. $typeCaptcha = __('Captcha', 'Ip-admin', false);
  47. $typeFile = __('File', 'Ip-admin', false);
  48. $typeRichText = __('Rich text', 'Ip-admin', false);
  49. $typeCheckboxes = __('Checkboxes', 'Ip-admin', false);
  50. $typeFieldset= __('Fieldset', 'Ip-admin', false);
  51. $fieldTypes['Text'] = new FieldType('Text', '\Ip\Form\Field\Text', $typeText);
  52. $fieldTypes['Email'] = new FieldType('Email', '\Ip\Form\Field\Email', $typeEmail);
  53. $fieldTypes['Textarea'] = new FieldType('Textarea', '\Ip\Form\Field\Textarea', $typeTextarea);
  54. $fieldTypes['Select'] = new FieldType('Select', '\Ip\Form\Field\Select', $typeSelect, 'ipWidgetForm_InitListOptions', 'ipWidgetForm_SaveListOptions', ipView(
  55. 'view/formFieldOptions/list.php'
  56. )->render());
  57. $fieldTypes['Checkbox'] = new FieldType('Checkbox', '\Ip\Form\Field\Checkbox', $typeCheckbox, 'ipWidgetForm_InitWysiwygOptions', 'ipWidgetForm_SaveWysiwygOptions', ipView(
  58. 'view/formFieldOptions/wysiwyg.php',
  59. array('form' => self::wysiwygForm())
  60. )->render());
  61. $fieldTypes['Radio'] = new FieldType('Radio', '\Ip\Form\Field\Radio', $typeRadio, 'ipWidgetForm_InitListOptions', 'ipWidgetForm_SaveListOptions', ipView(
  62. 'view/formFieldOptions/list.php'
  63. )->render());
  64. $fieldTypes['Captcha'] = new FieldType('Captcha', '\Ip\Form\Field\Captcha', $typeCaptcha);
  65. $fieldTypes['File'] = new FieldType('File', '\Ip\Form\Field\File', $typeFile);
  66. $fieldTypes['RichText'] = new FieldType('RichText', '\Ip\Form\Field\RichText', $typeRichText);
  67. $fieldTypes['Checkboxes'] = new FieldType('Checkboxes', '\Ip\Form\Field\Checkboxes', $typeCheckboxes, 'ipWidgetForm_InitListOptions', 'ipWidgetForm_SaveListOptions', ipView(
  68. 'view/formFieldOptions/list.php'
  69. )->render());
  70. $fieldTypes['Fieldset'] = new FieldType('Fieldset', '\Ip\Form\Fieldset', $typeFieldset);
  71. return $fieldTypes;
  72. }
  73. private static function wysiwygForm()
  74. {
  75. $form = new \Ip\Form();
  76. $form->setEnvironment(\Ip\Form::ENVIRONMENT_ADMIN);
  77. $field = new \Ip\Form\Field\RichText(array(
  78. 'name' => 'text'
  79. ));
  80. $form->addField($field);
  81. return $form;
  82. }
  83. private static function getPluginWidgetDirs()
  84. {
  85. $answer = array();
  86. $plugins = \Ip\Internal\Plugins\Service::getActivePluginNames();
  87. foreach ($plugins as $plugin) {
  88. $answer = array_merge($answer, static::findPluginWidgets($plugin));
  89. }
  90. return $answer;
  91. }
  92. private static function findPluginWidgets($moduleName)
  93. {
  94. $widgetDir = ipFile('Plugin/' . $moduleName . '/' . Model::WIDGET_DIR . '/');
  95. if (!is_dir($widgetDir)) {
  96. return array();
  97. }
  98. $widgetFolders = scandir($widgetDir);
  99. if ($widgetFolders === false) {
  100. return array();
  101. }
  102. $answer = array();
  103. //foreach all widget folders
  104. foreach ($widgetFolders as $widgetFolder) {
  105. //each directory is a widget
  106. if (!is_dir($widgetDir . $widgetFolder) || $widgetFolder == '.' || $widgetFolder == '..') {
  107. continue;
  108. }
  109. if (isset ($answer[(string)$widgetFolder])) {
  110. ipLog()->warning(
  111. 'Content.duplicateWidget: {widget}',
  112. array('plugin' => 'Content', 'widget' => $widgetFolder)
  113. );
  114. }
  115. $answer[] = array(
  116. 'module' => $moduleName,
  117. 'dir' => $widgetDir . $widgetFolder . '/',
  118. 'widgetKey' => $widgetFolder
  119. );
  120. }
  121. return $answer;
  122. }
  123. public static function ipAdminNavbarButtons($buttons, $info)
  124. {
  125. $breadcrumb = ipContent()->getBreadcrumb();
  126. if (!empty($breadcrumb[0])) {
  127. $rootPage = $breadcrumb[0];
  128. $menu = ipContent()->getPage($rootPage->getParentId());
  129. $alias = $menu->getAlias();
  130. } else {
  131. $alias = '';
  132. }
  133. if (ipContent()->getCurrentPage()) {
  134. if (!ipAdminPermission('Content')) {
  135. //Do nothing
  136. } elseif (ipIsManagementState()) {
  137. $buttons[] = array(
  138. 'text' => __('Preview', 'Ip-admin', false),
  139. 'hint' => __('Hides admin tools', 'Ip-admin', false),
  140. 'class' => 'ipsContentPreview',
  141. 'faIcon' => 'fa-eye',
  142. 'url' => '#'
  143. );
  144. } else {
  145. $buttons[] = array(
  146. 'text' => __('Edit', 'Ip-admin', false),
  147. 'hint' => __('Show widgets', 'Ip-admin', false),
  148. 'class' => 'ipsContentEdit',
  149. 'faIcon' => 'fa-edit',
  150. 'url' => '#'
  151. );
  152. }
  153. if (ipAdminPermission('Pages')) {
  154. $buttons[] = array(
  155. 'text' => __('Settings', 'Ip-admin', false),
  156. 'hint' => __('Page settings', 'Ip-admin', false),
  157. 'class' => 'ipsAdminPageSettings',
  158. 'faIcon' => 'fa-gear',
  159. 'url' => ipActionUrl(array('aa' => 'Pages.index')) . '#hash&language=' . ipContent(
  160. )->getCurrentLanguage()->getCode() . '&menu=' . $alias . '&page=' . ipContent()->getCurrentPage(
  161. )->getId()
  162. );
  163. }
  164. }
  165. return $buttons;
  166. }
  167. public static function ipAdminNavbarCenterElements($elements, $info)
  168. {
  169. if (ipContent()->getCurrentPage() && ipAdminPermission('Content')) {
  170. $revision = \Ip\ServiceLocator::content()->getCurrentRevision();
  171. $revisions = \Ip\Internal\Revision::getPageRevisions(ipContent()->getCurrentPage()->getId());
  172. $managementUrls = array();
  173. $currentPageLink = ipContent()->getCurrentPage()->getLink();
  174. foreach ($revisions as $value) {
  175. $managementUrls[] = $currentPageLink . '?_revision=' . $value['revisionId'];
  176. }
  177. $data = array(
  178. 'revisions' => $revisions,
  179. 'currentRevision' => $revision,
  180. 'managementUrls' => $managementUrls,
  181. 'isPublished' => !\Ip\Internal\Content\Model::isRevisionModified($revision['revisionId']) && ipContent(
  182. )->getCurrentPage()->isVisible(),
  183. 'isVisible' => ipContent()->getCurrentPage()->isvisible()
  184. );
  185. $elements[] = ipView('view/publishButton.php', $data);
  186. }
  187. return $elements;
  188. }
  189. public static function ipHead($head, $info)
  190. {
  191. $relativePath = ipRequest()->getRelativePath();
  192. $canonicalUrl = null;
  193. //detect if we need to add canonical meta tag because we are on the home page
  194. if (ipContent()->getCurrentPage() && ipContent()->getCurrentPage()->getId() == ipContent()->getDefaultPageId() && ipRequest()->getRelativePath() != '') {
  195. //if current page is the default page of current language and relative path is not empty
  196. $languages = ipContent()->getLanguages();
  197. $firstLanguage = $languages[0];
  198. if (ipContent()->getCurrentLanguage()->getId() == $firstLanguage->getId()) {
  199. //if current language is the first language, set canonical to the base URL.
  200. $canonicalUrl = ipConfig()->baseUrl();
  201. } elseif(ipRequest()->getRoutePath() != '') {
  202. //if current URL is not equal to the language URL, set canonical as language URL
  203. $canonicalUrl = ipContent()->getcurrentLanguage()->getLink();
  204. }
  205. }
  206. //detect if we need to add canonical tag because of missing trailing slash
  207. if (!$canonicalUrl) {
  208. //if canonicalUrl is not set yet
  209. if (ipGetOption('Config.trailingSlash', 1) && ipContent()->getCurrentPage()) {
  210. if (substr($relativePath, -1) != '/') {
  211. $canonicalUrl = ipConfig()->baseUrl() . $relativePath . '/';
  212. }
  213. } else {
  214. if (substr($relativePath, -1) == '/') {
  215. $canonicalUrl = ipConfig()->baseUrl() . substr($relativePath, 0, -1);
  216. }
  217. }
  218. }
  219. if ($canonicalUrl) {
  220. $append = ' <link rel="canonical" href="' . escAttr($canonicalUrl) . '" />' . "\n";
  221. $head .= $append;
  222. }
  223. return $head;
  224. }
  225. }