Esempio n. 1
0
 public function save()
 {
     $this->errors = array();
     if (!$this->validate()) {
         return false;
     }
     $ar = array();
     foreach (static::getFields() as $f) {
         if (!($f->name == 'id' && empty($this->row[$f->dbname])) && !$f->skip()) {
             $ar[$f->dbname] = $f->toString(Functions::nz($this->row[$f->dbname], $f->default));
         }
     }
     if (isset($this->row['id']) && $this->row['id'] != 0 && DB::fetchOne('SELECT id FROM ' . static::getName() . ' WHERE id=' . $this->row['id'])) {
         DB::ezQuery('UPDATE', static::getName(), $ar, 'id=' . $this->row['id']);
     } else {
         DB::ezQuery('INSERT', static::getName(), $ar);
         $this->row['id'] = DB::getLastId();
     }
     return true;
 }
Esempio n. 2
0
 private static function checkTableForm($formid, $table, $ar, $fields, $subform, $allfields, $onupdate, $relocate = true)
 {
     $test = function ($val) use(&$test) {
         if (is_array($val)) {
             $val = array_map($test, $val);
         } else {
             $val = mysql_real_escape_string($val);
         }
         return $val;
     };
     $ar = $test($ar);
     $line = 0;
     while (isset($ar[$formid . '_id_' . $line])) {
         if ($ar[$formid . '_' . $line . '_dirty'] == 1) {
             $qry = array();
             foreach ($fields as $field) {
                 $type = Functions::nz($field['type'], $allfields[$field['name']]['type']);
                 if ($type != 'hidden') {
                     if ($type == 'checkbox') {
                         $val = isset($ar[$formid . '_' . $field['name'] . '_' . $line]) ? 'true' : 'false';
                     } elseif ($type == 'date') {
                         if ($ar[$formid . '_' . $field['name'] . '_' . $line] == '') {
                             $val = '0000-00-00';
                         } else {
                             $tmp = new Date();
                             $tmp->fromstring($ar[$formid . '_' . $field['name'] . '_' . $line]);
                             $val = $tmp->toUS();
                         }
                     } elseif ($type == 'real') {
                         $val = str_replace(',', '.', str_replace('.', '', $ar[$formid . '_' . $field['name'] . '_' . $line]));
                     } else {
                         $val = $ar[$formid . '_' . $field['name'] . '_' . $line];
                     }
                     if ($validate = Functions::nz($field['validate'], false)) {
                         if ($validate($val)) {
                             $qry[$field['name']] = $val;
                         }
                     } else {
                         $qry[$field['name']] = $val;
                     }
                 }
             }
             DB::ezQuery('UPDATE', $table, $qry, 'id=' . $ar[$formid . '_id_' . $line]);
             if ($onupdate) {
                 $onupdate($ar[$formid . '_id_' . $line]);
             }
         }
         ++$line;
     }
     if ($ar[$formid . '_id_new'] == 1) {
         $qry = array();
         foreach ($fields as $field) {
             $type = Functions::nz($field['type'], $allfields[$field['name']]['type']);
             if ($type == 'checkbox') {
                 $val = isset($ar[$formid . '_' . $field['name'] . '_new']) ? 'true' : 'false';
             } elseif ($type == 'date') {
                 if ($ar[$formid . '_' . $field['name'] . '_new'] == '') {
                     $val = '0000-00-00';
                 } else {
                     $tmp = new Date();
                     $tmp->fromstring($ar[$formid . '_' . $field['name'] . '_new']);
                     $val = $tmp->toUS();
                 }
             } elseif ($type == 'real') {
                 $val = str_replace(',', '.', str_replace('.', '', $ar[$formid . '_' . $field['name'] . '_new']));
             } else {
                 $val = $ar[$formid . '_' . $field['name'] . '_new'];
             }
             if ($validate = Functions::nz($field['validate'], false)) {
                 if ($validate($val)) {
                     $qry[$field['name']] = $val;
                 }
             } else {
                 $qry[$field['name']] = $val;
             }
         }
         DB::ezQuery('INSERT', $table, $qry);
         if ($onupdate) {
             $id = DB::getLastId();
             $onupdate($id);
         }
     }
     if ($subform) {
         foreach ($ar[$formid . '_subforms'] as $subformid) {
             $rs = DB::fetch('SELECT * FROM ' . $subform['table']);
             $allFields = array();
             $qryFields = $rs->fetchFields();
             foreach ($qryFields as $f) {
                 $allFields[$f->name] = array('type' => $f->type);
             }
             self::checkTableForm($subformid, $subform['table'], $ar, $subform['fields'], false, $allFields, Functions::nz($subform['onupdate'], false), false);
         }
     }
     if ($relocate) {
         header('Location: ' . Functions::getArgs(''));
     }
 }