/concrete/blocks/page_attribute_display/controller.php

https://gitlab.com/koodersmiikka/operaatio-terveys · PHP · 203 lines · 147 code · 22 blank · 34 comment · 24 complexity · ca98ab38f497e9d84e1b0e9155614d6f MD5 · raw file

  1. <?php
  2. namespace Concrete\Block\PageAttributeDisplay;
  3. use Concrete\Core\Block\BlockController;
  4. use Concrete\Core\Attribute\Key\CollectionKey as CollectionAttributeKey;
  5. use Core;
  6. defined('C5_EXECUTE') or die('Access Denied.');
  7. /**
  8. * @author Ryan Tyler
  9. */
  10. class Controller extends BlockController
  11. {
  12. protected $btTable = 'btPageAttributeDisplay';
  13. protected $btInterfaceWidth = "500";
  14. protected $btInterfaceHeight = "365";
  15. public $dateFormat = "m/d/y h:i:a";
  16. protected $btCacheBlockOutput = true;
  17. protected $btCacheBlockOutputOnPost = true;
  18. protected $btCacheBlockOutputForRegisteredUsers = false;
  19. /**
  20. * @var int thumbnail height
  21. */
  22. public $thumbnailHeight = 250;
  23. /**
  24. * @var int thumbnail width
  25. */
  26. public $thumbnailWidth = 250;
  27. public function getBlockTypeDescription()
  28. {
  29. return t("Displays the value of a page attribute for the current page.");
  30. }
  31. public function getBlockTypeName()
  32. {
  33. return t("Page Attribute Display");
  34. }
  35. /**
  36. * @return mixed AttributeValue
  37. */
  38. public function getContent()
  39. {
  40. $c = \Page::getCurrentPage();
  41. $content = "";
  42. switch ($this->attributeHandle) {
  43. case "rpv_pageName":
  44. $content = h($c->getCollectionName());
  45. break;
  46. case "rpv_pageDescription":
  47. $content = h($c->getCollectionDescription());
  48. break;
  49. case "rpv_pageDateCreated":
  50. $content = $c->getCollectionDateAdded();
  51. break;
  52. case "rpv_pageDateLastModified":
  53. $content = $c->getCollectionDateLastModified();
  54. break;
  55. case "rpv_pageDatePublic":
  56. $content = $c->getCollectionDatePublic();
  57. break;
  58. default:
  59. $content = $c->getAttribute($this->attributeHandle);
  60. if (is_object($content) && $content instanceof \Concrete\Core\File\File) {
  61. if ($this->thumbnailWidth > 0 || $this->thumbnailHeight > 0) {
  62. $im = Core::make('helper/image');
  63. $thumb = $im->getThumbnail(
  64. $content,
  65. $this->thumbnailWidth,
  66. $this->thumbnailHeight
  67. ); //<-- set these 2 numbers to max width and height of thumbnails
  68. $content = "<img src=\"{$thumb->src}\" width=\"{$thumb->width}\" height=\"{$thumb->height}\" alt=\"\" />";
  69. } else {
  70. $image = Core::make('html/image', array($content));
  71. $content = (string) $image->getTag();
  72. }
  73. } else {
  74. if (!is_scalar($content) && (!is_object($content) || !method_exists($content, '__toString'))) {
  75. $content = $c->getAttribute($this->attributeHandle, 'displaySanitized');
  76. }
  77. }
  78. break;
  79. }
  80. $is_stack = $c->getController() instanceof \Concrete\Controller\SinglePage\Dashboard\Blocks\Stacks;
  81. if (!strlen(trim(strip_tags($content))) && ($c->isMasterCollection() || $is_stack)) {
  82. $content = $this->getPlaceHolderText($this->attributeHandle);
  83. }
  84. return $content;
  85. }
  86. /**
  87. * Returns a place holder for pages that are new or when editing default page types.
  88. *
  89. * @param string $handle
  90. *
  91. * @return string
  92. */
  93. public function getPlaceHolderText($handle)
  94. {
  95. $pageValues = $this->getAvailablePageValues();
  96. if (in_array($handle, array_keys($pageValues))) {
  97. $placeHolder = $pageValues[$handle];
  98. } else {
  99. $attributeKey = CollectionAttributeKey::getByHandle($handle);
  100. if (is_object($attributeKey)) {
  101. $placeHolder = $attributeKey->getAttributeKeyName();
  102. }
  103. }
  104. return "[" . $placeHolder . "]";
  105. }
  106. /**
  107. * Returns the title text to display in front of the value.
  108. *
  109. * @return string
  110. */
  111. public function getTitle()
  112. {
  113. return (strlen($this->attributeTitleText) ? $this->attributeTitleText . " " : "");
  114. }
  115. public function getAvailablePageValues()
  116. {
  117. return array(
  118. 'rpv_pageName' => t('Page Name'),
  119. 'rpv_pageDescription' => t('Page Description'),
  120. 'rpv_pageDateCreated' => t('Page Date Created'),
  121. 'rpv_pageDatePublic' => t('Page Date Published'),
  122. 'rpv_pageDateLastModified' => t('Page Date Modified'),
  123. );
  124. }
  125. public function getAvailableAttributes()
  126. {
  127. return \Concrete\Core\Attribute\Key\CollectionKey::getList();
  128. }
  129. protected function getTemplateHandle()
  130. {
  131. if (in_array($this->attributeHandle, array_keys($this->getAvailablePageValues()))) {
  132. switch ($this->attributeHandle) {
  133. case "rpv_pageDateCreated":
  134. case 'rpv_pageDateLastModified':
  135. case "rpv_pageDatePublic":
  136. $templateHandle = 'date_time';
  137. break;
  138. }
  139. } else {
  140. $attributeKey = CollectionAttributeKey::getByHandle($this->attributeHandle);
  141. if (is_object($attributeKey)) {
  142. $attributeType = $attributeKey->getAttributeType();
  143. $templateHandle = $attributeType->getAttributeTypeHandle();
  144. }
  145. }
  146. return $templateHandle;
  147. }
  148. /**
  149. * Returns opening html tag.
  150. *
  151. * @return string
  152. */
  153. public function getOpenTag()
  154. {
  155. $tag = "";
  156. if (strlen($this->displayTag)) {
  157. $tag = "<" . $this->displayTag . " class=\"ccm-block-page-attribute-display-wrapper\">";
  158. }
  159. return $tag;
  160. }
  161. /**
  162. * Returns closing html tag.
  163. *
  164. * @return string
  165. */
  166. public function getCloseTag()
  167. {
  168. $tag = "";
  169. if (strlen($this->displayTag)) {
  170. $tag = "</" . $this->displayTag . ">";
  171. }
  172. return $tag;
  173. }
  174. public function view()
  175. {
  176. $templateHandle = $this->getTemplateHandle();
  177. if (in_array($templateHandle, array('date_time', 'boolean'))) {
  178. $this->render('templates/' . $templateHandle);
  179. }
  180. }
  181. }