      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 1548 2012-07-29 17:07:07Z 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:         if($text instanceof Text)
    101:             $this->textObject = $text;
    102:         else
    103:             throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
    104:
    105:         $this->label = $label;
    106:         $this->name = $name;
    107:         $this->rows = $rows;
    108:         $this->identifier = $identifier;
    109:     }
    110:
    111:     /**
    112:      * Renders the HTML and javascript for the text box
    113:      *
    114:      * @param bool $tableTags Determines if table tags are also rendered for the TextBox
    115:      * @param bool $markdown Set to true when rendering Markdown text content fields
    116:      * @return string
    117:      * @since 1.0
    118:      */
    119:     public function render($tableTags=true, $markdown=false) {
    120:         global $config;
    121:
    122:         $html = '';
    123:
    124:         if($tableTags) {
    125:             $html .= '<tr><th colspan="2" style="text-align:center;">';
    126:             $html .= $this->label;
    127:             $html .= '</th></tr>';
    128:
    129:             $html .= '<tr><td colspan="2">';
    130:         }
    131:
    132:         if($markdown) {
    133:             $html .= '<input type="hidden" id="markdownTextBoxRows" name="markdownTextBoxRows" value=""/>';
    134:         }
    135:         $html .= '<textarea class="checkMax" maxlength="'.$this->textObject->getSize().'" id="text_field_'.$this->name.'_'.$this->identifier.'" style="width:100%;" rows="'.$this->rows.'" name="'.$this->name.'">';
    136:
    137:         if($this->textObject->getAllowHTML())
    138:             $html .= InputFilter::decode($this->textObject->getValue(), true);
    139:         else
    140:             $html .= InputFilter::decode($this->textObject->getValue());
    141:
    142:         $html .= '</textarea><br>';
    143:
    144:         if($tableTags) {
    145:             $html .= '</td></tr>';
    146:             $html .= '<tr><td colspan="2">';
    147:         }
    148:
    149:         if($markdown) {
    150:             $increaseTextArea = new Button("document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = ".
    151:                 "(parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows) + 10);".
    152:                 "document.getElementById('markdownTextBoxRows').value = (parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows));",
    153:                 "Increase text area", $this->name."IncBut", $config->get('app.url')."/alpha/images/icons/arrow_down.png");
    154:         }else{
    155:             $increaseTextArea = new Button("document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = ".
    156:                 "(parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows) + 10);",
    157:                 "Increase text area", $this->name."IncBut", $config->get('app.url')."/alpha/images/icons/arrow_down.png");
    158:         }
    159:
    160:         $html .= $increaseTextArea->render();
    161:
    162:         if($markdown) {
    163:             $decreaseTextArea = new Button("if(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows > 10) ".
    164:                 "{document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = (parseInt(document.getElementById('text_field_".
    165:                 $this->name."_".$this->identifier."').rows) - 10)};".
    166:                 "document.getElementById('markdownTextBoxRows').value = (parseInt(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows));",
    167:                 "Decrease text area", $this->name."DecBut", $config->get('app.url')."/alpha/images/icons/arrow_up.png");
    168:         }else{
    169:             $decreaseTextArea = new Button("if(document.getElementById('text_field_".$this->name."_".$this->identifier."').rows > 10) ".
    170:                 "{document.getElementById('text_field_".$this->name."_".$this->identifier."').rows = (parseInt(document.getElementById('text_field_".
    171:                 $this->name."_".$this->identifier."').rows) - 10)};",
    172:                 "Decrease text area", $this->name."DecBut", $config->get('app.url')."/alpha/images/icons/arrow_up.png");
    173:         }
    174:
    175:         $html .= $decreaseTextArea->render();
    176:
    177:         if($tableTags)
    178:             $html .= '</td></tr>';
    179:
    180:         if($this->textObject->getRule() != '') {
    181:             $html .= '<input type="hidden" id="'.$this->name.'_msg" value="'.$this->textObject->getHelper().'"/>';
    182:             $html .= '<input type="hidden" id="'.$this->name.'_rule" value="'.$this->textObject->getRule().'"/>';
    183:         }
    184:
    185:         return $html;
    186:     }
    187:
    188:     /**
    189:      * Setter for text object
    190:      *
    191:      * @param string $text
    192:      * @since 1.0
    193:      * @throws IllegalArguementException
    194:      */
    195:     public function setTextObject($text) {
    196:         if($text instanceof Text)
    197:             $this->text = $text;
    198:         else
    199:             throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
    200:     }
    201:
    202:     /**
    203:      * Getter for text object
    204:      *
    205:      * @return Text
    206:      * @since 1.0
    207:      */
    208:     function getTextObject() {
    209:         return $this->textObject;
    210:     }
    211: }
    212:
    213: ?>
    214: