      1: <?php
      2:
      3: /**
      4:  * Text HTML input box custom widget
      5:  *
      6:  * @package alpha::view::widgets
      7:  * @since 1.0
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: TextBox.inc 1633 2013-01-22 16:43:56Z alphadevx $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class TextBox {
     48:     /**
     49:      * The text object that will be edited by this text box
     50:      *
     51:      * @var Text
     52:      * @since 1.0
     53:      */
     54:     public $textObject;
     55:
     56:     /**
     57:      * The data label for the text object
     58:      *
     59:      * @var string
     60:      * @since 1.0
     61:      */
     62:     public $label;
     63:
     64:     /**
     65:      * The name of the HTML input box
     66:      *
     67:      * @var string
     68:      * @since 1.0
     69:      */
     70:     public $name;
     71:
     72:     /**
     73:      * The amount of rows to display by default
     74:      *
     75:      * @var integer
     76:      * @since 1.0
     77:      */
     78:     public $rows;
     79:
     80:     /**
     81:      * An optional additional idenitfier to append to the id of the text box where many are on one page
     82:      *
     83:      * @var integer
     84:      * @since 1.0
     85:      */
     86:     public $identifier;
     87:
     88:     /**
     89:      * The constructor
     90:      *
     91:      * @param Text $text The text object that will be edited by this text box.
     92:      * @param string $label The data label for the text object.
     93:      * @param string $name The name of the HTML input box.
     94:      * @param integer $rows The display size (rows).
     95:      * @param integer $identifier An additional idenitfier to append to the id of the text box.
     96:      * @since 1.0
     97:      * @throws IllegalArguementException
     98:      */
     99:     public function __construct($text, $label, $name, $rows=5, $identifier=0) {
    100:         global $config;
    101:
    102:         if($text instanceof Text)
    103:             $this->textObject = $text;
    104:         else
    105:             throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
    106:
    107:         $this->label = $label;
    108:         $this->name = $name;
    109:         $this->rows = $rows;
    110:         $this->identifier = $identifier;
    111:     }
    112:
    113:     /**
    114:      * Renders the HTML and javascript for the text box
    115:      *
    116:      * @param bool $tableTags Determines if table tags are also rendered for the TextBox
    117:      * @param bool $markdown Set to true when rendering Markdown text content fields
    118:      * @return string
    119:      * @since 1.0
    120:      */
    121:     public function render($tableTags=true, $markdown=false) {
    122:         global $config;
    123:
    124:         $html = '';
    125:
    126:         if($tableTags) {
    127:             $html .= '<tr><th colspan="2" style="text-align:center;">';
    128:             $html .= $this->label;
    129:             $html .= '</th></tr>';
    130:
    131:             $html .= '<tr><td colspan="2">';
    132:         }
    133:
    134:         if($markdown) {
    135:             $html .= '<input type="hidden" id="markdownTextBoxRows" name="markdownTextBoxRows" value=""/>';
    136:         }
    137:         $html .= '<textarea class="checkMax" maxlength="'.$this->textObject->getSize().'" id="text_field_'.$this->name.'_'.$this->identifier.'" style="width:100%;" rows="'.$this->rows.'" name="'.$this->name.'">';
    138:
    139:         if($this->textObject->getAllowHTML())
    140:             $html .= InputFilter::decode($this->textObject->getValue(), true);
    141:         else
    142:             $html .= InputFilter::decode($this->textObject->getValue());
    143:
    144:         $html .= '</textarea><br>';
    145:
    146:         if($tableTags) {
    147:             $html .= '</td></tr>';
    148:             $html .= '<tr><td colspan="2">';
    149:         }
    150:
    151:         if($markdown) {
    152:             $increaseTextArea = new Button("document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = ".
    153:                 "(parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows) + 10);".
    154:                 "document.getElementById('markdownTextBoxRows').value = (parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows));",
    155:                 "Increase text area", $this->name."IncBut", $config->get('app.url')."/alpha/images/icons/arrow_down.png");
    156:         }else{
    157:             $increaseTextArea = new Button("document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = ".
    158:                 "(parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows) + 10);",
    159:                 "Increase text area", $this->name."IncBut", $config->get('app.url')."/alpha/images/icons/arrow_down.png");
    160:         }
    161:
    162:         $html .= $increaseTextArea->render();
    163:
    164:         if($markdown) {
    165:             $decreaseTextArea = new Button("if(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows > 10) ".
    166:                 "{document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = (parseInt(document.getElementById('text_field_".
    167:                 $this->name."_".$this->identifier."').rows) - 10)};".
    168:                 "document.getElementById('markdownTextBoxRows').value = (parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows));",
    169:                 "Decrease text area", $this->name."DecBut", $config->get('app.url')."/alpha/images/icons/arrow_up.png");
    170:         }else{
    171:             $decreaseTextArea = new Button("if(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows > 10) ".
    172:                 "{document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = (parseInt(document.getElementById('text_field_".
    173:                 $this->name."_".$this->identifier."').rows) - 10)};",
    174:                 "Decrease text area", $this->name."DecBut", $config->get('app.url')."/alpha/images/icons/arrow_up.png");
    175:         }
    176:
    177:         $html .= $decreaseTextArea->render();
    178:
    179:         if($tableTags)
    180:             $html .= '</td></tr>';
    181:
    182:         if($this->textObject->getRule() != '') {
    183:             $html .= '<input type="hidden" id="'.$this->name.'_msg" value="'.$this->textObject->getHelper().'"/>';
    184:             $html .= '<input type="hidden" id="'.$this->name.'_rule" value="'.$this->textObject->getRule().'"/>';
    185:         }
    186:
    187:         return $html;
    188:     }
    189:
    190:     /**
    191:      * Setter for text object
    192:      *
    193:      * @param string $text
    194:      * @since 1.0
    195:      * @throws IllegalArguementException
    196:      */
    197:     public function setTextObject($text) {
    198:         if($text instanceof Text)
    199:             $this->text = $text;
    200:         else
    201:             throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
    202:     }
    203:
    204:     /**
    205:      * Getter for text object
    206:      *
    207:      * @return Text
    208:      * @since 1.0
    209:      */
    210:     function getTextObject() {
    211:         return $this->textObject;
    212:     }
    213: }
    214:
    215: ?>