Example #1
0
 /**
  * Method addRowLoadFromSqlDataView
  * @access private
  * @param mixed $row 
  * @param mixed $list_attribute 
  * @param mixed $list_attribute_type 
  * @param mixed $key_attributes 
  * @param mixed $ind 
  * @param boolean $is_delete_action [default value: false]
  * @param double $line_nb [default value: 0]
  * @return boolean
  * @since 1.1.6
  */
 private function addRowLoadFromSqlDataView($row, $list_attribute, $list_attribute_type, $key_attributes, $ind, $is_delete_action = false, $line_nb = null)
 {
     if ($this->from_sql_data_view_delete) {
         // create delete button if not already exists
         $bnt_del_id = $this->id . "_btndel__ind_" . $ind;
         $delete_pic = $this->getPage()->getObjectId($bnt_del_id);
         if ($delete_pic == null) {
             $delete_pic = new Picture("img/wsp-admin/delete_16.png", 16, 16, 0, Picture::ALIGN_ABSMIDDLE);
             $delete_pic->setId($bnt_del_id);
             $delete_pic->onClickJs("if (!confirm('" . __(TABLE_CONFIME_DEL_ROW) . "')) { return false; }");
             $delete_pic->onClick($this->getPage(), "onChangeTableFromSqlDataView")->setAjaxEvent()->disableAjaxWaitMessage();
         }
     }
     // create row
     $row_table = new RowTable();
     $row_table->setId($this->id . "_row_" . $ind);
     for ($i = 0; $i < sizeof($list_attribute); $i++) {
         // get field properties
         if (is_array($this->from_sql_data_view_properties[$list_attribute[$i]])) {
             $attribute_properties = $this->from_sql_data_view_properties[$list_attribute[$i]];
         } else {
             $attribute_properties = array();
         }
         // get property display
         if (isset($attribute_properties["display"]) && $attribute_properties["display"] == false) {
             continue;
         }
         // get property update
         $is_update_ok = true;
         if (isset($attribute_properties["update"]) && $attribute_properties["update"] == false) {
             $is_update_ok = false;
         }
         if ($this->from_sql_data_view_update && !in_array($list_attribute[$i], $key_attributes) && $is_update_ok) {
             $row_value = $row->getValue($list_attribute[$i]);
             if (gettype($row_value) == "object" && method_exists($row_value, "render")) {
                 $row_value = $row_value->render();
             }
             $edit_pic = new Picture("wsp/img/edit_16x16.png", 16, 16);
             $row_obj = new Object($edit_pic, trim($row_value) == "" ? "&nbsp;&nbsp;" : utf8encode($row_value));
             $row_obj->setId($this->id . "_" . $list_attribute[$i] . "_obj_" . $ind)->setStyle("cursor:pointer;border:1px solid gray;");
             $input_obj = $this->createDbAttributeObject($row, $list_attribute, $list_attribute_type, $i, $ind, $key_attributes);
             if (get_class($input_obj) == "ComboBox") {
                 // Get foreign key value
                 $row_obj->emptyObject();
                 $value = $input_obj->getText();
                 $row_obj->add($edit_pic, trim($value) == "" ? "&nbsp;&nbsp;" : $value);
             } else {
                 if (get_class($input_obj) == "Calendar") {
                     $row_obj->emptyObject();
                     $value = $input_obj->getValueStr();
                     $row_obj->add($edit_pic, trim($value) == "" ? "&nbsp;&nbsp;" : $value);
                     $row_table->setNowrap();
                 }
             }
             $row_obj_input = new Object($input_obj);
             $row_obj_input->setId($this->id . "_" . $list_attribute[$i] . "_input_obj_" . $ind);
             $cancel_pic = new Picture("wsp/img/cancel_12x12.png", 12, 12);
             $cancel_pic->setId($this->id . "_img_" . $ind . "_cancel_" . $list_attribute[$i]);
             $cancel_pic->onClickJs("\$('#" . $row_obj->getId() . "').css('display', 'inline');\$('#" . $row_obj_input->getId() . "').hide();" . ($this->from_sql_data_view_add_button != null ? "\$('#" . $this->from_sql_data_view_add_button->getId() . "').button({ disabled: false });" : ""));
             $cancel_pic_obj = new Object($cancel_pic);
             $row_obj_input->add($cancel_pic_obj->forceSpanTag()->setStyle("position:absolute;"));
             if (!$this->getPage()->isAjaxPage() || $is_delete_action || $this->from_sql_data_view_reload_pic != null && $this->from_sql_data_view_reload_pic->isClicked() || $this->from_sql_data_view_add_button != null && $this->from_sql_data_view_add_button->isClicked()) {
                 $this->getPage()->addObject(new JavaScript("\$(document).ready(function() { \$('#" . $row_obj_input->getId() . "').hide(); });"));
             }
             $row_obj->onClickJs("\$('#" . $row_obj->getId() . "').hide();\$('#" . $row_obj_input->getId() . "').show();" . ($this->from_sql_data_view_add_button != null ? "\$('#" . $this->from_sql_data_view_add_button->getId() . "').button({ disabled: true });" : ""));
             $row_table->add(new Object($row_obj, $row_obj_input));
             // get properties align
             if (isset($attribute_properties["align"])) {
                 $row_table->setColumnAlign($i + 1, $attribute_properties["align"]);
             }
         } else {
             $value = $row->getValue($list_attribute[$i]);
             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_tmp->setValue($value);
                 $value = $input_obj_tmp->getText();
             }
             if (get_class($value) == "DateTime") {
                 $value = $value->format("Y-m-d");
             }
             $row_table->add(utf8encode($value));
         }
     }
     if ($this->from_sql_data_view_delete) {
         $row_table->add($delete_pic);
     } else {
         if ($this->from_sql_data_view_insert) {
             $row_table->add();
         }
     }
     if ($is_table_defined_style) {
         $row_table->setBorderPredefinedStyle($this->class);
     }
     if ($this->is_advance_table) {
         if (isset($line_nb) && $line_nb !== null) {
             $row_table->setRowClass($line_nb % 2 == 0 ? "odd" : "even");
         } else {
             if (is_numeric($ind)) {
                 $row_table->setRowClass($ind % 2 == 0 ? "odd" : "even");
             } else {
                 $row_table->setRowClass("even");
             }
         }
     }
     $this->addRow($row_table);
 }
