Ejemplo n.º 1
0
 /**
  * Rename all keys in all data rows to match DB column names, and normalize
  * all values to be valid for the `$table`.
  *
  * If a _value_ in the array matches a lowercased DB column header, the _key_
  * of that value is the DB column name to which that header has been matched.
  *
  * @param Webdb_DBMS_Table $table
  * @param array $columns
  * @return array Array of error messages.
  */
 public function match_fields($table, $column_map)
 {
     // First get the indexes of the headers
     $heads = $this->remap($column_map);
     $errors = array();
     for ($row_num = 1; $row_num <= $this->row_count(); $row_num++) {
         foreach ($this->data[$row_num] as $col_num => $value) {
             if (!isset($heads[$col_num])) {
                 continue;
             }
             $col_errors = array();
             $db_column_name = $heads[$col_num];
             $column = $table->get_column($db_column_name);
             // Required, has no default, and is empty
             if ($column->is_required() and !$column->has_default() and empty($value)) {
                 $col_errors[] = 'Required but empty';
             }
             // Already exists
             if ($column->is_unique_key()) {
                 // @TODO
             }
             // Too long (if the column has a size and the value is greater than this)
             if (!$column->is_foreign_key() and !$column->is_boolean() and $column->get_size() > 0 and strlen($value) > $column->get_size()) {
                 $col_errors[] = 'Value (' . $value . ') too long (maximum length of ' . $column->get_size() . ')';
             }
             // Invalid foreign key value
             if (!empty($value) and $column->is_foreign_key()) {
                 $err = $this->validate_foreign_key($column, $col_num, $row_num, $value);
                 if ($err) {
                     $col_errors[] = $err;
                 }
             }
             // Dates
             if ($column->get_type() == 'date' and !empty($value) and preg_match('/\\d{4}-\\d{2}-\\d{2}/', $value) !== 1) {
                 $col_errors[] = 'Value (' . $value . ') not in date format';
             }
             if (count($col_errors) > 0) {
                 // Construct error details array
                 $errors[] = array('column_name' => $this->headers[$col_num], 'column_number' => $col_num, 'field_name' => $column->get_name(), 'row_number' => $row_num, 'messages' => $col_errors);
             }
         }
     }
     return $errors;
 }
Ejemplo n.º 2
0
 /**
  * Rename all keys in all data rows to match DB column names, and normalize
  * all values to be valid for the `$table`.
  *
  * If a _value_ in the array matches a lowercased DB column header, the _key_
  * of that value is the DB column name to which that header has been matched.
  *
  * @param Webdb_DBMS_Table $table
  * @param array $array
  * @return void
  */
 public function match_fields($table, $array)
 {
     // First get the indexes of the headers
     foreach ($array as $key => $val) {
         foreach ($this->headers as $head_num => $head_name) {
             if (strtolower($head_name) == $val) {
                 $heads[$head_num] = $key;
             }
         }
     }
     // Now rename the keys in all the data rows, and get IDs for foreign keys.
     foreach ($this->data as &$row) {
         $new_row = array();
         foreach ($row as $cell_num => $value) {
             if (isset($heads[$cell_num])) {
                 $db_column_name = $heads[$cell_num];
                 if (!empty($value) && $table->get_column($db_column_name)->is_foreign_key()) {
                     $foreign_table = $table->get_column($db_column_name)->get_referenced_table();
                     $foreign_table->reset_filters();
                     $foreign_table->add_filter($foreign_table->get_title_column()->get_name(), '=', $value);
                     $value = $foreign_table->get_rows()->current();
                     $value = $value['id'];
                 }
                 $new_row[$db_column_name] = $value;
             }
         }
         $row = array_merge($table->get_default_row(), $new_row);
     }
 }