Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
rest_insert.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2011 Web Lite Solutions Corp (steve@weblite.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Synopsis
21  * ==========
22  *
23  * An action to insert a new record.
24  *
25  * Credits
26  * ========
27  *
28  * @author Steve Hannah <steve@weblite.ca>
29  * @created May 1, 2011
30  *
31  * Rest API:
32  * ---------
33  *
34  * POST >
35  * -table : Name of table to insert record into
36  * <colname> : <colval> (Values to insert into columns)
37  *
38  * Response >
39  * Content-type: text/json
40  * {
41  * code: <response code>
42  * message: <response message>
43  * record: <record vals>
44  *
45  * Where:
46  * <response code> = Integer Response code.
47  * Values:
48  * 200 = Success
49  * Anything else = Failure
50  *
51  * <response message> = A string describing the result of the response.
52  * <record vals> = A JSON object with the resulting column values in the record.
53  *
54  */
55 define('REST_INSERT_VALIDATION_ERROR', 501);
57  function handle($params){
58  if ( !defined('DISABLE_reCAPTCHA') ) define('DISABLE_reCAPTCHA', 1);
59  import('Dataface/QuickForm.php');
62  $query = $app->getQuery();
63  $errors = null;
64 
65 
66  try {
67 
68  if ( !@$_POST['-table'] ){
69  throw new Exception("No table specified");
70  }
71 
72  $table = $_POST['-table'];
73 
74 
75  $rec = new Dataface_Record($table, array());
76  $tableObj = $rec->_table;
77 
78  $fields = array();
79  if ( !$rec->checkPermission('new') ){
80  throw new Exception("Failed to insert record. Permission denied");
81  }
82  foreach ($_POST as $k=>$v){
83  if ( $k{0} == '-' ) continue;
84  $fields[] = $k;
85  $rec->setValue($k, $v);
86  if ( !$rec->checkPermission('new', array('field'=>$k) ) ){
87  throw new Exception(sprintf("Failed to insert record because you do not have permission to insert data into the %s column", $k));
88  }
89  }
90 
91 
92 
93  $form = df_create_new_record_form($table, $fields);
94  $form->_flagSubmitted = true;
95  $res = $form->validate();
96  if ( !$res ){
97  $errors = $form->_errors;
98  throw new Exception('Validation error', REST_INSERT_VALIDATION_ERROR);
99  }
100 
101 
102 
103 
104 
105  $res = $rec->save(null, true);
106  if ( PEAR::isError($res) ){
107  throw new Exception("Failed to insert record due to a server error: ".$res->getMessage(), 500);
108  }
109 
110  $out = array();
111  $vals = $rec->strvals();
112  foreach ($vals as $k=>$v){
113  if ( $rec->checkPermission('view') ){
114  $out[$k] = $v;
115  }
116  }
117 
118  $this->out(array(
119  'code'=>200,
120  'message'=>'Record successfully inserted',
121  'record'=>$out
122  ));
123  exit;
124 
125 
126  } catch (Exception $ex){
127  $this->out(array(
128  'code'=>$ex->getCode(),
129  'message'=>$ex->getMessage(),
130  'errors'=>$errors
131  ));
132  exit;
133 
134  }
135  }
136 
137  function out($params){
138  header('Content-type: application/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
139  echo json_encode($params);
140  }
141 }