Dotclear


Ignore:
Timestamp:
08/16/13 15:48:37 (12 years ago)
Author:
Dsls
Branch:
twig
Message:
  • reworked breadcrumb
  • tuned checkboxes (need to see if it works for multiple values)
  • magic methods for dcForm are more intuitive now, getField method added
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/class.dc.form.php

    r1319 r1414  
    277277     public function renderField($name,$attributes=array(),$extra=array()) 
    278278     { 
    279           $field = $this->currentForm->$name; 
     279          $field = $this->currentForm->getField($name); 
    280280          if ($field) { 
    281281               $attr = $field->getAttributes($attributes); 
     
    396396     /** @var array(dcField) list of form errors */ 
    397397     protected $errors; 
    398  
     398     /** @var array() list of form properties */ 
     399     protected $properties; 
     400      
     401      
    399402    /** 
    400403     * Class constructor 
     
    420423          $this->hiddenfields = array(); 
    421424          $this->errors = array(); 
     425          $this->properties = array(); 
    422426          if ($method == 'POST') { 
    423427               $this->addNonce(); 
     
    425429     } 
    426430 
     431      
     432    /** 
     433     * setProperty - sets form property 
     434     *  
     435     * @param string $name the property name. 
     436     * @param mixed $value the property value. 
     437     * 
     438     * @access public 
     439     */ 
     440     public function setProperty($prop,$value) { 
     441          $this->properties[$prop]=$value; 
     442     } 
     443      
     444    /** 
     445     * getProperty - gets form property 
     446     *  
     447     * @param string $name the property name. 
     448      * 
     449     * @return mixed the property value, null if no property found. 
     450     * @access public 
     451     */    
     452     public function getProperty($prop) { 
     453          if (isset($this->properties[$prop])) { 
     454               return $this->properties[$prop]; 
     455          } else { 
     456               return null; 
     457          } 
     458     } 
    427459    /** 
    428460     * addTemplate - Adds a template file to enrich form fields 
     
    532564     } 
    533565 
     566    /** 
     567     * getField - retrieves a field form form 
     568     * 
     569     * @param string the field name 
     570     * 
     571     * @access public 
     572     * 
     573     * @return dcForm the requested field 
     574     */    
     575      public function getField($name) { 
     576          if (isset($this->fields[$name])) { 
     577               return $this->fields[$name]; 
     578          } else { 
     579               return null; 
     580          } 
     581     } 
     582      
    534583    /** 
    535584     * removeField - removes a field 
     
    634683     public function __get($name) 
    635684     { 
    636           return isset($this->fields[$name]) ? 
    637                $this->fields[$name] : null; 
     685          if (isset($this->fields[$name])) { 
     686               $f = $this->fields[$name]; 
     687               if ($f->isMultiple()) { 
     688                    return $f->getValues(); 
     689               } else { 
     690                    return $f->getValue(); 
     691               } 
     692          } else { 
     693               return $this->getProperty($name); 
     694          } 
    638695     } 
    639696 
     
    650707     { 
    651708          if (isset($this->fields[$name])) { 
    652                $this->fields[$name]->setValue($value); 
    653           } 
    654      } 
    655  
    656 /*   public function isSubmitted() 
    657      { 
    658           $from = $this->method == 'POST' ? $_POST : $_GET; 
    659      } 
    660 */ 
     709               $f = $this->fields[$name]; 
     710               if ($f->isMultiple()) { 
     711                    $this->fields[$name]->setValues($value); 
     712               } else { 
     713                    $this->fields[$name]->setValue($value); 
     714               } 
     715          } else { 
     716               $this->setProperty($name,$value); 
     717          } 
     718     } 
    661719 
    662720    /** 
     
    667725     protected function setupFields() { 
    668726          $from = $this->method == 'POST' ? $_POST : $_GET; 
    669           foreach ($this->fields as $f) { 
    670                $f->setup($from); 
     727          if (!empty($from)) { 
     728               foreach ($this->fields as $f) { 
     729                    $f->setup($from); 
     730               } 
    671731          } 
    672732     } 
     
    680740     */ 
    681741     protected function handleActions($submitted) { 
     742          $hasActions = false; 
    682743          foreach ($submitted as $f) { 
    683744               $action = $f->getAction(); 
    684745               if ($action != NULL) { 
     746                    if (!$hasActions) { 
     747                         $this->core->callBehavior('coreBeforeFormSubmit',$this); 
     748                    } 
     749                    $hasActions = true; 
    685750                    $ret = call_user_func($action,$this); 
    686751               } 
     752          } 
     753          if ($hasActions) { 
     754               $this->core->callBehavior('coreAfterFormSubmit',$this); 
    687755          } 
    688756     } 
     
    706774 
    707775    /** 
     776     * isSubmitted - returns whether form has been submitted or not 
     777     * 
     778     * @access public 
     779     * 
     780     * @return boolean true if the form has been submitted. 
     781     */    
     782     public function isSubmitted() { 
     783          foreach ($this->submitfields as $f) { 
     784               if ($f->isDefined()) { 
     785                    return true; 
     786               } 
     787          } 
     788          return false;        
     789     } 
     790      
     791    /** 
    708792     * setup - sets up the form, given the parameters given to the page 
    709793     *              should be called after fields have been defined. 
     
    726810     * @access public 
    727811     */ 
    728      public function check() 
    729      { 
     812     public function check(dcAdminContext $ctx) 
     813     { 
     814          $valid = true; 
    730815          foreach ($this->fields as $f) { 
    731816               try { 
     
    733818               } 
    734819               catch (InvalidFieldException $e) { 
    735                     $this->errors[] = $e->getMessage(); 
     820                    $valid = false; 
     821                    $ctx->addError($e->getMessage()); 
    736822               } 
     823          } 
     824          if (!$valid) { 
     825               throw new InvalidFieldException ("Some fields are missing"); 
    737826          } 
    738827     } 
     
    796885 
    797886    /** 
     887     * defines whether a field is multiple or not 
     888     * 
     889     * @param boolean true if the field is multiple 
     890      * 
     891     * @access public 
     892     */ 
     893     public function setMultiple($m=true) { 
     894          $this->multiple = $m; 
     895     } 
     896      
     897    /** 
     898     * Returns whether can have multiple values or not 
     899     * 
     900     * @return boolean true if the field has multiple values 
     901      * 
     902     * @access public 
     903     */ 
     904     public function isMultiple($m=true) { 
     905          return $this->multiple; 
     906     } 
     907 
     908    /** 
    798909     * setNID - defines fiels name & id 
    799910     * 
     
    9051016     abstract public function getWidgetBlock(); 
    9061017      
     1018     public function isEmpty() { 
     1019          return (count($this->values) == 0) || empty($this->values[0]); 
     1020     } 
    9071021 
    9081022    /** 
     
    9771091     public function check() 
    9781092     { 
    979           if (!$this->defined && $this->options['mandatory']) { 
    980                throw new InvalidFieldException(sprintf( 
    981                     'Field "%s" is mandatory', 
    982                     $this->attributes['label']) 
    983                ); 
     1093          if (isset($this->options ['required']) && $this->options['required']) { 
     1094               if (!$this->defined || $this->isEmpty()) { 
     1095                    throw new InvalidFieldException(sprintf( 
     1096                         'Field "%s" is mandatory', 
     1097                         $this->options['label']) 
     1098                    ); 
     1099               } 
    9841100          } 
    9851101     } 
     
    10881204class dcFieldCheckbox extends dcField 
    10891205{ 
     1206     protected $checked; 
     1207      
     1208     public function __construct($name,$values,$options=array()) 
     1209     { 
     1210          $val = array(); 
     1211          if (!is_array($values)) { 
     1212               $values = array("1" => !empty($values)); 
     1213          } 
     1214          $this->checked = $values; 
     1215          parent::__construct($name,array_keys($values),$options); 
     1216     } 
     1217 
     1218    /** 
     1219     * setValue - sets field value 
     1220     * 
     1221     * @param mixed $value  field value. 
     1222     * @param int   $offset value offset to define (default 0). 
     1223     * 
     1224     * @access public 
     1225     */ 
     1226     public function setValue($value,$offset=0) { 
     1227          $this->checked[$this->values[0]] = $value; 
     1228     } 
     1229      
     1230     public function getValue($offset=0) { 
     1231          $val = parent::getValue($offset); 
     1232          if (isset($this->checked[$val])) { 
     1233               return $this->checked[$val]?$val:false; 
     1234          } else { 
     1235               return false; 
     1236          } 
     1237     } 
     1238      
     1239     public function getAttributes($options) 
     1240     { 
     1241          $a = parent::getAttributes($options); 
     1242           
     1243          $val = $a['value']; 
     1244          if (isset($this->checked[$val]) && $this->checked[$val]) { 
     1245               $a['checked']='checked'; 
     1246          } 
     1247          return $a; 
     1248     } 
     1249 
     1250     public function setup($from) 
     1251     { 
     1252          $values = $this->parseValues($from); 
     1253          foreach ($this->checked as $k=>&$v) { 
     1254               $v=false; 
     1255          } 
     1256          foreach ($values as $v) { 
     1257               $this->checked[$v] = true; 
     1258          } 
     1259          $this->setValues(array_keys($this->checked)); 
     1260     } 
     1261 
    10901262     public function getWidgetBlock() 
    10911263     { 
     
    10941266 
    10951267     public function getDefaultValue() { 
    1096           return 0; 
     1268          return false; 
    10971269     } 
    10981270} 
     
    11391311{ 
    11401312     protected $combo; 
     1313     protected $combo_values; 
    11411314 
    11421315     public function __construct($name,$value,$combo,$options=array()) 
    11431316     { 
    11441317          $this->combo = $combo; 
     1318          $this->combo_values = $combo; 
     1319          foreach ($combo as $k=>$v) { 
     1320               if (is_array($v)) { 
     1321                    unset($this->combo_values[$k]); 
     1322                    $this->combo_values = array_merge($v,$this->combo_values); 
     1323               } 
     1324          } 
    11451325          parent::__construct($name,$value,$options); 
    11461326     } 
     
    11611341          } 
    11621342          foreach ($values as &$v) { 
    1163                if (!isset($this->combo[$v])) 
    1164                $v = $this->getDefaultValue(); 
     1343               if (!isset($this->combo_values[$v])) { 
     1344                    $v = $this->getDefaultValue(); 
     1345               } 
    11651346          } 
    11661347          return $values; 
Note: See TracChangeset for help on using the changeset viewer.

Sites map