Oppaitime'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.2KB

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