SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/views/tinymce4/image_gallery.php

  1. <?php
  2. /*
  3.  * kate: tab-width 4; indent-width 4; space-indent on; word-wrap off; word-wrap-column 120;
  4.  * :tabSize=4:indentSize=4:noTabs=true:wrap=false:maxLineLen=120:mode=php:
  5.  *
  6.  * Copyright (C) 2015 Karmabunny Pty Ltd.
  7.  *
  8.  * This file is a part of SproutCMS.
  9.  *
  10.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  11.  * of the GNU General Public License as published by the Free Software Foundation, either
  12.  * version 3 of the License, or (at your option) any later version.
  13.  *
  14.  * For more information, visit <http://getsproutcms.com>.
  15.  */
  16.  
  17. use Sprout\Helpers\Enc;
  18. use Sprout\Helpers\File;
  19. use Sprout\Helpers\Form;
  20.  
  21.  
  22. $data = [];
  23. $data['captions'] = (int) @$_GET['captions'];
  24. $data['thumbs'] = (int) @$_GET['thumbs'];
  25. $data['max'] = (int) @$_GET['limit'];
  26. $data['cat'] = (int) @$_GET['cat'];
  27. $data['crop'] = @$_GET['crop'];
  28. $data['order'] = (int) @$_GET['order'];
  29. $data['display_opts'] = @$_GET['display_opts'];
  30. $data['slider_dots'] = (int) @$_GET['slider_dots'];
  31. $data['slider_arrows'] = (int) @$_GET['slider_arrows'];
  32. $data['slider_autoplay'] = (int) @$_GET['slider_autoplay'];
  33. $data['slider_speed'] = (int) @$_GET['slider_speed'];
  34.  
  35. Form::setData($data);
  36.  
  37. $category_name = '';
  38. foreach ($categories as $category) {
  39. if ($data['cat'] == $category['id']) {
  40. $category_name = $category['name'];
  41. break;
  42. }
  43. }
  44.  
  45. $order_opts = [
  46. 1 => 'Date (most recent at top)',
  47. 2 => 'Date (oldest at top)',
  48. 3 => 'Alphabetical by name',
  49. 4 => 'Alphabetical (reverse)',
  50. 5 => 'Manual (in category options)',
  51. 6 => 'Stable random',
  52. 7 => 'True random',
  53. ];
  54.  
  55. $crop_opts = [
  56. 'lt' => 'Top left',
  57. 'ct' => 'Top center',
  58. 'rt' => 'Top right',
  59. 'lc' => 'Middle left',
  60. 'cc' => 'Middle center',
  61. 'rc' => 'Middle right',
  62. 'lb' => 'Bottom left',
  63. 'cb' => 'Bottom center',
  64. 'rb' => 'Bottom right',
  65. ];
  66.  
  67. $caption_opts = [
  68. '0' => 'No',
  69. '1' => 'Yes',
  70. ];
  71.  
  72. $type_opts = [
  73. 'grid' => 'Grid gallery',
  74. 'slider' => 'Slider gallery',
  75. ];
  76.  
  77. $thumb_opts = [
  78. '2' => '2',
  79. '3' => '3',
  80. '4' => '4',
  81. '5' => '5',
  82. ];
  83. ?>
  84.  
  85. <link href="ROOT/sprout/media/css/admin_layout.css" rel="stylesheet">
  86.  
  87. <div class="emu-mce-wrapper">
  88. <div class="field-group-wrap -clearfix">
  89. <div class="field-group-item col col--one-half">
  90. <?php
  91. Form::nextFieldDetails('Max number of images to show', false);
  92. echo Form::text('max', ['class' => 'js-gallery-max']);
  93. ?>
  94. </div>
  95. <div class="field-group-item col col--one-half">
  96. <?php
  97. Form::nextFieldDetails('Display order', false);
  98. echo Form::dropdown('order', ['class' => 'js-ordering'], $order_opts);
  99. ?>
  100.  
  101. </div>
  102. </div>
  103. <div class="field-group-wrap -clearfix">
  104. <div class="field-group-item col col--one-third">
  105. <?php
  106. Form::nextFieldDetails('Show captions?', false);
  107. echo Form::dropdown('captions', ['class' => 'js-gallery-captions'], $caption_opts);
  108. ?>
  109. </div>
  110. <div class="field-group-item col col--one-third">
  111. <?php
  112. Form::nextFieldDetails('Cropping anchor', false);
  113. echo Form::dropdown('crop', ['class' => 'js-gallery-crop'], $crop_opts);
  114. ?>
  115. </div>
  116. <div class="field-group-item col col--one-third">
  117. <?php
  118. Form::nextFieldDetails('Gallery type', false);
  119. echo Form::dropdown('display_opts', ['class' => 'js-gallery-type'], $type_opts);
  120. ?>
  121. </div>
  122. </div>
  123.  
  124. <div class="field-group-wrap -clearfix js-gallery-grid">
  125. <div class="field-group-item">
  126. <?php
  127. Form::nextFieldDetails('Thumbnails per row', false);
  128. echo Form::dropdown('thumbs', ['class' => 'js-gallery-thumbs'], $thumb_opts);
  129. ?>
  130. </div>
  131. </div>
  132.  
  133. <div class="field-group-wrap -clearfix js-gallery-slider">
  134. <div class="field-group-wrap -clearfix">
  135. <div class="field-group-item col col--one-half">
  136. <?php
  137. Form::nextFieldDetails('Slider options', false);
  138. echo Form::checkboxList(['slider_dots' => 'Dots', 'slider_arrows' => 'Arrows', 'slider_autoplay' => 'Auto-scroll'], []);
  139. ?>
  140. </div>
  141. <div class="field-group-item col col--one-half">
  142. <?php
  143. Form::nextFieldDetails('Auto-scroll timer', false, 'Seconds');
  144. echo Form::number('slider_speed', []);
  145. ?>
  146. </div>
  147. </div>
  148. </div>
  149.  
  150. </div>
  151.  
  152. <p><button type="button" class="button right js-gallery-save">Save changes</button></p>
  153.  
  154. <hr>
  155.  
  156. <div class="info">
  157. <?php if (!empty($category_name)): ?>Currently chosen <strong><?php echo Enc::html($category_name); ?></strong>.<?php endif; ?>
  158. Choose a category of images from the gallery below to save changes.
  159. </div>
  160.  
  161. <div class="gallery-wrap">
  162.  
  163. <?php foreach ($categories as $category): ?>
  164. <?php
  165. $thumbs = explode('|', $category['filenames']);
  166. $index = 0;
  167. ?>
  168.  
  169. <a href="javascript:;" data-id="<?php echo Enc::html($category['id']); ?>" data-title="<?php echo Enc::html($category['name']); ?>" class="js-gallery-cat <?php if ($category['id'] == $data['cat']): echo 'active'; endif; ?>">
  170. <div class="multi-thumb">
  171.  
  172. <?php foreach ($thumbs as $filename): ?>
  173. <?php if (File::exists($filename)): ?>
  174. <img src="<?php echo File::resizeUrl($filename, 'c102x102'); ?>" class="idx <?php echo Enc::html($index); ?>">
  175. <?
  176. $index++;
  177. if ($index == 3) break;
  178. ?>
  179. <?php endif; ?>
  180. <?php endforeach; ?>
  181.  
  182. </div>
  183. <div class="name"><?php echo Enc::html($category['name']); ?></div>
  184. </a>
  185. <?php endforeach; ?>
  186.  
  187. <div style="clear: both;"></div>
  188. </div>
  189.  
  190. <script>
  191.  
  192. $(document).ready(function() {
  193. // Category thumb match height
  194. if (jQuery().matchHeight) {
  195. $(".gallery-wrap > a").matchHeight();
  196. }
  197.  
  198. // Category thumb click event
  199. $('a.js-gallery-cat').click(function() {
  200. $('a.js-gallery-cat').removeClass('active');
  201. $(this).addClass('active');
  202. saveChanges();
  203. });
  204.  
  205. // Save changes click event
  206. $('.js-gallery-save').click(function() {
  207. saveChanges();
  208. });
  209.  
  210. // Gallery type drop-down event handler
  211. $('.js-gallery-type').on('change', function() {
  212. if ($(this).val() == 'grid') {
  213. $('.js-gallery-grid').show();
  214. $('.js-gallery-slider').hide();
  215. } else if ($(this).val() == 'slider') {
  216. $('.js-gallery-grid').hide();
  217. $('.js-gallery-slider').show();
  218. } else {
  219. $('.js-gallery-grid').hide();
  220. $('.js-gallery-slider').hide();
  221. }
  222. });
  223.  
  224. $('.js-gallery-type').change();
  225. });
  226.  
  227. function saveChanges() {
  228. // Get form values
  229. var thumbs = parseInt($('select.js-gallery-thumbs').val());
  230. var captions = parseInt($('select.js-gallery-captions').val());
  231. var max = parseInt($('input.js-gallery-max').val());
  232. var crop = $('select.js-gallery-crop').val();
  233. var cat = parseInt($('.gallery-wrap a.active').attr('data-id'));
  234. var display_opts = $('select.js-gallery-type').val();
  235. var order_opts = parseInt($('select.js-ordering').val());
  236. var slider_dots = parseInt($('input[name="slider_dots"]').val());
  237. var slider_arrows = parseInt($('input[name="slider_arrows"]').val());
  238. var slider_autoplay = parseInt($('input[name="slider_autoplay"]').val());
  239. var slider_speed = parseInt($('input[name="slider_speed"]').val());
  240.  
  241. if (isNaN(thumbs) || thumbs <= 0) thumbs = 5;
  242. if (isNaN(captions)) captions = 0;
  243. if (isNaN(max) || max <= 0) max = 100;
  244. if (crop == '') crop = 'cc';
  245. if (isNaN(cat) || cat < 0) cat = 0;
  246. if (display_opts == '') display_opts = 'grid';
  247. if (isNaN(order_opts) || order_opts <= 0) order_opts = 1;
  248. if (isNaN(slider_dots) || slider_dots < 0) slider_dots = 0;
  249. if (isNaN(slider_arrows) || slider_arrows < 0) slider_arrows = 0;
  250. if (isNaN(slider_autoplay) || slider_autoplay < 0) slider_autoplay = 0;
  251. if (isNaN(slider_speed) || slider_speed <= 0) slider_speed = 3;
  252.  
  253. // Use form values as data-attributes for Widget's settings
  254. var content = '<div class="sprout-editor--widget sprout-editor--gallery" data-id="' + cat + '"';
  255. content += ' data-max="' + max + '"';
  256. content += ' data-captions="' + captions + '"';
  257. content += ' data-crop="' + crop + '"';
  258. content += ' data-thumbs="' + thumbs + '"';
  259. content += ' data-type="' + display_opts + '"';
  260. content += ' data-ordering="' + order_opts + '"';
  261. content += ' data-slider-dots="' + slider_dots + '"';
  262. content += ' data-slider-arrows="' + slider_arrows + '"';
  263. content += ' data-slider-autoplay="' + slider_autoplay + '"';
  264. content += ' data-slider-speed="' + slider_speed + '"';
  265. content += '>' + $('.gallery-wrap a.active').attr('data-title') + ' Gallery</div>';
  266.  
  267. // Update or insert new div
  268. var elem = top.tinymce.activeEditor.selection.getNode();
  269. if (elem.nodeName == 'DIV' && $(elem).hasClass('sprout-editor--gallery')) {
  270. var nu = document.createElement('div');
  271. nu.innerHTML = content;
  272. top.tinymce.activeEditor.dom.replace(nu.firstChild, elem);
  273. } else {
  274. top.tinymce.activeEditor.selection.setContent(content, {'format': 'raw'});
  275. }
  276.  
  277. TinyMCE4.closePopup();
  278. }
  279.  
  280. </script>
  281.