mahi /atk4/lib/Form/Field.php

Language PHP Lines 402
MD5 Hash d24e2c6e8b422f673e1743273768df4b
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php // vim:ts=4:sw=4:et:fdm=marker
/*
 * Undocumented
 *
 * @link http://agiletoolkit.org/
*//*
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
/**
 * Implementation of abstract form's field
 *
 * @author      Romans <romans@adevel.com>
 * @copyright   See file COPYING
 * @version     $Id$
 */
abstract class Form_Field extends AbstractView {
    /**
     * Description of the field shown next to it on the form
     */
    public $error_template;      // template used to put errors on the field line
    public $mandatory_template;  // template used to mark mandatory fields
    public $caption;
    protected $value=null;       // use $this->get(), ->set().
    public $short_name=null;
    public $attr=array();
    public $no_save=null;

    public $field_prepend='';
    public $field_append='';

    public $comment='&nbsp;';
    protected $disabled=false;
    protected $mandatory=false;
    public $default_value=null;

    // Field customization
    private $separator='';
    public $show_input_only;
    public $form=null;

    public $button_prepend=null;
    public $button_append=null;

    function init(){
        parent::init();
        if(@$_GET[$this->owner->name.'_cut_field']==$this->name){
            $this->api->addHook('pre-render',array($this,'_cutField'));
        }
    }
    function setForm($form){
        $form->addHook('loadPOST',$this);
        $form->addHook('validate',$this);
        $this->form=$form;
        $this->form->data[$this->short_name] = $this->value;
        $this->value =& $this->form->data[$this->short_name];
        return $this;
    }

