Ejemplo n.º 1
0
 function handle($params)
 {
     if (!defined('DISABLE_reCAPTCHA')) {
         define('DISABLE_reCAPTCHA', 1);
     }
     import('Dataface/QuickForm.php');
     Dataface_QuickForm::$TRACK_SUBMIT = false;
     $app = Dataface_Application::getInstance();
     $query = $app->getQuery();
     $errors = null;
     try {
         if (!@$_POST['-table']) {
             throw new Exception("No table specified");
         }
         $table = $_POST['-table'];
         $rec = new Dataface_Record($table, array());
         $tableObj = $rec->_table;
         $fields = array();
         if (!$rec->checkPermission('new')) {
             throw new Exception("Failed to insert record.  Permission denied");
         }
         foreach ($_POST as $k => $v) {
             if ($k[0] == '-') {
                 continue;
             }
             $fields[] = $k;
             $rec->setValue($k, $v);
             if (!$rec->checkPermission('new', array('field' => $k))) {
                 throw new Exception(sprintf("Failed to insert record because you do not have permission to insert data into the %s column", $k));
             }
         }
         $form = df_create_new_record_form($table, $fields);
         $form->_flagSubmitted = true;
         $res = $form->validate();
         if (!$res) {
             $errors = $form->_errors;
             throw new Exception('Validation error', REST_INSERT_VALIDATION_ERROR);
         }
         $res = $rec->save(null, true);
         if (PEAR::isError($res)) {
             throw new Exception("Failed to insert record due to a server error: " . $res->getMessage(), 500);
         }
         $out = array();
         $vals = $rec->strvals();
         foreach ($vals as $k => $v) {
             if ($rec->checkPermission('view')) {
                 $out[$k] = $v;
             }
         }
         $this->out(array('code' => 200, 'message' => 'Record successfully inserted', 'record' => $out));
         exit;
     } catch (Exception $ex) {
         $this->out(array('code' => $ex->getCode(), 'message' => $ex->getMessage(), 'errors' => $errors));
         exit;
     }
 }