Exemplo n.º 1
0
 /**
  * function to add a custom field
  * It will recieve the custom field information for custom field add form
  * Add the data in the fields table and then to the custom field table
  * @see CustomFields::get_custom_fields_tablename()
  * @see popups/add_custom_field_modal.php
  */
 public function eventAddCustomField(EventControler $evctl)
 {
     $idmodule = $evctl->idmodule;
     $custom_field_type = $evctl->custom_field_type;
     $req = $evctl->cf_req;
     $field_validation = array();
     $is_required = false;
     if ($req == 'on') {
         $is_required = true;
         $field_validation["required"] = true;
     }
     $field_data_type = '';
     switch ($custom_field_type) {
         case 1:
             $fld_length = (int) $evctl->cf_len;
             $field_data_type = 'VARCHAR(' . $fld_length . ')';
             if ($is_required === true) {
                 if ($evctl->cf_max_len != '' || (int) $evctl->cf_max_len > 0) {
                     $field_validation["maxlength"] = (int) $evctl->cf_max_len;
                 }
                 if ($evctl->cf_min_len != '' || (int) $evctl->cf_min_len > 0) {
                     $field_validation["minlength"] = (int) $evctl->cf_min_len;
                 }
             }
             break;
         case 2:
             $field_data_type = 'TEXT';
             break;
         case 3:
             $field_data_type = 'VARCHAR(3)';
             break;
         case 5:
             $pick_values = $evctl->cf_pick;
             $not_equal = $evctl->cf_pick_notequal;
             if ($is_required === true) {
                 $field_validation["notEqual"] = $not_equal;
             }
             $field_data_type = 'VARCHAR(100)';
             break;
         case 6:
             $pick_values = $evctl->cf_pick;
             $field_data_type = 'VARCHAR(100)';
             break;
         case 7:
             $fld_length = (int) $evctl->cf_len;
             $field_data_type = 'VARCHAR(' . $fld_length . ')';
             break;
         case 8:
             $fld_length = (int) $evctl->cf_len;
             $field_data_type = 'VARCHAR(' . $fld_length . ')';
             break;
         case 9:
             $field_data_type = 'DATE';
             break;
         case 10:
             $field_data_type = 'VARCHAR(10)';
             break;
         case 210:
             $field_data_type = 'VARCHAR(15)';
             break;
     }
     if (count($field_validation) > 0) {
         $field_validation_entry = json_encode($field_validation);
     } else {
         $field_validation_entry = '';
     }
     $qry = "select * from " . $this->getTable() . " where field_name like '%ctf_%' order by idfields desc limit 1 ";
     $stmt = $this->getDbConnection()->executeQuery($qry);
     if ($stmt->rowCount() > 0) {
         $data = $stmt->fetch();
         $last_custom_field = $data["field_name"];
         $field_sequence = $data["field_sequence"];
         $last_custom_field_explode = explode("_", $last_custom_field);
         $custom_field_suffix = $last_custom_field_explode[1];
         $new_custom_field_suffix = $custom_field_suffix + 1;
         $custom_field_name = "ctf_" . $new_custom_field_suffix;
         $custom_field_sequence = $field_sequence + 1;
     } else {
         $custom_field_name = "ctf_1";
         $custom_field_sequence = 1;
     }
     $insert_data = array('field_name' => $custom_field_name, 'field_label' => CommonUtils::purify_input($evctl->cf_label), 'field_sequence' => $custom_field_sequence, 'idblock' => $this->get_custom_field_blocks($idmodule), 'idmodule' => $idmodule, 'table_name' => $this->get_custom_fields_tablename($idmodule), 'field_type' => $custom_field_type, 'field_validation' => $field_validation_entry);
     $this->insert($this->getTable(), $insert_data);
     $idfields = $this->getInsertId();
     if ($idfields > 0) {
         if ($custom_field_type == 5 || $custom_field_type == 6) {
             //$pick_values_seperated = explode(PHP_EOL,$evctl->cf_pick);
             $pick_values_seperated = preg_split('/[\\r\\n]+/', $evctl->cf_pick, -1, PREG_SPLIT_NO_EMPTY);
             $do_combo_values = new ComboValues();
             $do_combo_values->add_combo_values($idfields, $pick_values_seperated);
         }
         // add field to the custom field table for the moduleedit_custom_field_modal
         $qry_alter = "\n\t\t\talter table `" . $this->get_custom_fields_tablename($idmodule) . "` \n\t\t\tadd column `{$custom_field_name}` {$field_data_type}\n\t\t\t";
         $this->query($qry_alter);
         $_SESSION["do_crm_messages"]->set_message('success', _('Custom field added successfully.'));
         $next_page = NavigationControl::getNavigationLink("Settings", "customfield");
         $dis = new Display($next_page);
         $dis->addParam("cmid", $idmodule);
         $evctl->setDisplayNext($dis);
     } else {
         $_SESSION["do_crm_messages"]->set_message('error', _('Custom field could not be added, please try again ! '));
     }
 }