    function _cutField(){
        // this method is used by ui.atk4_form, when doing reloadField();
        unset($_GET['cut_object']);
        $this->recursiveRender();
        if($this->api->jquery)$this->api->jquery->getJS($this);
        throw new Exception_StopRender(
            $this->template->renderRegion($this->template->tags['before_field']).
            $this->getInput().
            $this->template->renderRegion($this->template->tags['after_field'])
        );
    }
    function setMandatory($mandatory=true){
        $this->mandatory=$mandatory;
        return $this;
    }
    function setReadonly($readonly=true){
        $this->readonly=$readonly;
        return $this;
    }
    function isMandatory(){
        return $this->mandatory;
    }
    function setCaption($_caption){
        $this->caption=$this->api->_($_caption);
        return $this;
    }
    function displayFieldError($msg=null){
        if(!isset($msg))$msg='Error in field "'.$this->caption.'"';

        $this->form->js(true)
            ->atk4_form('fieldError',$this->short_name,$msg)
            ->execute();

        $this->form->errors[$this->short_name]=$msg;
    }
    function setNoSave(){
        // Field value will not be saved into defined source (such as database)
        $this->no_save=true;
        return $this;
    }
    function disable(){
        // sets 'disabled' property and setNoSave()
        $this->setAttr('disabled');
        $this->setNoSave();
        $this->disabled=true;
        return $this;
    }
    function isDisabled(){
        return $this->disabled;
    }
    function set($value){
        // Use this function when you want to assign $this->value.
        // If you use this function, your field will operate in AJAX mode.
        $this->value=$value;
        return $this;
    }
    /** Position can be either 'before' or 'after' */
    function addButton($label,$options=array()){
        $position='after';
        if(is_string($options)){
            $position=$options;
        }else{
            if(isset($options['position']))$position=$options['position'];
        }
        if($position=='after'){
            return $this->afterField()->add('Button',$options)->setLabel($label);
        }else{
            return $this->beforeField()->add('Button',$options)->setLabel($label);
        }
    }
    function beforeField(){
        if(!$this->template->hasTag('after_input')){
            $el=$this->owner->add('HtmlElement');
            $this->owner->add('Order')->move($el,'before',$this)->now();
            return $el;
        }
        if(!$this->button_prepend)return $this->button_prepend=$this
            ->add('HtmlElement',null,'before_input')->addClass('input-cell');
        return $this->button_prepend;
    }
    function afterField(){
        if(!$this->template->hasTag('after_input')){
            $el=$this->owner->add('HtmlElement');
            $this->owner->add('Order')->move($el,'after',$this)->now();
            return $el;
        }
        if(!$this->button_append)return $this->button_append=$this
            ->add('HtmlElement',null,'after_input')->addClass('input-cell');
        return $this->button_append;
    }
    function aboveField(){
        return $this->add('HtmlElement',null,'before_field');
    }
    function belowField(){
        return $this->add('HtmlElement',null,'after_field');
    }
    function setComment($text=''){
        $this->belowField()->setElement('ins')->set($text);
        return $this;
    }
    function addComment($text=''){
        return $this->belowField()->setElement('ins')->set($text);
    }
    function get(){
        return $this->value;
    }
    function setClass($class){
        $this->attr['class']=$class;
        return $this;
    }
    function addClass($class){
        $this->attr['class'].=($this->attr['class']?' ':'').$class;
        return $this;
    }
    function setAttr($attr,$value=undefined){
        if(is_array($attr)&&$value===undefined){
            foreach($attr as $k=>$v) $this->setAttr($k,$v);
            return $this;
        }
        if($attr){
            $this->attr[$attr] = $value===undefined?'true':$value;
        }
        return $this;
    }
    function setProperty($property,$value){ // synonym, setAttr is preferred
        return $this->setAttr($property,$value);
    }
    function setFieldHint($var_args=null){
        /* Adds a hint after this field. Thes will call Field_Hint->set()
           with same arguments you called this funciton.
         */
        if(!$this->template->hasTag('after_field'))return $this;
        $hint=$this->add('Form_Hint',null,'after_field');
        call_user_func_array(array($hint,'set'), func_get_args());
        return $this;
    }
    function loadPOST(){
        if(isset($_POST[$this->name]))$this->set($_POST[$this->name]);
        else $this->set($this->default_value);
        $this->normalize();
    }
    function normalize(){
        /* Normalization will make sure that entry conforms to the field type. 
           Possible trimming, rounding or length enforcements may happen. */
        $this->hook('normalize');
    }
    function validate(){
        // NoSave and disabled fields should not be validated
        if($this->disabled || $this->no_save)return true;
        // we define "validate" hook, so actual validators could hook it here
        // and perform their checks
        if(is_bool($result = $this->hook('validate')))return $result;
    }
    /** @private - handles field validation callback output */
    function _validateField($caller,$condition,$msg){
        $ret=call_user_func($condition,$this);

        if($ret===false){
            if(is_null($msg))$msg=$this->api->_('Error in ').$this->caption;
            $this->displayFieldError($msg);
        }elseif(is_string($ret)){
            $this->displayFieldError($ret);
        }
        return $this;
    }
    /** Executes a callback. If callback returns string, shows it as error message.
     * If callback returns "false" shows either $msg or a standard error message
     * about field being incorrect */
    function validateField($condition,$msg=null){
        if(is_callable($condition)){
            $this->addHook('validate',array($this,'_validateField'),array($condition,$msg));
        }else{
            $this->addHook('validate',$s='if(!('.$condition.'))$this->displayFieldError("'.
                ($msg?$msg:'Error in ".$this->caption."').'");');
        }
        return $this;
    }
    function _validateNotNull($field){
        if($field->get()==="" || is_null($field->get()))return false;
    }
    /** Adds "X is a mandatory field" message */
    function validateNotNULL($msg=null){
        $this->setMandatory();
        if($msg && $msg!==true){
            $msg=$this->api->_($msg);
        }else{
            $msg=sprintf($this->api->_('%s is a mandatory field'),$this->caption);
        }
        $this->validateField(array($this,'_validateNotNull'),$msg);
        return $this;
    }
    function getInput($attr=array()){
        // This function returns HTML tag for the input field. Derived classes
        // should inherit this and add new properties if needed
        return $this->getTag('input',
                array_merge(array(
                        'name'=>$this->name,
                        'data-shortname'=>$this->short_name,
                        'id'=>$this->name,
                        'value'=>$this->value,
                        ),$attr,$this->attr)
                );
    }
    function setSeparator($separator){
        $this->separator = $separator;
        return $this;
    }
    function render(){
        if($this->show_input_only){
            $this->output($this->getInput());
            return;
        }
        if(!$this->error_template)$this->error_template = $this->form->template_chunks['field_error'];
        if((!property_exists($this, 'mandatory_template')) || (!$this->mandatory_template))$this->mandatory_template=$this->form->template_chunks['field_mandatory'];
        $this->template->trySet('field_caption',$this->caption?($this->caption.$this->separator):'');
        $this->template->trySet('field_name',$this->name);
        $this->template->trySet('field_comment',$this->comment);
        // some fields may not have field_input tag at all...
        if($this->button_prepend || $this->button_append){
            $this->field_prepend.='<div class="input-cell expanded">';
            $this->field_append='</div>'.$this->field_append;
            $this->template->trySetHTML('input_row_start','<div class="input-row">');
            $this->template->trySetHTML('input_row_stop','</div>');
        }
        $this->template->trySetHTML('field_input',$this->field_prepend.$this->getInput().$this->field_append);
        $this->template->trySetHTML('field_error',
                isset($this->form->errors[$this->short_name])?
                $this->error_template->set('field_error_str',$this->form->errors[$this->short_name])->render()
                :''
                );
        if (is_object($this->mandatory_template)) {
            $this->template->trySet('field_mandatory',$this->isMandatory()?$this->mandatory_template->render():'');
        }
        $this->output($this->template->render());
    }
    function getTag($tag, $attr=null, $value=null){
        /**
         * Draw HTML attribute with supplied attributes.
         *
         * Short description how this getTag may be used:
         *
         * Use get tag to build HTML tag.
         * echo getTag('img',array('src'=>'foo.gif','border'=>0);
         *
         * The unobvius advantage of this function is ability to merge
         * attribute arrays. For example, if you have function, which
         * must display img tag, you may add optional $attr argument
         * to this function.
         *
         * function drawImage($src,$attr=array()){
         *     echo getTag('img',array_merge(array('src'=>$src),$attr));
         * }
         *
         * so calling drawImage('foo.gif') will echo: <img src="foo.gif">
         *
         * The benefit from such a function shows up when you use 2nd argument:
         *
         * 1. adding additional attributes
         * drawImage('foo.gif',array('border'=>0'));
         * --> <img src="foo.gif" border="0">
         * (NOTE: you can even have attr templates!)
         *
         * 2. adding no-value attributes, such as nowrap:
         * getTag('td',arary('nowrap'=>true));
         * --> <td nowrap>
         *
         * 3. disabling some attributes.
         * drawImage('foo.gif',array('src'=>false));
         * --> <img>
         *
         * 4. re-defining attributes
         * drawImage('foo.gif',array('src'=>'123'));
         * --> <img src="123">
         *
         * 5. or you even can re-define tag itself
         * drawImage('foo.gif',array(
         *                      ''=>'input',
         *                      'type'=>'picture'));
         * --> <input type="picture" src="foo.gif">
         *
         * 6. xml-valid tags without closing tag
         * getTag('img/',array('src'=>'foo.gif'));
         * --> <img src=>"foo.gif"/>
         *
         * 7. closing tags
         * getTag('/td');
         * --> </td>
         *
         * 8. using $value will add $value after tag followed by closing tag
         * getTag('a',array('href'=>'foo.html'),'click here');
         * --> <a href="foo.html">click here</a>
         *
         * 9. you may not skip attribute argument.
         * getTag('b','text in bold');
         * --> <b>text in bold</b>
         *
         * 10. nesting
         * getTag('a',array('href'=>'foo.html'),getTag('b','click here'));
         * --> <a href="foo.html"><b>click here</b></a>
         */
        if(is_string($attr)){
            $value=$attr;
            $attr=null;
        }
        if(!$attr){
            return "<$tag>".($value?$value."</$tag>":"");
        }
        $tmp = array();
        if(substr($tag,-1)=='/'){
            $tag = substr($tag,0,-1);
            $postfix = '/';
        } else $postfix = '';
        foreach ($attr as $key => $val) {
            if($val === false) continue;
            if($val === true) $tmp[] = "$key";
            elseif($key === '')$tag=$val;
            else $tmp[] = "$key=\"".htmlspecialchars($val)."\"";
        }
        return "<$tag ".join(' ',$tmp).$postfix.">".($value?$value."</$tag>":"");
    }

    function setSource(){
        return call_user_func_array(array($this->form,'setSource'),func_get_args());
    }
    function addField(){
        return call_user_func_array(array($this->form,'addField'),func_get_args());
        //throw new ObsoleteException('$form->addField() now returns Field object and not Form. Do not chain it.');
    }
}

///////// Because many fields are really simple extenions of the base-line field,
///////// they are defined here.

// Visually different fields
Back to Top