Example #2
0
 /**
  * Method loadFromSqlDataView
  * @access public
  * @param mixed $sql 
  * @param mixed $properties [default value: array(]
  * @return mixed
  * @since 1.2.0
  */
 public function loadFromSqlDataView($sql, $properties = array(), $hide_empty_fields = false)
 {
     if (gettype($sql) != "object" || get_class($sql) != "SqlDataView") {
         throw new NewException(get_class($this) . "->loadFromSqlDataView() error: \$sql is not SqlDataView object", 0, getDebugBacktrace(1));
     }
     if ($insert || $update || $delete) {
         if ($sql->isQueryWithJoin()) {
             throw new NewException(get_class($this) . "->loadFromSqlDataView() error: you need use SqlDataView object without JOIN if you want to use insert update or delete.", 0, getDebugBacktrace(1));
         }
     }
     if (!is_array($properties)) {
         throw new NewException(get_class($this) . "->loadFromSqlDataView() error: \$properties need to be an array", 0, getDebugBacktrace(1));
     }
     $list_attribute = $sql->getListAttributeArray();
     // 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]])) {
                     $properties[$list_attribute[$i]] = array_merge($properties[$list_attribute[$i]], $property);
                 } else {
                     $properties[$list_attribute[$i]] = $property;
                 }
             }
         }
     }
     // check foreign keys
     $db_table_foreign_keys = $sql->getDbTableObject()->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);
                 // 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);
             }
         }
     }
     $this->from_sql_data_view_properties = $properties;
     $key_attributes = $sql->getPrimaryKeysAttributes();
     // check if all the fields of sql object are in the SQL attributes
     $list_attribute_change = false;
     $auto_hide_field_from = -1;
     $all_list_attributes = $sql->getDbTableObject()->getDbTableAttributes();
     for ($i = 0; $i < sizeof($all_list_attributes); $i++) {
         if (!in_array($all_list_attributes[$i], $list_attribute)) {
             $tmp_list_attribute = $sql->getCustomFields();
             $tmp_list_attribute .= ", `" . $sql->getDbTableObject()->getDbTableName() . "`.`" . $all_list_attributes[$i] . "`";
             $sql->setCustomFields($tmp_list_attribute);
             if ($auto_hide_field_from == -1) {
                 $auto_hide_field_from = sizeof($list_attribute);
             }
         }
     }
     if ($auto_hide_field_from != -1) {
         $list_attribute = $sql->getListAttributeArray();
         for ($i = $auto_hide_field_from; $i < sizeof($list_attribute); $i++) {
             $properties[$list_attribute[$i]]["display"] = false;
         }
     }
     $list_attribute_type = $sql->getListAttributeTypeArray();
     $auto_increment_id = $sql->getDbTableObject()->getDbTableAutoIncrement();
     $this->from_sql_data_view_properties = $properties;
     $it = $sql->retrieve();
     if ($it->getRowsNum() > 1) {
         throw new NewException(get_class($this) . "->loadFromSqlDataView() error: the query return more than 1 result. Form object can only modify 1 " . get_class($sql->getDbTableObject()) . ".", 0, getDebugBacktrace(1));
     }
     $this->table_from_sql_data_view = new Table();
     $this->table_from_sql_data_view->setDefaultAlign(RowTable::ALIGN_LEFT);
     $this->table_from_sql_data_view->setDefaultValign(RowTable::VALIGN_CENTER);
     // Generate form data
     $this->sql_data_view_object = $sql;
     $this->data_row_iterator_object = $it;
     if ($it->hasNext()) {
         $row = $it->next();
         $this->data_row_object = $row;
     } else {
         $temp_it = clone $it;
         $row = $temp_it->insert();
         for ($i = 0; $i < sizeof($list_attribute); $i++) {
             $row->setValue($list_attribute[$i], "");
         }
     }
     for ($i = 0; $i < sizeof($list_attribute); $i++) {
         // get field properties
         if (is_array($this->from_sql_data_view_properties[$list_attribute[$i]])) {
             $attribute_properties = $this->from_sql_data_view_properties[$list_attribute[$i]];
         } else {
             $attribute_properties = array();
         }
         // get property display
         if (isset($attribute_properties["display"]) && $attribute_properties["display"] == false) {
             continue;
         }
         // get property update
         $is_update_ok = true;
         if (isset($attribute_properties["update"]) && $attribute_properties["update"] == false) {
             $is_update_ok = false;
         }
         $row_table = new RowTable();
         if (isset($attribute_properties["title"])) {
             $row_table->add($attribute_properties["title"] . ": ");
         } else {
             $row_table->add($list_attribute[$i] . ": ");
         }
         $row_table->add("&nbsp;");
         if ($list_attribute[$i] != $auto_increment_id && $is_update_ok) {
             $row_value = $row->getValue($list_attribute[$i]);
             if (gettype($row_value) == "object" && method_exists($row_value, "render")) {
                 $row_value = $row_value->render();
             }
             $input_obj = $this->createDbAttributeObject($row, $list_attribute, $list_attribute_type, $i, $key_attributes);
             $row_table->add($input_obj);
             // get properties align
             if (isset($attribute_properties["align"])) {
                 $row_table->setColumnAlign($i + 1, $attribute_properties["align"]);
             }
         } else {
             $value = $row->getValue($list_attribute[$i]);
             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_tmp->setValue($value);
                 $value = $input_obj_tmp->getText();
             }
             if (get_class($value) == "DateTime") {
                 $value = $value->format("Y-m-d");
             }
             $row_table->add(utf8encode($value));
         }
         if ($hide_empty_fields && $value == '') {
             // Do not add the empty field
         } else {
             $this->table_from_sql_data_view->addRow($row_table);
         }
         if (isset($attribute_properties["row_id"]) && $attribute_properties["row_id"] != "") {
             $row_table->setId($attribute_properties["row_id"]);
         } else {
             $row_table->setId($i);
         }
     }
     return $it;
 }