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

/resposta_free_package/blocks/resposta_flex_slider/controller.php

https://bitbucket.org/c5labs/resposta-framework
PHP | 210 lines | 156 code | 37 blank | 17 comment | 9 complexity | 613d9a2762dc205542dccde2b7758234 MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die(_("Access Denied."));
  2. class RespostaFlexSliderBlockController extends BlockController {
  3. protected $btTable = 'btFlexSlider'; //Must be the same as table name in db.xml.
  4. //Should correspond to block's directory/class name.
  5. public function getBlockTypeName() {
  6. return t('Flex Slider'); //Appears in "Add Block" list when adding blocks to a page
  7. }
  8. public function getBlockTypeDescription() {
  9. return t('An awesome, fully responsive jQuery slider plugin.'); //Only appears in dashboard "Add Functionality" page
  10. }
  11. //Default values for new blocks...
  12. //Note that if you disable controls below, the defaults here serve as
  13. // "permanent" or "hard-coded" values that the user can never change.
  14. private $defaultLargeWidth = 0;
  15. private $defaultLargeHeight = 0;
  16. private $defaultCropLarge = false;
  17. private $defaultThumbWidth = 0;
  18. private $defaultThumbHeight = 0;
  19. private $defaultCropThumb = false;
  20. private $defaultRandomize = false;
  21. //Add/Edit interface configuration...
  22. private $showLargeControls = true;
  23. private $showThumbControls = false;
  24. //Caching is disabled while in development,
  25. // but you should change these to TRUE for production.
  26. protected $btCacheBlockRecord = false;
  27. protected $btCacheBlockOutput = false;
  28. protected $btCacheBlockOutputOnPost = false;
  29. protected $btCacheBlockOutputForRegisteredUsers = false;
  30. protected $btCacheBlockOutputLifetime = 300;
  31. /* DONE! You generally don't need to change anything below this line.
  32. **************************************************************************************************/
  33. protected $btInterfaceWidth = "500";
  34. protected $btInterfaceHeight = "200";
  35. public function getJavaScriptStrings() {
  36. return array(
  37. 'fileset-required' => t('You must choose a file set.'),
  38. );
  39. }
  40. public function add() {
  41. $this->set('fsID', 0);
  42. $this->setFileSets();
  43. $this->setInterfaceSettings();
  44. //Default values for new blocks...
  45. $this->randomize = $this->defaultRandomize;
  46. $this->largeWidth = $this->defaultLargeWidth;
  47. $this->largeHeight = $this->defaultLargeHeight;
  48. $this->cropLarge = $this->defaultCropLarge;
  49. $this->thumbWidth = $this->defaultThumbWidth;
  50. $this->thumbHeight = $this->defaultThumbHeight;
  51. $this->cropThumb = $this->defaultCropThumb;
  52. $this->setNormalizedValues();
  53. }
  54. public function edit() {
  55. $this->setFileSets();
  56. $this->setInterfaceSettings();
  57. $this->setNormalizedValues();
  58. }
  59. private function setNormalizedValues() {
  60. //Don't show 0 for empty widths/heights...
  61. $this->set('largeWidth', empty($this->largeWidth) ? '' : $this->largeWidth);
  62. $this->set('largeHeight', empty($this->largeHeight) ? '' : $this->largeHeight);
  63. $this->set('thumbWidth', empty($this->thumbWidth) ? '' : $this->thumbWidth);
  64. $this->set('thumbHeight', empty($this->thumbHeight) ? '' : $this->thumbHeight);
  65. $this->set('cropLarge', (empty($this->largeWidth) && empty($this->largeHeight)) ? '-1' : ($this->cropLarge ? 1 : 0));
  66. $this->set('cropThumb', $this->cropThumb ? 1 : 0);
  67. }
  68. private function setInterfaceSettings() {
  69. $filesetsToolsURL = Loader::helper('concrete/urls')->getToolsURL('fileset_select_options', $this->getPkgHandle());
  70. $this->set('filesetsToolURL', $filesetsToolsURL);
  71. $this->set('showLargeControls', $this->showLargeControls);
  72. $this->set('showThumbControls', $this->showThumbControls);
  73. }
  74. //Internal helper function (this isn't extending a block_type_controller method)
  75. private function getPkgHandle() {
  76. return BlockType::getByHandle($this->btHandle)->getPackageHandle();
  77. }
  78. private function setFileSets() {
  79. Loader::model('file_set');
  80. $fileSets = FileSet::getMySets();
  81. $this->set('fileSets', $fileSets);
  82. }
  83. public function save($data) {
  84. $data['largeWidth'] = intval($data['largeWidth']);
  85. $data['largeHeight'] = intval($data['largeHeight']);
  86. $data['thumbWidth'] = intval($data['thumbWidth']);
  87. $data['thumbHeight'] = intval($data['thumbHeight']);
  88. $data['cropLarge'] = (intval($data['cropLarge']) < 1) ? 0 : 1; //Watch out for the "-1" option
  89. parent::save($data);
  90. }
  91. public function view() {
  92. $files = $this->getFilesetImages($this->fsID, $this->randomize);
  93. $images = $this->processImageFiles($files, $this->largeWidth, $this->largeHeight, $this->cropLarge, $this->thumbWidth, $this->thumbHeight, $this->cropThumbs);
  94. $this->set('images', $images);
  95. }
  96. static function getFilesetImages($fsID, $randomize = false) {
  97. Loader::model('file_set');
  98. Loader::model('file_list');
  99. $fs = FileSet::getByID($fsID);
  100. $fl = new FileList();
  101. $fl->filterBySet($fs);
  102. $fl->filterByType(FileType::T_IMAGE);
  103. $fl->setPermissionLevel('canRead');
  104. if ($randomize) {
  105. $fl->sortBy('RAND()', 'asc');
  106. } else {
  107. $fl->sortByFileSetDisplayOrder(); //Requires 5.4.1 or higher: version_compare(APP_VERSION, '5.4.1', '>=');
  108. }
  109. $files = $fl->get();
  110. return $files;
  111. }
  112. private function processImageFiles($imageFiles, $largeWidth, $largeHeight, $cropLarge, $thumbWidth, $thumbHeight, $cropThumbs) {
  113. $ih = version_compare(APP_VERSION, '5.4.2', '>=') ? Loader::helper('image') : Loader::helper('cropping_image', $this->getPkgHandle());
  114. $nh = Loader::helper('navigation');
  115. $resizeLarge = ($largeWidth > 0 || $largeHeight > 0);
  116. $resizeLargeWidth = empty($largeWidth) ? 9999 : $largeWidth;
  117. $resizeLargeHeight = empty($largeHeight) ? 9999 : $largeHeight;
  118. $resizeThumb = ($thumbWidth > 0 || $thumbHeight > 0);
  119. $resizeThumbWidth = empty($thumbWidth) ? 9999 : $thumbWidth;
  120. $resizeThumbHeight = empty($thumbHeight) ? 9999 : $thumbHeight;
  121. $maxOrigWidth = 0;
  122. $maxOrigHeight = 0;
  123. $maxLargeWidth = 0;
  124. $maxLargeHeight = 0;
  125. $maxThumbWidth = 0;
  126. $maxThumbHeight = 0;
  127. $images = array();
  128. foreach ($imageFiles as $f) {
  129. $image = new StdClass;
  130. //Metadata...
  131. $image->fID = $f->fID;
  132. $image->titleRaw = $f->getTitle();
  133. $image->title = htmlspecialchars($image->titleRaw, ENT_QUOTES, APP_CHARSET);
  134. $image->descriptionRaw = $f->getDescription();
  135. $image->description = htmlspecialchars($image->descriptionRaw, ENT_QUOTES, APP_CHARSET);
  136. $linkToCID = $f->getAttribute('gallery_link_to_cid'); //To make this work: 1) Install http://www.concrete5.org/marketplace/addons/page-selector-attribute/ . 2) Go to Dashboard -> Sitewide Settings -> Attributes, check the box under the "File" column for the "Page Selector" attribute type. 3) Go to Dashboard -> File Manager -> Attributes, find the "Choose Attribute Type" dropdown at the bottom, select "Page Selector" from the dropdown, click "Go" button. Handle should be "gallery_link_to_cid" (no quotes), name can be whatever you want ("Link To Page" might be good). Ignore the "Searchable" checkboxes. When done, click the "Add Attribute" button. 4) Now users can choose the page an image will link to by setting this property (click on an image in the file manager, choose "Properties" from the popup menu, find this attribute at the bottom of the list, edit it there and save).
  137. $image->linkUrl = empty($linkToCID) ? '' : $nh->getLinkToCollection(Page::getByID($linkToCID));
  138. //Original Image (full size)...
  139. $image->orig = new StdClass;
  140. $image->orig->src = $f->getRelativePath();
  141. $size = getimagesize($f->getPath());
  142. $image->orig->width = $size[0];
  143. $image->orig->height = $size[1];
  144. $maxOrigWidth = ($image->orig->width > $maxOrigWidth) ? $image->orig->width : $maxOrigWidth;
  145. $maxOrigHeight = ($image->orig->height > $maxOrigHeight) ? $image->orig->height : $maxOrigHeight;
  146. //"Large" Size...
  147. if (!$resizeLarge) {
  148. $image->large = $image->orig;
  149. } else {
  150. $image->large = $ih->getThumbnail($f, $resizeLargeWidth, $resizeLargeHeight, $cropLarge);
  151. }
  152. $maxLargeWidth = ($image->large->width > $maxLargeWidth) ? $image->large->width : $maxLargeWidth;
  153. $maxLargeHeight = ($image->large->height > $maxLargeHeight) ? $image->large->height : $maxLargeHeight;
  154. //Thumbnail...
  155. if (!$resizeThumb) {
  156. $image->thumb = $image->orig;
  157. } else {
  158. $image->thumb = $ih->getThumbnail($f, $resizeThumbWidth, $resizeThumbHeight, $cropThumbs);
  159. }
  160. $maxThumbWidth = ($image->thumb->width > $maxThumbWidth) ? $image->thumb->width : $maxThumbWidth;
  161. $maxThumbHeight = ($image->thumb->height > $maxThumbHeight) ? $image->thumb->height : $maxThumbHeight;
  162. $images[] = $image;
  163. }
  164. //These may come in handy to the view...
  165. $this->set('maxOrigWidth', $maxOrigWidth);
  166. $this->set('maxOrigHeight', $maxOrigHeight);
  167. $this->set('maxLargeWidth', $maxLargeWidth);
  168. $this->set('maxLargeHeight', $maxLargeHeight);
  169. $this->set('maxThumbWidth', $maxThumbWidth);
  170. $this->set('maxThumbHeight', $maxThumbHeight);
  171. return $images;
  172. }
  173. }