Beispiel #1
0
 function __construct($modelname, $formoptions = array())
 {
     try {
         if (isset($formoptions['modelIdSelected'])) {
             $this->modelIdSelected = $formoptions['modelIdSelected'];
         }
         //echo "__construct App_Form_Default";
         $this->_ignoreAuthorize = @($formoptions['ignore_authorize'] == true) ? true : false;
         parent::__construct();
         //echo "__construct App_Form_Default";
         $this->setModel($modelname);
         $config = $this->_getConfig($modelname);
         $req = Zend_Controller_Front::getInstance()->getRequest();
         $action = $req->getActionName();
         // echo $action;
         foreach ($config->prop as $prop) {
             if ($prop->disabledOnForm == 'true' or $prop->form == 'false') {
                 continue;
             } elseif ($action == 'new') {
                 if ($prop->disabledOnInsert == 'true') {
                     continue;
                 } elseif ($prop->autoIncrement == "true") {
                     continue;
                 }
             } elseif ($action != 'new') {
                 if ($prop->disabledOnEdit == 'true') {
                     continue;
                 }
             }
             $elementType = '';
             $options = array();
             $options['id'] = $prop->name;
             $elementType = $options['type'] = isset($prop->input) ? $prop->input : "text";
             $fieldtype = isset($prop->type) ? $prop->type : "text";
             $input = isset($prop->input) ? $prop->input : "text";
             $options['filters'] = isset($prop->filters) ? explode(",", $prop->filters) : array('StringTrim');
             $options['required'] = (isset($prop->required) and $prop->required == 'true') ? true : false;
             $options['alt'] = isset($prop->suffix) ? $prop->suffix : $prop->sufix;
             $options['label'] = isset($prop->label) ? $prop->label : ucfirst($prop->name);
             $options['label'] = $this->getView()->translate($options['label']);
             $options['class'] = isset($prop->class) ? $prop->class : "";
             $options['style'] = isset($prop->style) ? $prop->style : "width:80%";
             $options['value'] = isset($prop->default) ? $prop->default : "";
             $options['decorators'] = $this->{"elementDecorators"};
             if (isset($prop->minlength)) {
                 $options['minlength'] = (int) $prop->minlength;
             }
             if (isset($prop->maxlength)) {
                 $options['maxlength'] = (int) $prop->maxlength;
             }
             if (isset($prop->min)) {
                 $options['min'] = (int) $prop->min;
             }
             if (isset($prop->max)) {
                 $options['max'] = (int) $prop->max;
             }
             if ('readonly' == $prop->modification and $this->isEditForm()) {
                 $input = "text";
                 $options['type'] = "text";
             }
             if (0) {
                 switch (strtolower($fieldtype)) {
                     case 'int':
                         $options['validators'] = array('Digits');
                         $options['class'] = 'valid-number';
                         $options['style'] = isset($prop->style) ? $prop->style : "width:100px";
                         break;
                     case 'float':
                     case 'decimal':
                         $options['type'] = 'text';
                         $options['input'] = 'text';
                         $options['class'] .= ' currency ';
                         $options['validators'] = array('Float');
                         $options['style'] = isset($prop->style) ? $prop->style : "width:100px";
                         break;
                     case 'date':
                     case 'datepicker':
                     case 'datetime':
                         $options['input'] = 'text';
                         if (!in_array($options['class'], array('datepicker-from', 'datepicker-to'))) {
                             $options['class'] = 'datepicker';
                         }
                         $options['type'] = 'text';
                         $options['validators'] = array(array('Date', array('format' => 'dd/mm/yyyy')));
                         break;
                     case 'bool':
                     case 'boolean':
                         $options['style'] = isset($prop->style) ? $prop->style : "width:100px";
                         $options['type'] = 'select';
                         $options['input'] = 'select';
                         break;
                 }
             }
             switch ($input) {
                 case 'date':
                 case 'datepicker':
                 case 'datetime':
                     $options['input'] = 'text';
                     if (!in_array($options['class'], array('datepicker-from', 'datepicker-to'))) {
                         $options['class'] = 'datepicker';
                     }
                     $options['type'] = 'text';
                     $options['validators'] = array(array('Date', array('format' => 'dd/mm/yyyy')));
                     break;
                 case 'editor':
                     $options['type'] = 'textarea';
                     $options['class'] = 'editor-full';
                     $options['style'] = "width:600px;height:100px;";
                     break;
                 case 'money':
                     $options['type'] = 'text';
                     $options['input'] = 'text';
                     $options['class'] = 'valid-number';
                     $options['validators'] = array('Digits');
                     break;
                     break;
                 case 'spin':
                     $options['type'] = 'text';
                     $options['input'] = 'text';
                     $options['class'] = 'valid-number';
                     $options['validators'] = array('Digits');
                     break;
                 case 'currency':
                     $options['type'] = 'text';
                     $options['input'] = 'text';
                     $options['class'] .= ' currency ';
                     //$options['validators']= array('Digits' );
                     break;
                 case 'filepicker':
                     $options['type'] = 'filepicker';
                     $options['input'] = 'filepicker';
                     $options['class'] .= ' filepicker ';
                     $options['PrefixPath'] .= 'App_Form_Element ';
                     break;
                 case 'acccodepicker':
                     $options['type'] = 'text';
                     $options['input'] = 'text';
                     $options['class'] .= ' acccodepicker ';
                     //$options['validators']= array('Digits' );
                     break;
                 case 'email':
                     $options['input'] = 'email';
                     $options['type'] = 'email';
                     $options['class'] .= ' validemail ';
                     break;
                 case 'textarea':
                     //$options['type']='text';
                     if ($options['style'] == '') {
                         $options['style'] = "width:70%;height:40px;";
                     }
                     break;
                 case 'autocomplete':
                     $options['class'] = 'autocomplete';
                     $options['readonly'] = 'readonly';
                     $elementName = $elementId = $options['id'];
                     @($options['callBackAffterSelect'] = $prop->autocompleteOptions->callBackSelectedFunction);
                     @($options['resource'] = $prop->autocompleteOptions->resource);
                     break;
                 case 'reference':
                     $options['type'] = 'select';
                     list($RefmodelName, $fk) = explode(".", $prop->reference);
                     $model = new $RefmodelName();
                     $condition = $prop->multiOptions->condition;
                     $paramsString = $prop->multiOptions->conditionParams;
                     $where = '';
                     if ('' != trim($condition)) {
                         $select = str_replace(array_keys($model->PROPS_TO_COLUMS_LIST), array_keys($model->COLUMS_TO_PROPS_LIST), $condition);
                         $selectParams = array();
                         $paramsAr = explode(",", $paramsString);
                         $formModel = clone $this->_model;
                         if ($formModel instanceof Eau_Model_Client_Assessment) {
                             $clientId = App_Env::getRequest()->getParam("clientId");
                             $formModel->setClientId($clientId);
                         }
                         if (!empty($paramsAr)) {
                             foreach ($paramsAr as $attr) {
                                 $attr = str_replace("()", "", $attr);
                                 $selectParams[] = $formModel->{$attr}();
                             }
                             $where = App_Util::bindSqlParam($select, $selectParams);
                         }
                     } elseif ($model instanceof Sam_Model_User_Group) {
                         if (false == App_Env::isAdmin()) {
                             $where = " id != '3' ";
                         }
                     }
                     if ($where != '') {
                         $result = $model->fetchAll($where);
                     } else {
                         $result = $model->fetchAll();
                     }
                     foreach ($result as $item) {
                         $pk = $item->{$fk};
                         $options['multiOptions'][$pk] = "" . $item;
                     }
                     break;
             }
             if ($options['type'] == 'radio') {
                 $options['decorators'] = $this->redioDecorators;
             }
             if ($input != 'reference' and ($options['type'] == 'select' or $options['type'] == 'radio')) {
                 $options['Separator'] = "";
                 if (!is_string($prop->multiOptions->model)) {
                     // var_dump($prop->multiOptions);
                     $multiOptions = $prop->multiOptions->toArray();
                     if (isset($multiOptions['option'])) {
                         foreach ($multiOptions['option'] as $option) {
                             $value = $option['id'];
                             $label = $option['label'];
                             $options['multiOptions'][$value] = $label;
                         }
                     }
                 } else {
                     // สร้าง model จาก  ตาราง
                     $modelName = $prop->multiOptions->model;
                     $pkCol = $prop->multiOptions->pk;
                     $labelCol = $prop->multiOptions->label;
                     $model = new $modelName();
                     // $cacheId = $modelName;
                     //$cache = Zend_Registry::get('cache');
                     //if( ($result = $cache->load($cacheId)) === false ) {
                     //echo "noCach";
                     $result = $model->fetchAll();
                     //	$cache->save($result, $cacheId);
                     //}
                     foreach ($result as $item) {
                         $pk = $item->{$pkCol};
                         $label = $item->{$labelCol};
                         $options['multiOptions'][$pk] = $label;
                     }
                 }
             }
             if ($options['class'] == 'acccodepicker') {
                 $elementName = $elementId = $options['id'];
                 $selectedId = $this->getElement($elementId)->getValue();
                 @($options['callBackAffterSelect'] = $prop->datapickerOptions->callBackSelectedFunction);
                 @($options['value'] = $prop->datapickerOptions->value);
                 @($options['display'] = $prop->datapickerOptions->display);
                 $resource = $prop->datapickerOptions->resource;
                 $this->getView()->formAutocomplete($elementId, $elementName, $selectedId, $resource, $options);
             } elseif ($prop->proptype == 'view' and $options['class'] != 'datapicker') {
                 $options['readonly'] = 'readonly';
                 //$options['disabled'] = 'disabled';
                 $options['required'] = false;
                 $options['class'] .= " element-readonly ";
             } elseif ($prop->readonly == 'true') {
                 $options['readonly'] = 'readonly';
                 //$options['disabled'] = 'disabled';
                 $options['required'] = false;
                 if ($options['class'] == 'datepicker') {
                     $options['class'] = '';
                 }
                 $options['class'] .= " element-readonly ";
             }
             if ($prop->disabled == 'true') {
                 $options['readonly'] = 'readonly';
                 //$options['disabled'] = 'disabled';
                 $options['required'] = false;
                 if ($options['class'] == 'datepicker') {
                     $options['class'] = '';
                 }
                 $options['class'] .= " element-readonly ";
             }
             if ($action == 'edit') {
                 if ($prop->pk == "true") {
                     $options['readonly'] = 'readonly';
                     $options['class'] .= " element-readonly ";
                 }
             }
             if ($options['required'] == true and $elementType != 'filepicker') {
                 $options['class'] .= " required ";
             }
             //if($options['type']=='')$options['type']='text';
             try {
                 $element = $this->_setElement($options);
             } catch (Exception $e) {
                 echo $e->getMessage();
             }
             if ($options['class'] == 'datapicker') {
                 $elementName = $elementId = $options['id'];
                 $selectedId = $this->getElement($elementId)->getValue();
                 @($options['callBackAffterSelect'] = $prop->datapickerOptions->callBackSelectedFunction);
                 @($options['value'] = $prop->datapickerOptions->value);
                 @($options['display'] = $prop->datapickerOptions->display);
                 $resource = $prop->datapickerOptions->resource;
                 $this->getView()->formAutocomplete($elementId, $elementName, $selectedId, $resource, $options);
             } elseif ($elementType == 'spin') {
                 $elementName = $elementId = $options['id'];
                 $selectedId = $this->getElement($elementId)->getValue();
                 $this->getView()->formSpin($elementId, $elementName, $selectedId);
             } elseif ($options['class'] == 'joint-renters-picker') {
                 $req = Zend_Controller_Front::getInstance()->getRequest();
                 $elementName = $elementId = $options['id'];
                 $selectedId = $req->getParam('id', '');
                 if ($selectedId == '') {
                     $selectedId = $this->modelIdSelected;
                 }
                 //echo $this->getModel()->id;
                 $this->getView()->formRenterPicker($elementId, $elementName, $selectedId);
             } elseif (trim($options['class']) == 'filepicker') {
                 //echo 'filepicker';
                 //$this->getView()->formFilePicker($options['id'],$options['id']);
             } elseif (trim($options['class']) == 'acccodepicker') {
                 $this->getView()->formAcccodepickerPicker($options['id'], $options['id']);
             }
         }
         $this->_addSaveButton();
         $this->_addDecorators();
     } catch (Exception $e) {
         //echo $e->getMessage();
         // echo $e->getLine();
         //  echo str_replace("\n", "<br/>", $e->getTraceAsString());
     }
 }