PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/resource/insertall.php

https://code.google.com/p/s3db/
PHP | 344 lines | 224 code | 82 blank | 38 comment | 39 complexity | 6b3f5d9a1e3224ca2ea1d4ba0d275aa6 MD5 | raw file
  1. <?php
  2. #insertall.php is a form for inserting the values of rules in a single instance at once
  3. #Helena F Deus (helenadeus@gmail.com)
  4. ini_set('display_errors',0);
  5. if($_REQUEST['su3d'])
  6. ini_set('display_errors',1);
  7. if($_SERVER['HTTP_X_FORWARDED_HOST']!='')
  8. $def = $_SERVER['HTTP_X_FORWARDED_HOST'];
  9. else
  10. $def = $_SERVER['HTTP_HOST'];
  11. if(file_exists('../config.inc.php'))
  12. {
  13. include('../config.inc.php');
  14. }
  15. else
  16. {
  17. Header('Location: http://'.$def.'/s3db/');
  18. exit;
  19. }
  20. #just to know where we are...
  21. $thisScript = end(explode('/', $_SERVER['SCRIPT_FILENAME'])).'?'.$_SERVER['argv'][0];
  22. $key = $_GET['key'];
  23. #echo '<pre>';print_r($_GET);
  24. #Get the key, send it to check validity
  25. include_once('../core.header.php');
  26. if($key)
  27. $user_id = get_entry('access_keys', 'account_id', 'key_id', $key, $db);
  28. else
  29. $user_id = $_SESSION['user']['account_id'];
  30. #Universal variables
  31. $instance_id = ($_REQUEST['item_id']!='')?$_REQUEST['item_id']:$_REQUEST['instance_id'];
  32. if($instance_id)
  33. $instance_info = URIinfo('I'.$instance_id, $user_id, $key, $db);
  34. #echo '<pre>';print_r($instance_info);exit;
  35. if($instance_id=='')
  36. {
  37. echo "Please specify a valid item_id";
  38. exit;
  39. }
  40. else
  41. {
  42. if(!$instance_info['add_data'])
  43. {
  44. echo "User cannot add statements in this instance";
  45. exit;
  46. }
  47. else
  48. {
  49. #include all the javascript functions for the menus...
  50. include('../S3DBjavascript.php');
  51. #and the short menu for the resource script
  52. if($class_id=='')
  53. $class_id= $instance_info['resource_class_id'];
  54. if($class_id == '')
  55. $class_id = $instance_info['class_id'];
  56. include('../action.header.php');
  57. #include the header for the instance
  58. include('instance.header.php'); #this is a header but an html one, not an HTTP one.
  59. echo '<form name="insertstatement" enctype="multipart/form-data" action="'.$action['instanceform'].'" method="post" autocomplete="on">';
  60. $s3ql = compact('db', 'user_id');
  61. $s3ql['select'] = '*';
  62. $s3ql['from'] = 'rules';
  63. $s3ql['where']['subject_id'] = $instance_info['class_id'];
  64. if($_REQUEST['project_id'])
  65. $s3ql['where']['project_id']=$_REQUEST['project_id'];
  66. $s3ql['where']['object'] = "!=UID";
  67. $s3ql['format']='html';
  68. $rules = S3QLaction($s3ql);
  69. #echo '<pre>';print_r($rules);exit;
  70. #need to remove the non-add data rules
  71. $js = sprintf("%s\n", '<script type="text/javascript">');
  72. $js .= sprintf("%s\n", 'function go_to_right_position()');
  73. $js .= sprintf("%s\n", '{');
  74. if($_POST['insert_all'])
  75. {
  76. $rule_ids = find_out_inserted_statement(compact('instance_info', 'rules','project_id', 'db')); #figure out how many/which rules were inserted
  77. $js .= sprintf("%s\n", ' window.location="#'.$rule_ids[0].'"');
  78. if(is_array($rule_ids))
  79. {
  80. echo render_inserted_statement_all(compact('instance_info', 'rules','user_id', 'db','rule_ids', 'project_id'));
  81. }
  82. }
  83. else
  84. {
  85. $_SESSION['current_color']='0';
  86. $_SESSION['previous_verb']='';
  87. $resource_id=$instance_info['resource_id'];
  88. if(is_array($rules))
  89. {
  90. $stats ='';
  91. $index = 1;
  92. foreach($rules as $rule_info)
  93. {
  94. if($rule_info['add_data'])
  95. $form .= render_empty_form(compact('index', 'rule_info', 'project_id', 'instance_id', 'db', 'user_id'));
  96. }
  97. }
  98. echo $form;
  99. }
  100. $js .= sprintf("%s\n", '}');
  101. $js .= sprintf("%s\n", '</script>');
  102. echo $js;
  103. if(is_array($rules)){
  104. echo '<input type="submit" name="insert_all" value="Insert">';
  105. echo '&nbsp;&nbsp;&nbsp;<input type="button" name="clean" value="Clear Form" onClick="window.location=\''.$action['instanceform'].'\'">';
  106. echo '&nbsp;&nbsp;&nbsp;<input type="button" name="clean" value="View Data" onClick="window.location=\''.$action['item'].'\'">';
  107. }
  108. else {
  109. echo "Create some rules for this class before inserting data.";
  110. }
  111. echo '</form>';
  112. }
  113. }
  114. function find_out_inserted_statement($S)
  115. {extract($S);
  116. $resource_id = $instance_info['resource_id'];
  117. #echo '<pre>';print_r($S);exit;
  118. $rule_ids = Array();
  119. if(is_array($rules))
  120. foreach($rules as $rule_info)
  121. {
  122. $rule_id = $rule_info['rule_id'];
  123. if($_POST['insert_all'])
  124. {
  125. if($_POST['input_'.$resource_id.'_'.$rule_id] !='' || $_POST['text_'.$resource_id.'_'.$rule_id] !='' || $_FILES['upload_input_'.$resource_id.'_'.$rule_id]['name'] !='' || $_POST['input_'.str_replace('.','_', $resource_id).'_'.str_replace('.','_',$rule_id)]!='')
  126. array_push($rule_ids, $rule_id);
  127. }
  128. else
  129. {
  130. if($_POST['insert_'.$resource_id.'_'.$rule_id] && ($_POST['input_'.$resource_id.'_'.$rule_id] !='' || $_POST['text_'.$resource_id.'_'.$rule_id] !=''))
  131. array_push($rule_ids, $rule_id);
  132. }
  133. }
  134. return array_unique($rule_ids);
  135. }
  136. # Function to parse a submitted statement
  137. function render_inserted_statement_all($I)
  138. {extract($I);
  139. $_SESSION['current_color']='0';
  140. $_SESSION['previous_verb']='';
  141. $instance_id = $instance_info['resource_id'];
  142. $stats ='';
  143. if(is_array($rules))
  144. foreach($rules as $rule_info)
  145. {
  146. $report_msg ='';
  147. $subject = $rule_info['subject'];
  148. $verb = $rule_info['verb'];
  149. $object = $rule_info['object'];
  150. $rule_id = $rule_info['rule_id'];
  151. $rule_notes = $rule_info['notes'];
  152. $notes = $_POST['text_'.$instance_id.'_'.$rule_id];
  153. $index = $index+1;
  154. if(in_array($rule_id, $rule_ids))
  155. {
  156. #echo 'upload_input_'.$instance_id.'_'.$rule_id.'<BR>';
  157. #gather data from post
  158. if($_FILES['upload_input_'.$instance_id.'_'.$rule_id]['name']=='')
  159. {
  160. $value = $_POST['input_'.$instance_id.'_'.$rule_id];
  161. if($value=='')
  162. $value = $_POST['input_'.str_replace('.', '_',$instance_id).'_'.str_replace('.', '_',$rule_id)];
  163. #insert the statement, run S3QL
  164. $s3ql = compact('db', 'user_id');
  165. $s3ql['insert'] = 'statement';
  166. #$s3ql['where']['project_id'] = $project_id;
  167. $s3ql['where']['item_id'] = $instance_id;
  168. $s3ql['where']['rule_id'] = $rule_id;
  169. $s3ql['where']['value'] = $value;
  170. $s3ql['where']['notes'] = $notes;
  171. #$s3ql['format']='html';
  172. $done = S3QLaction($s3ql);
  173. $done = html2cell($done);
  174. #ereg('<error>([0-9]+)</error>.*<(message|statement_id)>(.*)</(message|statement_id)>', $done, $s3qlout);
  175. $statement_id = $done[2]['statement_id'];
  176. $S = compact('user_id', 'rule_info', 'instance_id', 'statement_id', 'value', 'notes', 'db', 'done');
  177. if($done[2]['error_code']=='0')
  178. {
  179. $report_msg = render_inserted($s3ql, $statement_id);
  180. #$report_msg .= sprintf("%s\n", ' <br /><input type="button" value="Insert Another" onClick="window.location=\''.$action['instanceform'].'\'">');
  181. #$report_msg .= sprintf("%s\n", ' <br /><input type="button" value="Close Window" onClick="window.location=\''.$action['instanceform'].'\'">');
  182. }
  183. else
  184. {
  185. $report_msg = couldnot_insert_statement($S);
  186. #render_statement_already_exists($s3ql);
  187. }
  188. #elseif($s3qlout[1]=='7')
  189. #{
  190. #$report_msg = render_resource_doesnot_exist($s3ql);
  191. #}
  192. #elseif($s3qlout[1]=='3')
  193. #{
  194. #$report_msg = render_value_cannot_be_null($s3ql);
  195. #}
  196. }
  197. else #a file was uploaded
  198. {
  199. #project is the same that will go to instance
  200. $project_id = $rule_info['project_id'];
  201. $value = project_folder_name($project_id, $db);
  202. $notes = $_REQUEST['text_'.$instance_id.'_'.$rule_id];
  203. $filename = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['name'];
  204. $mimetype = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['type'];
  205. $filesize = filesize($_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name']);
  206. $uploadedfile = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name'];
  207. if ($filesize <= 0)
  208. $report_msg = 'Filesize cannot be null';
  209. elseif($filename == '')
  210. $report_msg = 'Filename cannot be empty';
  211. elseif($value=='' || $uploadedfile=='')
  212. $report_msg = 'Could not move file, please check with you administrator if file uploads are allowed.';
  213. else
  214. {
  215. $tmp = fileNameAndExtension($filename);
  216. extract($tmp);
  217. #write a filekey to send the file by the API
  218. $filekey = generateAFilekey(compact('filename', 'extension', 'filesize', 'user_id', 'db'));
  219. #move the file like the API would do
  220. $file = $uploadedfile;
  221. $fileMoved = MoveFile(compact('filekey','db', 'file'));
  222. #generate a statement_id
  223. if($fileMoved)
  224. {
  225. $s3ql = compact('db', 'user_id');
  226. $s3ql['insert'] = 'file';
  227. $s3ql['where']['filekey'] = $filekey;
  228. $s3ql['where']['notes'] = $notes;
  229. $s3ql['where']['project_id'] = $project_id;
  230. $s3ql['where']['rule_id'] = $rule_id;
  231. $s3ql['where']['item_id'] = $instance_id;
  232. $s3ql['format']='html';
  233. $done = S3QLaction($s3ql);
  234. $done=html2cell($done);
  235. #echo '<pre>';print_r($done);
  236. #ereg('<statement_id>([0-9]+)</statement_id>', $done, $s3qlout);
  237. $statement_id = $done[2]['file_id'];
  238. if($statement_id!='')
  239. {
  240. $insert='file';
  241. $S = compact('rule_id', 'instance_id','db','insert','filename');
  242. $report_msg = render_inserted($S, $statement_id);
  243. }
  244. }
  245. else
  246. {
  247. $report_msg = "<font color='red'>Could not move the file</font>";
  248. exit;
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. $report_msg = render_empty_form(compact('index', 'rule_info', 'project_id', 'instance_id', 'db'));
  256. }
  257. $finalOutput .= $report_msg;
  258. }
  259. //echo $stats;
  260. return $finalOutput;
  261. }
  262. ?>