Xataface 2.0
Xataface Application Framework
actions/rest_insert.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2011 Web Lite Solutions Corp (steve@weblite.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *
00020  * Synopsis
00021  * ==========
00022  *
00023  * An action to insert a new record.
00024  *
00025  * Credits
00026  * ========
00027  *
00028  * @author Steve Hannah <steve@weblite.ca>
00029  * @created May 1, 2011
00030  *
00031  * Rest API:
00032  * ---------
00033  *
00034  * POST >
00035  *              -table                  : Name of table to insert record into
00036  *              <colname>                       : <colval>   (Values to insert into columns)
00037  *
00038  * Response >
00039  *              Content-type: text/json
00040  *              {
00041  *                      code: <response code>
00042  *                      message: <response message>
00043  *                      record: <record vals>
00044  *
00045  *      Where:
00046  *              <response code> = Integer Response code.
00047  *                      Values:
00048  *                              200 = Success
00049  *                              Anything else = Failure
00050  *
00051  *              <response message> = A string describing the result of the response.
00052  *              <record vals> = A JSON object with the resulting column values in the record.
00053  *
00054  */
00055 define('REST_INSERT_VALIDATION_ERROR', 501);
00056 class dataface_actions_rest_insert {
00057         function handle($params){
00058                 if ( !defined('DISABLE_reCAPTCHA') ) define('DISABLE_reCAPTCHA', 1);
00059                 import('Dataface/QuickForm.php');
00060                 Dataface_QuickForm::$TRACK_SUBMIT = false;
00061                 $app = Dataface_Application::getInstance();
00062                 $query = $app->getQuery();
00063                 $errors = null;
00064                 
00065                 
00066                 try {
00067                 
00068                         if ( !@$_POST['-table'] ){
00069                                 throw new Exception("No table specified");
00070                         }
00071                         
00072                         $table = $_POST['-table'];
00073 
00074                         
00075                         $rec = new Dataface_Record($table, array());
00076                         $tableObj = $rec->_table;
00077                         
00078                         $fields = array();
00079                         if ( !$rec->checkPermission('new') ){
00080                                 throw new Exception("Failed to insert record.  Permission denied");
00081                         }
00082                         foreach ($_POST as $k=>$v){
00083                                 if ( $k{0} == '-' ) continue;
00084                                 $fields[] = $k;
00085                                 $rec->setValue($k, $v);
00086                                 if ( !$rec->checkPermission('new', array('field'=>$k) ) ){
00087                                         throw new Exception(sprintf("Failed to insert record because you do not have permission to insert data into the %s column", $k));
00088                                 }
00089                         }
00090                         
00091                         
00092                         
00093                         $form = df_create_new_record_form($table, $fields);
00094                         $form->_flagSubmitted = true;
00095                         $res = $form->validate();
00096                         if ( !$res ){
00097                                 $errors = $form->_errors;
00098                                 throw new Exception('Validation error', REST_INSERT_VALIDATION_ERROR);
00099                         }
00100                         
00101                         
00102                         
00103                         
00104                         
00105                         $res = $rec->save(null, true);
00106                         if ( PEAR::isError($res) ){
00107                                 throw new Exception("Failed to insert record due to a server error: ".$res->getMessage(), 500);
00108                         }
00109                         
00110                         $out = array();
00111                         $vals = $rec->strvals();
00112                         foreach ($vals as $k=>$v){
00113                                 if ( $rec->checkPermission('view') ){
00114                                         $out[$k] = $v;
00115                                 }
00116                         }
00117                         
00118                         $this->out(array(
00119                                 'code'=>200,
00120                                 'message'=>'Record successfully inserted',
00121                                 'record'=>$out
00122                         ));
00123                         exit;
00124                                 
00125                         
00126                 } catch (Exception $ex){
00127                         $this->out(array(
00128                                 'code'=>$ex->getCode(),
00129                                 'message'=>$ex->getMessage(),
00130                                 'errors'=>$errors
00131                         ));
00132                         exit;
00133                 
00134                 }
00135         }
00136         
00137         function out($params){
00138                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00139                 echo json_encode($params);
00140         }
00141 }
 All Data Structures Namespaces Files Functions Variables Enumerations