Exemplo n.º 1
0
 /**
  * Method prepareFieldsArray
  * @access public
  * @param mixed $properties [default value: array(]
  * @return mixed
  * @since 1.2.1
  */
 public function prepareFieldsArray($properties = array())
 {
     $this->fields_array = array();
     $list_attribute = $this->database_object->getDbTableAttributes();
     $list_attribute_type = $this->database_object->getDbTableAttributesType();
     $auto_increment_id = $this->database_object->getDbTableAutoIncrement();
     // Add properties to apply on all fields
     if (isset($properties[ModelViewMapper::PROPERTIES_ALL]) && is_array($properties[ModelViewMapper::PROPERTIES_ALL])) {
         $apply_all_array = $properties[ModelViewMapper::PROPERTIES_ALL];
         foreach ($apply_all_array as $property_name => $property_value) {
             for ($i = 0; $i < sizeof($list_attribute); $i++) {
                 $property[$property_name] = $property_value;
                 if (isset($properties[$list_attribute[$i]])) {
                     // Handle child override of global property
                     if (!isset($properties[$list_attribute[$i]][$property_name])) {
                         $properties[$list_attribute[$i]] = array_merge($properties[$list_attribute[$i]], $property);
                     }
                 } else {
                     $properties[$list_attribute[$i]] = $property;
                 }
             }
         }
     }
     // check foreign keys
     $db_table_foreign_keys = $this->database_object->getDbTableForeignKeys();
     foreach ($db_table_foreign_keys as $fk_attribute => $value) {
         if (isset($properties[$fk_attribute])) {
             $fk_property = $properties[$fk_attribute];
             if (isset($fk_property["fk_attribute"])) {
                 // create combobox
                 $cmb = new ComboBox($this->form_or_page);
                 // get foreign key data
                 $query = "select distinct " . $value["column"] . " as id, " . $fk_property["fk_attribute"] . " as value from " . $value["table"];
                 if (isset($fk_property["fk_where"])) {
                     $query .= " where " . $fk_property["fk_where"];
                 }
                 if (isset($fk_property["fk_orderby"])) {
                     $query .= " order by " . $fk_property["fk_orderby"];
                 }
                 $stmt = DataBase::getInstance()->prepareStatement($query);
                 $row = DataBase::getInstance()->stmtBindAssoc($stmt, $row);
                 while ($stmt->fetch()) {
                     $cmb->addItem(utf8encode($row['id']), utf8encode($row['value']));
                 }
                 // add combo box in properties
                 $value['cmb_obj'] = $cmb;
                 $properties[$fk_attribute] = array_merge($properties[$fk_attribute], $value);
             }
         }
     }
     foreach ($list_attribute as $i => $attribute) {
         $wspobject = "TextBox";
         $attribute_properties = array();
         if (is_array($properties[$attribute])) {
             $attribute_properties = $properties[$attribute];
         }
         if (isset($attribute_properties["display"]) && $attribute_properties["display"] == false) {
             continue;
         }
         $is_update_ok = true;
         if (isset($attribute_properties["update"]) && $attribute_properties["update"] == false) {
             $is_update_ok = false;
         }
         $method = "get" . $this->getFormatValue($attribute);
         $value = call_user_func_array(array($this->database_model_object, $method), array());
         if ($attribute != $auto_increment_id && $is_update_ok) {
             // get property cmb_obj
             if (isset($attribute_properties['cmb_obj'])) {
                 $field = $attribute_properties['cmb_obj'];
             } else {
                 if (isset($attribute_properties["wspobject"]) && $attribute_properties["wspobject"] != "") {
                     $wspobject = $attribute_properties["wspobject"];
                 } else {
                     if ($list_attribute_type[$i] == "datetime") {
                         $wspobject = "Calendar";
                     } else {
                         if ($list_attribute_type[$i] == "boolean") {
                             $wspobject = "CheckBox";
                         }
                     }
                 }
                 if ($wspobject == "Calendar") {
                     $field = new Calendar($this->form_or_page);
                 } else {
                     if ($wspobject == "CheckBox") {
                         $field = new CheckBox($this->form_or_page);
                     } else {
                         if ($wspobject == "TextArea") {
                             $field = new TextArea($this->form_or_page);
                         } else {
                             if ($wspobject == "Editor") {
                                 $field = new Editor($this->form_or_page);
                                 if (isset($attribute_properties["editor_param"]) && $attribute_properties["editor_param"] != "") {
                                     $field->setToolbar($attribute_properties["editor_param"]);
                                 }
                             } else {
                                 if ($wspobject == "ComboBox") {
                                     $field = new ComboBox($this->form_or_page);
                                     if (isset($attribute_properties["combobox_values"])) {
                                         if (is_array($attribute_properties["combobox_values"])) {
                                             for ($j = 0; $j < sizeof($attribute_properties["combobox_values"]); $j++) {
                                                 $field->addItem($attribute_properties["combobox_values"][$j]['value'], $attribute_properties["combobox_values"][$j]['text']);
                                             }
                                         } else {
                                             throw new NewException(get_class($this) . "->prepareFieldsArray() error: the property combobox_values need to be an array.", 0, getDebugBacktrace(1));
                                         }
                                     }
                                 } else {
                                     $field = new TextBox($this->form_or_page);
                                     if ($list_attribute_type[$i] == "integer" || $list_attribute_type[$i] == "double") {
                                         $field->setWidth(70);
                                     }
                                     if (in_array($attribute, $key_attributes)) {
                                         $lv = new LiveValidation();
                                         $field->setLiveValidation($lv->addValidatePresence());
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             // Handle Checkbox case that only support value as "on" or "off"
             if (get_class($field) == "CheckBox") {
                 if ($value == "1") {
                     $field->setValue("on");
                 } else {
                     $field->setValue("off");
                 }
             } else {
                 if (get_class($field) == "Calendar") {
                     $field->setValue($value);
                 } else {
                     $field->setValue(utf8encode($value));
                 }
             }
             if (isset($attribute_properties["width"]) && method_exists($field, "setWidth")) {
                 $field->setWidth($attribute_properties["width"]);
             }
             if (isset($attribute_properties["height"]) && method_exists($field, "setHeight")) {
                 $field->setHeight($attribute_properties["height"]);
             }
             if (isset($attribute_properties["class"]) && method_exists($field, "setClass")) {
                 $field->setClass($attribute_properties["class"]);
             }
             if (isset($attribute_properties["style"]) && method_exists($field, "setStyle")) {
                 $field->setStyle($attribute_properties["style"]);
             }
             if (isset($attribute_properties["disable"])) {
                 if ($attribute_properties["disable"] == true && method_exists($field, "disable")) {
                     $field->disable();
                 } else {
                     if ($attribute_properties["disable"] == false && method_exists($field, "enable")) {
                         $field->enable();
                     }
                 }
             }
             if (get_class($field) != "Calendar") {
                 if (isset($attribute_properties["strip_tags"]) && $attribute_properties["strip_tags"] == true && method_exists($field, "setStripTags")) {
                     if (isset($attribute_properties["allowable_tags"])) {
                         $field->setStripTags($attribute_properties["allowable_tags"]);
                     } else {
                         $field->setStripTags("");
                         // no tag allowed
                     }
                 }
             }
         } else {
             if (isset($attribute_properties['cmb_obj'])) {
                 $field_tmp = $attribute_properties['cmb_obj'];
                 $field_tmp->setValue($value);
                 $value = $field_tmp->getText();
             }
             if (get_class($value) == "DateTime") {
                 $value = $value->format("Y-m-d");
             }
             $field = new Object(utf8encode($value));
         }
         $this->fields_array[$attribute] = $field;
     }
     return $this->fields_array;
 }
Exemplo n.º 2
0
 /**
  * Method createDbAttributeObject
  * @access private
  * @param mixed $row 
  * @param mixed $list_attribute 
  * @param mixed $list_attribute_type 
  * @param mixed $i 
  * @param mixed $ind 
  * @param mixed $key_attributes 
  * @return mixed
  * @since 1.1.6
  */
 private function createDbAttributeObject($row, $list_attribute, $list_attribute_type, $i, $ind, $key_attributes)
 {
     // get property cmb_obj (created by method loadFromSqlDataView)
     if (isset($this->from_sql_data_view_properties[$list_attribute[$i]]['cmb_obj'])) {
         $input_obj_tmp = $this->from_sql_data_view_properties[$list_attribute[$i]]['cmb_obj'];
         $input_obj = clone $input_obj_tmp;
         $input_obj->setName($this->id . "_input_" . $list_attribute[$i] . "_ind_" . $ind);
         $register_objects = WebSitePhpObject::getRegisterObjects();
         $register_objects[] = $input_obj;
         $_SESSION['websitephp_register_object'] = $register_objects;
     } else {
         $wspobject = "TextBox";
         $attribute_properties = array();
         if (is_array($this->from_sql_data_view_properties[$list_attribute[$i]])) {
             $attribute_properties = $this->from_sql_data_view_properties[$list_attribute[$i]];
         }
         if (isset($attribute_properties["wspobject"]) && $attribute_properties["wspobject"] != "") {
             $wspobject = $attribute_properties["wspobject"];
         } else {
             if ($list_attribute_type[$i] == "datetime") {
                 $wspobject = "Calendar";
             } else {
                 if ($list_attribute_type[$i] == "boolean") {
                     $wspobject = "CheckBox";
                 }
             }
         }
         if ($wspobject == "Calendar") {
             $input_obj = new Calendar($this->table_form_object, $this->id . "_input_" . $list_attribute[$i] . "_ind_" . $ind);
         } else {
             if ($wspobject == "CheckBox") {
                 $input_obj = new CheckBox($this->table_form_object, $this->id . "_input_" . $list_attribute[$i] . "_ind_" . $ind);
             } else {
                 if ($wspobject == "TextArea") {
                     $input_obj = new TextArea($this->table_form_object, $object_id);
                 } else {
                     if ($wspobject == "Editor") {
                         $input_obj = new Editor($this->table_form_object, $object_id);
                         if (isset($attribute_properties["editor_param"]) && $attribute_properties["editor_param"] != "") {
                             $input_obj->setToolbar($attribute_properties["editor_param"]);
                         }
                     } else {
                         if ($wspobject == "ComboBox") {
                             $input_obj = new ComboBox($this->table_form_object, $object_id);
                             if (isset($attribute_properties["combobox_values"])) {
                                 if (is_array($attribute_properties["combobox_values"])) {
                                     for ($j = 0; $j < sizeof($attribute_properties["combobox_values"]); $j++) {
                                         $input_obj->addItem($attribute_properties["combobox_values"][$j]['value'], $attribute_properties["combobox_values"][$j]['text']);
                                     }
                                 } else {
                                     throw new NewException(get_class($this) . "->loadFromSqlDataView() error: the property combobox_values need to be an array.", 0, getDebugBacktrace(1));
                                 }
                             }
                         } else {
                             $input_obj = new TextBox($this->table_form_object, $this->id . "_input_" . $list_attribute[$i] . "_ind_" . $ind);
                             if ($list_attribute_type[$i] == "integer" || $list_attribute_type[$i] == "double") {
                                 $input_obj->setWidth(70);
                             }
                             if (in_array($list_attribute[$i], $key_attributes)) {
                                 $lv = new LiveValidation();
                                 $input_obj->setLiveValidation($lv->addValidatePresence());
                             }
                         }
                     }
                 }
             }
         }
     }
     // get properties width and strip_tags
     if (is_array($this->from_sql_data_view_properties[$list_attribute[$i]])) {
         $attribute_properties = $this->from_sql_data_view_properties[$list_attribute[$i]];
         if (isset($attribute_properties["width"]) && method_exists($input_obj, "setWidth")) {
             $input_obj->setWidth($attribute_properties["width"]);
         }
         if (isset($attribute_properties["height"]) && method_exists($input_obj, "setHeight")) {
             $input_obj->setHeight($attribute_properties["height"]);
         }
         if (isset($attribute_properties["class"]) && method_exists($input_obj, "setClass")) {
             $input_obj->setClass($attribute_properties["class"]);
         }
         if (isset($attribute_properties["style"]) && method_exists($input_obj, "setStyle")) {
             $input_obj->setStyle($attribute_properties["style"]);
         }
         if (isset($attribute_properties["disable"])) {
             if ($attribute_properties["disable"] == true && method_exists($input_obj, "disable")) {
                 $input_obj->disable();
             } else {
                 if ($attribute_properties["disable"] == false && method_exists($input_obj, "enable")) {
                     $input_obj->enable();
                 }
             }
         }
         if (get_class($input_obj) != "Calendar") {
             if (isset($attribute_properties["strip_tags"]) && $attribute_properties["strip_tags"] == true && method_exists($input_obj, "setStripTags")) {
                 if (isset($attribute_properties["allowable_tags"])) {
                     $input_obj->setStripTags($attribute_properties["allowable_tags"]);
                 } else {
                     $input_obj->setStripTags("");
                     // no tag allowed
                 }
             }
         }
     }
     if ($row != null) {
         // get property db_attribute
         $field_value = $row->getValue($list_attribute[$i]);
         if (isset($this->from_sql_data_view_properties[$list_attribute[$i]]["db_attribute"])) {
             $db_attribute = $this->from_sql_data_view_properties[$list_attribute[$i]]["db_attribute"];
             $field_value = $row->getValue($db_attribute);
         }
         $input_obj->onChange("onChangeTableFromSqlDataView")->setAjaxEvent()->disableAjaxWaitMessage();
         if (get_class($input_obj) == "TextBox") {
             $input_obj->onKeyUpJs("if (\$(this)[0].defaultValue != \$(this).val()) { \$('#" . $this->id . "_img_" . $ind . "_cancel_" . $list_attribute[$i] . "').hide(); } else { \$('#" . $this->id . "_img_" . $ind . "_cancel_" . $list_attribute[$i] . "').show(); }");
         }
         if ($list_attribute_type[$i] == "boolean") {
             if (!$input_obj->isChanged()) {
                 $input_obj->setValue($field_value == true ? "on" : "off");
             }
         } else {
             if (gettype($field_value) == "object") {
                 $input_obj->setValue($field_value);
             } else {
                 $input_obj->setValue(utf8encode($field_value));
             }
         }
     }
     return $input_obj;
 }