BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

textarea_preview.class.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * This super class is used to manage the ammount of textareas there are
  5. * and to generate the required JavaScript that enables the previews to work.
  6. */
  7. class TEXTAREA_PREVIEW_SUPER
  8. {
  9. /**
  10. * @static
  11. * @var int $Textareas Total number of textareas created
  12. */
  13. protected static $Textareas = 0;
  14. /**
  15. * @static
  16. * @var array $_ID Array of textarea IDs
  17. */
  18. protected static $_ID = [];
  19. /**
  20. * @static
  21. * @var bool For use in JavaScript method
  22. */
  23. private static $Exectuted = false;
  24. /**
  25. * This method should only run once with $all as true and should be placed
  26. * in the header or footer.
  27. *
  28. * If $all is true, it includes TextareaPreview and jQuery
  29. *
  30. * jQuery is required for this to work, include it in the headers.
  31. *
  32. * @static
  33. * @param bool $all Output all required scripts, otherwise just do iterator()
  34. * @example <pre><?php TEXT_PREVIEW::JavaScript(); ?></pre>
  35. * @return void
  36. */
  37. public static function JavaScript($all = true)
  38. {
  39. if (self::$Textareas === 0) {
  40. return;
  41. }
  42. if (self::$Exectuted === false && $all) {
  43. View::parse('generic/textarea/script.phtml');
  44. }
  45. self::$Exectuted = true;
  46. self::iterator();
  47. }
  48. /**
  49. * This iterator generates JavaScript to initialize each JavaScript
  50. * TextareaPreview object.
  51. *
  52. * It will generate a numeric or custom ID related to the textarea.
  53. * @static
  54. * @return void
  55. */
  56. private static function iterator()
  57. {
  58. $script = [];
  59. for ($i = 0; $i < self::$Textareas; $i++) {
  60. if (isset(self::$_ID[$i]) && is_string(self::$_ID[$i])) {
  61. $a = sprintf('%d, "%s"', $i, self::$_ID[$i]);
  62. } else {
  63. $a = $i;
  64. }
  65. $script[] = sprintf('[%s]', $a);
  66. }
  67. if (!empty($script)) {
  68. View::parse('generic/textarea/script_factory.phtml', array('script' => join(', ', $script)));
  69. }
  70. }
  71. }
  72. /**
  73. * Textarea Preview Class
  74. *
  75. * This class generates a textarea that works with the JS preview script.
  76. * Templates found in design/views/generic/textarea.
  77. *
  78. * @example <pre><?php
  79. * // Create a textarea with a name of content.
  80. * // Buttons and preview divs are generated automatically near the textarea.
  81. * new TEXTAREA_PREVIEW('content');
  82. *
  83. * // Create a textarea with name and id body_text with default text and
  84. * // no buttons or wrap preview divs.
  85. * // Buttons and preview divs are generated manually
  86. * $text = new TEXTAREA_PREVIEW('body_text', 'body_text', 'default text',
  87. * 50, 20, false, false, array('disabled="disabled"', 'class="text"'));
  88. *
  89. * $text->buttons(); // output buttons
  90. * $text->preview(); // output preview div
  91. *
  92. * // Create a textarea with custom preview wrapper around a table
  93. * // the table will be (in)visible depending on the toggle
  94. * $text = new TEXTAREA_PREVIEW('body', '', '', 30, 10, false, false);
  95. * $id = $text->getID();
  96. *
  97. * // some template
  98. * <div id="preview_wrap_<?=$id?>">
  99. * <table>
  100. * <tr>
  101. * <td>
  102. * <div id="preview_<?=$id?>"></div>
  103. * </td>
  104. * </tr>
  105. * </table>
  106. * </div>
  107. * </pre>
  108. */
  109. class TEXTAREA_PREVIEW extends TEXTAREA_PREVIEW_SUPER
  110. {
  111. /**
  112. * @var int Unique ID
  113. */
  114. private $id;
  115. /**
  116. * Flag for preview output
  117. * @var bool $preview
  118. */
  119. private $preview = false;
  120. /**
  121. * String table
  122. * @var string Buffer
  123. */
  124. private $buffer = null;
  125. /**
  126. * This method creates a textarea
  127. *
  128. * @param string $Name name attribute
  129. * @param string $ID id attribute
  130. * @param string $Value default text attribute
  131. * @param string $Cols cols attribute
  132. * @param string $Rows rows attribute
  133. * @param bool $Preview add the preview divs near the textarea
  134. * @param bool $Buttons add the edit/preview buttons near the textarea
  135. * @param bool $Buffer doesn't output the textarea, use getBuffer()
  136. * @param array $ExtraAttributesarray of attribute="value"
  137. *
  138. * If false for $Preview, $Buttons, or $Buffer, use the appropriate
  139. * methods to add the those elements manually. Alternatively, use getID
  140. * to create your own.
  141. *
  142. * It's important to have the right IDs as they make the JS function properly.
  143. */
  144. public function __construct(
  145. $Name,
  146. $ID = '',
  147. $Value = '',
  148. $Placeholder = '',
  149. $Cols = 40,
  150. $Rows = 20,
  151. $Preview = true,
  152. $Buttons = true,
  153. $Buffer = false,
  154. $ExtraAttributes = []
  155. ) {
  156. $this->id = parent::$Textareas;
  157. parent::$Textareas += 1;
  158. array_push(parent::$_ID, $ID);
  159. if (empty($ID)) {
  160. $ID = 'quickpost_' . $this->id;
  161. }
  162. if (!empty($ExtraAttributes)) {
  163. $Attributes = ' ' . implode(' ', $ExtraAttributes);
  164. } else {
  165. $Attributes = '';
  166. }
  167. if ($Preview === true) {
  168. $this->preview();
  169. }
  170. $this->buffer = View::parse(
  171. 'generic/textarea/textarea.phtml',
  172. array(
  173. 'ID' => $ID,
  174. 'NID' => $this->id,
  175. 'Name' => &$Name,
  176. 'Value' => &$Value,
  177. 'Placeholder' => &$Placeholder,
  178. 'Cols' => &$Cols,
  179. 'Rows' => &$Rows,
  180. 'Attributes' => &$Attributes
  181. ),
  182. $Buffer
  183. );
  184. if ($Buttons === true) {
  185. $this->buttons();
  186. }
  187. }
  188. /**
  189. * Outputs the divs required for previewing the AJAX content
  190. * Will only output once
  191. */
  192. public function preview()
  193. {
  194. if (!$this->preview) {
  195. View::parse('generic/textarea/preview.phtml', array('ID' => $this->id));
  196. }
  197. $this->preview = true;
  198. }
  199. /**
  200. * Outputs the preview and edit buttons
  201. * Can be called many times to place buttons in different areas
  202. */
  203. public function buttons()
  204. {
  205. View::parse('generic/textarea/buttons.phtml', array('ID' => $this->id));
  206. }
  207. /**
  208. * Returns the textarea's numeric ID.
  209. */
  210. public function getID()
  211. {
  212. return $this->id;
  213. }
  214. /**
  215. * Returns textarea string when buffer is enabled in the constructor
  216. * @return string
  217. */
  218. public function getBuffer()
  219. {
  220. return $this->buffer;
  221. }
  222. }