      1: <?php
      2:
      3: /**
      4:  * String 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: StringBox.inc 1624 2012-12-21 12:17:55Z 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 StringBox {
     48:     /**
     49:      * The string object that will be edited by this string box
     50:      *
     51:      * @var String
     52:      * @since 1.0
     53:      */
     54:     public $stringObject;
     55:
     56:     /**
     57:      * The data label for the string 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 display size of the input box
     74:      *
     75:      * @var integer
     76:      * @since 1.0
     77:      *
     78:      */
     79:     public $size;
     80:
     81:     /**
     82:      * The constructor
     83:      *
     84:      * @param String $string The string object that will be edited by this text box.
     85:      * @param string $label The data label for the string object.
     86:      * @param string $name The name of the HTML input box.
     87:      * @param integer $size The display size (characters).
     88:      * @since 1.0
     89:      * @throws IllegalArguementException
     90:      */
     91:     public function __construct($string, $label, $name, $size=0) {
     92:         if($string instanceof String)
     93:             $this->stringObject = $string;
     94:         else
     95:             throw new IllegalArguementException('String object passed ['.var_export($string, true).'] is not a valid String object!');
     96:
     97:         $this->label = $label;
     98:         $this->name = $name;
     99:         $this->size = $size;
    100:     }
    101:
    102:     /**
    103:      * Renders the HTML and javascript for the string box
    104:      *
    105:      * @param bool $tableTags determines if table tags are also rendered for the StringBox
    106:      * @param bool $readOnly set to true to make the text box readonly (defaults to false)
    107:      * @return string
    108:      * @since 1.0
    109:      */
    110:     public function render($tableTags=true, $readOnly=false) {
    111:         $html = '';
    112:
    113:         if ($tableTags) {
    114:             $html .= '<tr><th>';
    115:             $html .= $this->label;
    116:             $html .= '</th>';
    117:
    118:             $html .= '<td>';
    119:             $html .= '<input '.($this->stringObject->checkIsPassword()? 'type="password"':'type="text"').($this->size == 0 ? ' style="width:100%;"' : ' size="'.$this->size.'"').' maxlength="'.String::MAX_SIZE.'" name="'.$this->name.'" id="'.$this->name.'" value="'.((isset($_POST[$this->name]) && $this->stringObject->getValue() == "" && !$this->stringObject->checkIsPassword())? $_POST[$this->name] : $this->stringObject->getValue()).'"'.($readOnly ? 'readonly class="readonly"' : '').'/>';
    120:             $html .= '</td></tr>';
    121:         }else{
    122:             $html .= '<input '.($this->stringObject->checkIsPassword()? 'type="password"':'type="text"').($this->size == 0 ? ' style="width:100%;"' : ' size="'.$this->size.'"').' maxlength="'.String::MAX_SIZE.'" name="'.$this->name.'" id="'.$this->name.'" value="'.((isset($_POST[$this->name]) && $this->stringObject->getValue() == "" && !$this->stringObject->checkIsPassword())? $_POST[$this->name] : $this->stringObject->getValue()).'"'.($readOnly ? 'readonly class="readonly"' : '').'/>';
    123:         }
    124:
    125:         if($this->stringObject->getRule() != '') {
    126:             $html .= '<input type="hidden" id="'.$this->name.'_msg" value="'.$this->stringObject->getHelper().'"/>';
    127:             $html .= '<input type="hidden" id="'.$this->name.'_rule" value="'.$this->stringObject->getRule().'"/>';
    128:         }
    129:
    130:         return $html;
    131:     }
    132:
    133:     /**
    134:      * Setter for string object
    135:      *
    136:      * @param String $string
    137:      * @since 1.0
    138:      * @throws IllegalArguementException
    139:      */
    140:     public function setStringObject($string) {
    141:         if($string instanceof String)
    142:             $this->stringObject = $string;
    143:         else
    144:             throw new IllegalArguementException('String object passed ['.var_export($string, true).'] is not a valid String object!');
    145:     }
    146:
    147:     /**
    148:      * Qetter for string object
    149:      *
    150:      * @return String
    151:      * @since 1.0
    152:      */
    153:     public function getStringObject() {
    154:         return $this->stringObject;
    155:     }
    156: }
    157:
    158: ?>
    159: