Gazelle/classes/textarea_preview.class.php

221 lines
5.5 KiB
PHP
Raw Normal View History

2012-10-29 08:00:20 +00:00
<?php
/**
* This super class is used to manage the ammount of textareas there are and to
* generate the required JavaScript that enables the previews to work.
*/
2013-04-30 18:18:07 +00:00
class TEXTAREA_PREVIEW_SUPER {
2012-10-29 08:00:20 +00:00
/**
* @static
* @var int $Textareas Total number of textareas created
*/
2013-06-25 08:00:52 +00:00
protected static $Textareas = 0;
2012-10-29 08:00:20 +00:00
/**
* @static
* @var array $_ID Array of textarea IDs
*/
2013-06-25 08:00:52 +00:00
protected static $_ID = array();
2012-10-29 08:00:20 +00:00
/**
* @static
* @var bool For use in JavaScript method
*/
2013-06-25 08:00:52 +00:00
private static $Exectuted = false;
2012-10-29 08:00:20 +00:00
/**
* This method should only run once with $all as true and should be placed
* in the header or footer.
*
2013-06-27 08:01:06 +00:00
* If $all is true, it includes TextareaPreview and jQuery
2012-10-29 08:00:20 +00:00
*
* jQuery is required for this to work, include it in the headers.
*
* @static
* @param bool $all Output all required scripts, otherwise just do iterator()
* @example <pre><?php TEXT_PREVIEW::JavaScript(); ?></pre>
* @return void
*/
2013-06-25 08:00:52 +00:00
public static function JavaScript ($all = true) {
2013-04-30 18:18:07 +00:00
if (self::$Textareas === 0) {
return;
}
if (self::$Exectuted === false && $all) {
2012-10-29 08:00:20 +00:00
View::parse('generic/textarea/script.phtml');
2013-04-30 18:18:07 +00:00
}
2012-10-29 08:00:20 +00:00
self::$Exectuted = true;
self::iterator();
}
/**
* This iterator generates JavaScript to initialize each JavaScript
* TextareaPreview object.
*
* It will generate a numeric or custom ID related to the textarea.
* @static
* @return void
*/
2013-06-27 08:01:06 +00:00
private static function iterator() {
2012-10-29 08:00:20 +00:00
$script = array();
2013-04-30 18:18:07 +00:00
for ($i = 0; $i < self::$Textareas; $i++) {
2012-10-29 08:00:20 +00:00
if (isset(self::$_ID[$i]) && is_string(self::$_ID[$i])) {
$a = sprintf('%d, "%s"', $i, self::$_ID[$i]);
} else {
$a = $i;
}
$script[] = sprintf('[%s]', $a);
}
2013-04-30 18:18:07 +00:00
if (!empty($script)) {
View::parse('generic/textarea/script_factory.phtml', array('script' => join(', ', $script)));
}
2012-10-29 08:00:20 +00:00
}
}
/**
* Textarea Preview Class
*
* This class generates a textarea that works with the JS preview script.
*
* Templates found in design/views/generic/textarea
*
* @example <pre><?php
* // Create a textarea with a name of content.
* // Buttons and preview divs are generated automatically near the textarea.
* new TEXTAREA_PREVIEW('content');
*
* // Create a textarea with name and id body_text with default text and
* // no buttons or wrap preview divs.
* // Buttons and preview divs are generated manually
* $text = new TEXTAREA_PREVIEW('body_text', 'body_text', 'default text',
2013-04-18 08:00:54 +00:00
* 50, 20, false, false, array('disabled="disabled"', 'class="text"'));
2012-10-29 08:00:20 +00:00
*
* $text->buttons(); // output buttons
*
* $text->preview(); // output preview div
*
* // Create a textarea with custom preview wrapper around a table
* // the table will be (in)visible depending on the toggle
* $text = new TEXTAREA_PREVIEW('body', '', '', 30, 10, false, false);
* $id = $text->getID();
*
* // some template
* <div id="preview_wrap_<?=$id?>">
2013-04-18 08:00:54 +00:00
* <table>
* <tr>
* <td>
* <div id="preview_<?=$id?>"></div>
* </td>
* </tr>
* </table>
2012-10-29 08:00:20 +00:00
* </div>
* </pre>
*/
2013-04-30 18:18:07 +00:00
class TEXTAREA_PREVIEW extends TEXTAREA_PREVIEW_SUPER {
2012-10-29 08:00:20 +00:00
/**
* @var int Unique ID
*/
private $id;
/**
* Flag for preview output
* @var bool $preview
*/
2013-06-23 08:00:54 +00:00
private $preview = false;
2012-10-29 08:00:20 +00:00
2013-01-23 08:00:38 +00:00
/**
* String table
* @var string Buffer
*/
private $buffer = null;
2012-10-29 08:00:20 +00:00
/**
* This method creates a textarea
*
2013-04-18 08:00:54 +00:00
* @param string $Name name attribute
* @param string $ID id attribute
* @param string $Value default text attribute
* @param string $Cols cols attribute
* @param string $Rows rows attribute
* @param bool $Preview add the preview divs near the textarea
* @param bool $Buttons add the edit/preview buttons near the textarea
* @param bool $Buffer doesn't output the textarea, use getBuffer()
* @param array $ExtraAttributes array of attribute="value"
2012-10-29 08:00:20 +00:00
*
2013-01-23 08:00:38 +00:00
* If false for $Preview, $Buttons, or $Buffer, use the appropriate
* methods to add the those elements manually. Alternatively, use getID
* to create your own.
2012-10-29 08:00:20 +00:00
*
2013-01-23 08:00:38 +00:00
* It's important to have the right IDs as they make the JS function properly.
2012-10-29 08:00:20 +00:00
*/
2013-06-27 08:01:06 +00:00
public function __construct($Name, $ID = '', $Value = '', $Cols = 50, $Rows = 10,
2013-01-23 08:00:38 +00:00
$Preview = true, $Buttons = true, $Buffer = false,
array $ExtraAttributes = array()
2012-10-29 08:00:20 +00:00
) {
$this->id = parent::$Textareas;
parent::$Textareas += 1;
array_push(parent::$_ID, $ID);
2013-04-30 18:18:07 +00:00
if (empty($ID)) {
$ID = 'quickpost_' . $this->id;
}
2012-10-29 08:00:20 +00:00
2013-04-30 18:18:07 +00:00
if (!empty($ExtraAttributes)) {
2012-10-29 08:00:20 +00:00
$Attributes = ' ' . implode(' ', $ExtraAttributes);
2013-04-30 18:18:07 +00:00
} else {
2012-10-29 08:00:20 +00:00
$Attributes = '';
2013-04-30 18:18:07 +00:00
}
2012-10-29 08:00:20 +00:00
2013-04-30 18:18:07 +00:00
if ($Preview === true) {
$this->preview();
}
2012-10-29 08:00:20 +00:00
2013-01-23 08:00:38 +00:00
$this->buffer = View::parse('generic/textarea/textarea.phtml', array(
2012-10-29 08:00:20 +00:00
'ID' => $ID,
'NID' => $this->id,
'Name' => &$Name,
'Value' => &$Value,
'Cols' => &$Cols,
'Rows' => &$Rows,
'Attributes' => &$Attributes
2013-01-23 08:00:38 +00:00
), $Buffer);
2012-10-29 08:00:20 +00:00
2013-04-30 18:18:07 +00:00
if ($Buttons === true) {
$this->buttons();
}
2012-10-29 08:00:20 +00:00
}
/**
* Outputs the divs required for previewing the AJAX content
* Will only output once
*/
2013-06-27 08:01:06 +00:00
public function preview() {
2013-04-30 18:18:07 +00:00
if (!$this->preview) {
2012-10-29 08:00:20 +00:00
View::parse('generic/textarea/preview.phtml', array('ID' => $this->id));
2013-04-30 18:18:07 +00:00
}
2012-10-29 08:00:20 +00:00
$this->preview = true;
}
/**
* Outputs the preview and edit buttons
* Can be called many times to place buttons in different areas
*/
2013-06-27 08:01:06 +00:00
public function buttons() {
2012-10-29 08:00:20 +00:00
View::parse('generic/textarea/buttons.phtml', array('ID' => $this->id));
}
/**
* Returns the textarea's numeric ID.
*/
2013-06-27 08:01:06 +00:00
public function getID() {
2012-10-29 08:00:20 +00:00
return $this->id;
}
2013-01-23 08:00:38 +00:00
/**
* Returns textarea string when buffer is enabled in the constructor
* @return string
*/
2013-06-27 08:01:06 +00:00
public function getBuffer() {
2013-01-23 08:00:38 +00:00
return $this->buffer;
}
2013-04-18 08:00:54 +00:00
}