PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/perch/core/lib/PerchForm.class.php

https://bitbucket.org/pauver/dirty-bastards
PHP | 964 lines | 674 code | 265 blank | 25 comment | 150 complexity | ccc85a55d4206864cdc464fbb624efe2 MD5 | raw file
  1. <?php
  2. class PerchForm
  3. {
  4. var $html_encode = true;
  5. var $required = array();
  6. var $validate = array();
  7. var $messages = array();
  8. var $error = false;
  9. var $display_only = false;
  10. var $allow_edits = true;
  11. var $name = false;
  12. var $force_clear = false;
  13. public $submitted_via_ajax = false;
  14. public $csrf_token = false;
  15. var $fields = array();
  16. public $translate_errors = true;
  17. function __construct($name=false, $display_only=false, $allow_edits=true)
  18. {
  19. $Perch = PerchAdmin::fetch();
  20. $this->name = $name;
  21. $this->display_only = $display_only;
  22. $this->allow_edits = $allow_edits;
  23. if (isset($_GET['editform']) && $_GET['editform']==$this->name) {
  24. $this->display_only = false;
  25. }
  26. if (strpos($Perch->get_page(true), 'editform='.$this->name)>0) {
  27. $this->display_only = false;
  28. }
  29. if (!$allow_edits) {
  30. $this->display_only = true;
  31. }
  32. // check csrf token
  33. if (PerchSession::is_set('csrf_token') && PerchSession::get('csrf_token')!='') {
  34. $this->csrf_token = PerchSession::get('csrf_token');
  35. }else{
  36. $this->csrf_token = md5(uniqid('csrf', true));
  37. PerchSession::set('csrf_token', $this->csrf_token);
  38. }
  39. }
  40. public function get_token()
  41. {
  42. return $this->csrf_token;
  43. }
  44. public function set_name($name)
  45. {
  46. $this->name = $name;
  47. }
  48. public function posted()
  49. {
  50. if (isset($_POST) && isset($_POST['formaction']) && $_POST['formaction'] == $this->name) {
  51. // check csrf token
  52. if (isset($_POST['token']) && $_POST['token']!='' && $_POST['token']==$this->csrf_token) {
  53. // generate new token
  54. $this->csrf_token = md5(uniqid('csrf', true));
  55. PerchSession::set('csrf_token', $this->csrf_token);
  56. $this->display_only(false);
  57. if (isset($_POST['_perch_ajax']) && $_POST['_perch_ajax']=='1') {
  58. $this->submitted_via_ajax = true;
  59. }
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. public function required($id)
  66. {
  67. $data = $this->required;
  68. if (isset($data[$id])){
  69. return $data[$id];
  70. }
  71. return false;
  72. }
  73. public function message($id, $value)
  74. {
  75. $translate = $this->translate_errors;
  76. if ($this->error == true){
  77. if (trim($value) === ''){
  78. return ' <span class="error">' . ($translate ? $this->html(PerchLang::get($this->required($id))) : $this->html($this->required($id))) . '</span> ';
  79. }
  80. if (isset($this->messages[$id])){
  81. return ' <span class="error">' . ($translate ? $this->html(PerchLang::get($this->messages[$id])) : $this->html($this->messages[$id])) . '</span> ';
  82. }
  83. }
  84. return ' <span class="required">*</span> ';
  85. }
  86. public function error($id, $class=true)
  87. {
  88. if ($this->error == true){
  89. if ($this->required($id) && (!isset($_POST[$id]) || ($_POST[$id]) === '') && (!isset($_FILES[$id]) && !isset($_POST[$id.'_populated']))) {
  90. if ($class) return ' class="error"';
  91. return ' error';
  92. }
  93. }
  94. return '';
  95. }
  96. public function display_only($display_only=false) {
  97. $this->display_only = $display_only;
  98. }
  99. public function clear()
  100. {
  101. $this->force_clear = true;
  102. }
  103. public function is_valid($id, $value)
  104. {
  105. $r= true;
  106. $args = array();
  107. if (isset($value[2])) $args = $value[2];
  108. switch ( $value[0] )
  109. {
  110. case 'email':
  111. $r = $this->check_email($id, $args);
  112. break;
  113. case 'username':
  114. $r = $this->check_username($id, $args);
  115. break;
  116. case 'password':
  117. $r = $this->check_password($id, $args);
  118. break;
  119. default:
  120. # code...
  121. break;
  122. }
  123. if (!$r) $this->messages[$id] = $value[1];
  124. return $r;
  125. }
  126. public function validate()
  127. {
  128. $this->error = true;
  129. $r = true;
  130. //check required
  131. foreach($this->required as $key => $value) {
  132. // check in POST
  133. if (!isset($_POST[$key]) || trim($_POST[$key])==''){
  134. // check in FILES
  135. if (!isset($_FILES[$key]) && !isset($_POST[$key.'_populated'])) {
  136. $r = false;
  137. }
  138. }
  139. }
  140. //run validations
  141. foreach($this->validate as $key => $value) {
  142. if (isset($_POST[$key]) && !$_POST[$key]==''){
  143. if (!$this->is_valid($key, $value)) {
  144. $r = false;
  145. }
  146. }
  147. }
  148. if ($r) $this->error = false;
  149. return $r;
  150. }
  151. public function set_required($data)
  152. {
  153. $this->required = $data;
  154. }
  155. public function set_validation($data)
  156. {
  157. $this->validate = $data;
  158. }
  159. private function check_password($id, $args)
  160. {
  161. $str = $_POST[$id];
  162. $str2 = $_POST[$id.'2'];
  163. if ($str != $str2){
  164. return false;
  165. }
  166. return true;
  167. }
  168. private function check_email($id, $args)
  169. {
  170. $email = $_POST[$id];
  171. $Users = new PerchUsers;
  172. // check for a passed in UserID
  173. // so that a user can be excluded from the check
  174. // (so we don't prevent editing of a record)
  175. if (isset($args['userID'])) {
  176. $exclude_userID = $args['userID'];
  177. }else{
  178. $exclude_userID = false;
  179. }
  180. if (!PerchUtil::is_valid_email($email) || PerchUtil::contains_bad_str($email) || !$Users->email_available($email, $exclude_userID)){
  181. return false;
  182. }
  183. return true;
  184. }
  185. private function check_username($id, $args)
  186. {
  187. $str = $_POST[$id];
  188. $Users = new PerchUsers;
  189. // check for a passed in UserID
  190. // so that a user can be excluded from the check
  191. // (so we don't prevent editing of a record)
  192. if (isset($args['userID'])) {
  193. $exclude_userID = $args['userID'];
  194. }else{
  195. $exclude_userID = false;
  196. }
  197. if (!$Users->username_available($str, $exclude_userID)){
  198. return false;
  199. }
  200. return true;
  201. }
  202. public function get($array, $key, $default='', $POSTprefix=false)
  203. {
  204. if ($POSTprefix) {
  205. $postkey = $POSTprefix.$key;
  206. }else{
  207. $postkey = $key;
  208. }
  209. if (isset($_POST[$postkey])){
  210. return $_POST[$postkey];
  211. }else{
  212. if (isset($array) && isset($array[$key])){
  213. return $array[$key];
  214. }
  215. }
  216. // is it a date?
  217. $d = $this->get_date($postkey);
  218. if ($d!=false) return $d;
  219. return $default;
  220. }
  221. public function find_items($prefix, $keys_only=false)
  222. {
  223. $out = array();
  224. foreach($_POST as $key=>$val) {
  225. if (strpos($key, $prefix)===0) {
  226. $key = str_replace($prefix, '', $key);
  227. if ($keys_only) {
  228. $out[] = $key;
  229. }else{
  230. $out[$key] = $val;
  231. }
  232. }
  233. }
  234. return $out;
  235. }
  236. public function hint($txt)
  237. {
  238. return '<span class="hint">'.$this->html($txt).'</span>';
  239. }
  240. public function label($id, $txt, $class='', $colon=false, $translate=true)
  241. {
  242. if ($translate) $txt = PerchLang::get($txt);
  243. if ($this->display_only) return '<span class="label">'.$this->html($txt).($colon?':':'').'</span>';
  244. return '<label for="'.$this->html($id, true).'" class="'.$this->html($class, true).'">'.$this->html($txt, true).($colon?':':'') . '</label>';
  245. }
  246. public function text($id, $value='', $class='', $limit=false, $type='text', $attributes='')
  247. {
  248. $this->fields[] = $id;
  249. if ($this->display_only) return $this->html($this->value($value));
  250. if ($limit !== false) {
  251. $limit = ' maxlength="'.intval($limit).'"';
  252. }else{
  253. $limit = '';
  254. }
  255. if ($this->required($id)){
  256. $attributes .= ' required="required" ';
  257. }
  258. $s = '<input type="'.$type.'" id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" value="'.$this->html($this->value($value), true).'"'.$attributes.' class="'.$type.' '.$this->html($class, true).'"'.$limit.' />';
  259. if ($this->required($id)){
  260. $s .= $this->message($id, $value);
  261. }
  262. return $s;
  263. }
  264. public function email($id, $value='', $class='', $limit=false)
  265. {
  266. return $this->text($id, $value, $class, $limit=false, 'text email');
  267. }
  268. public function url($id, $value='', $class='', $limit=false)
  269. {
  270. return $this->text($id, $value, $class, $limit=false, 'text url');
  271. }
  272. public function color($id, $value='', $class='', $limit=false)
  273. {
  274. return $this->text($id, $value, $class, $limit=false, 'color');
  275. }
  276. public function password($id, $value='', $class='')
  277. {
  278. $this->fields[] = $id;
  279. if ($this->display_only) return $this->html($value);
  280. $s = '<input type="password" id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" value="'.$this->html($this->value($value), true).'" class="text '.$this->html($class, true).'" />';
  281. if ($this->required($id) || isset($this->messages[$id])){
  282. $s .= $this->message($id, $value);
  283. }
  284. return $s;
  285. }
  286. public function hidden($id, $value='', $skip_id=false)
  287. {
  288. $this->fields[] = $id;
  289. if ($this->display_only) return '';
  290. if ($skip_id) {
  291. $s = '<input type="hidden" name="'.$this->html($id, true).'" value="'.$this->html($value, true).'" />';
  292. }else{
  293. $s = '<input type="hidden" id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" value="'.$this->html($value, true).'" />';
  294. }
  295. return $s;
  296. }
  297. public function select($id, $array, $value, $class='')
  298. {
  299. $this->fields[] = $id;
  300. if ($this->display_only && trim($value)=='') return 'No selection';
  301. $s = '<select id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" class="'.$this->html($class, true).'">';
  302. for ($i=0; $i<PerchUtil::count($array); $i++){
  303. $s .= '<option value="'.$this->html($array[$i]['value'], true).'"';
  304. if ($array[$i]['value'] == $this->value($value)){
  305. $s .= ' selected="selected"';
  306. }
  307. if (isset($array[$i]['disabled']) && $array[$i]['disabled']) {
  308. $s .= ' disabled="disabled"';
  309. }
  310. $s .='>'.$this->html($array[$i]['label']).'</option>';
  311. if ($this->display_only && $array[$i]['value'] == $value) {
  312. return $this->html($array[$i]['label']);
  313. }
  314. }
  315. $s .= '</select>';
  316. if ($this->required($id)){
  317. $s .= $this->message($id, $value);
  318. }
  319. return $s;
  320. }
  321. function grouped_select($id, $groups, $value, $class='')
  322. {
  323. $this->fields[] = $id;
  324. if ($this->display_only && trim($value)=='') return 'No selection';
  325. $s = '<select id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" class="'.$this->html($class, true).'">';
  326. foreach($groups as $group_name=>$array) {
  327. $s .= '<optgroup label="'.$this->html($group_name, true).'">';
  328. for ($i=0; $i<PerchUtil::count($array); $i++){
  329. $s .= '<option value="'.$this->html($array[$i]['value'], true).'"';
  330. if ($array[$i]['value'] == $this->value($value)){
  331. $s .= ' selected="selected"';
  332. }
  333. $s .='>'.$this->html($array[$i]['label']).'</option>';
  334. if ($this->display_only && $array[$i]['value'] == $value) {
  335. return $this->html($array[$i]['label']);
  336. }
  337. }
  338. $s .= '</optgroup>';
  339. }
  340. $s .= '</select>';
  341. if ($this->required($id)){
  342. $s .= $this->message($id, $value);
  343. }
  344. return $s;
  345. }
  346. public function datepicker($id, $value=false)
  347. {
  348. $this->fields[] = $id;
  349. if ($this->display_only){
  350. if ($value) {
  351. return strftime('%d %b %Y', strtotime($value));
  352. }else{
  353. return '';
  354. }
  355. }
  356. $s = '';
  357. $value = ($this->value($value) ? $this->value($value) : strftime('%Y-%m-%d'));
  358. $d = array();
  359. $d['day'] = strftime('%d', strtotime($value));
  360. $d['month'] = strftime('%m', strtotime($value));
  361. $d['year'] = strftime('%Y', strtotime($value));
  362. // Day
  363. $days = array();
  364. for ($i=1; $i<32; $i++) $days[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  365. $s .= $this->select($id.'_day', $days, $d['day']);
  366. // Month
  367. $months = array();
  368. for ($i=1; $i<13; $i++) $months[] = array('label'=>strftime('%b', strtotime('2007-'.PerchUtil::pad($i).'-01')), 'value'=>PerchUtil::pad($i));
  369. $s .= $this->select($id.'_month', $months, $d['month']);
  370. // Year
  371. $years = array();
  372. for ($i=strftime('%Y')-100; $i<strftime('%Y')+11; $i++) $years[] = array('label'=>$i, 'value'=>$i);
  373. $s .= $this->select($id.'_year', $years, $d['year']);
  374. return $s;
  375. }
  376. public function datetimepicker($id, $value=false)
  377. {
  378. $this->fields[] = $id;
  379. if ($this->display_only){
  380. if ($value) {
  381. return strftime('%d %b %Y %H:%M', strtotime($value));
  382. }else{
  383. return '';
  384. }
  385. }
  386. $s = '';
  387. $value = ($this->value($value) ? $this->value($value) : strftime('%Y-%m-%d'));
  388. $d = array();
  389. $d['day'] = strftime('%d', strtotime($value));
  390. $d['month'] = strftime('%m', strtotime($value));
  391. $d['year'] = strftime('%Y', strtotime($value));
  392. $d['hour'] = strftime('%H', strtotime($value));
  393. $d['minute']= strftime('%M', strtotime($value));
  394. // Day
  395. $days = array();
  396. for ($i=1; $i<32; $i++) $days[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  397. $s .= $this->select($id.'_day', $days, $d['day']);
  398. // Month
  399. $months = array();
  400. for ($i=1; $i<13; $i++) $months[] = array('label'=>strftime('%b', strtotime('2007-'.PerchUtil::pad($i).'-01')), 'value'=>PerchUtil::pad($i));
  401. $s .= $this->select($id.'_month', $months, $d['month']);
  402. // Year
  403. $years = array();
  404. for ($i=strftime('%Y')-100; $i<strftime('%Y')+11; $i++) $years[] = array('label'=>$i, 'value'=>$i);
  405. $s .= $this->select($id.'_year', $years, $d['year']);
  406. $s .= ' : ';
  407. // Hours
  408. $hours = array();
  409. for ($i=0; $i<24; $i++) $hours[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  410. $s .= $this->select($id.'_hour', $hours, $d['hour']);
  411. // Minutes
  412. $minutes = array();
  413. for ($i=0; $i<60; $i++) $minutes[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  414. $s .= $this->select($id.'_minute', $minutes, $d['minute']);
  415. return $s;
  416. }
  417. public function timepicker($id, $value=false)
  418. {
  419. $this->fields[] = $id;
  420. if ($this->display_only){
  421. if ($value) {
  422. return strftime('%H:%M', strtotime($value));
  423. }else{
  424. return '';
  425. }
  426. }
  427. $s = '';
  428. $value = ($this->value($value) ? $this->value($value) : strftime('%H:%M'));
  429. $d = array();
  430. $d['hour'] = strftime('%H', strtotime($value));
  431. $d['minute']= strftime('%M', strtotime($value));
  432. // Hours
  433. $hours = array();
  434. for ($i=0; $i<24; $i++) $hours[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  435. $s .= $this->select($id.'_hour', $hours, $d['hour']);
  436. // Minutes
  437. $minutes = array();
  438. for ($i=0; $i<60; $i++) $minutes[] = array('label'=>PerchUtil::pad($i), 'value'=>PerchUtil::pad($i));
  439. $s .= $this->select($id.'_minute', $minutes, $d['minute']);
  440. return $s;
  441. }
  442. public function checkbox($id, $value, $checked, $class='', $group=false, $disabled=false)
  443. {
  444. $this->fields[] = $id;
  445. if (!$group){
  446. $group=$id;
  447. }else{
  448. $group = $group.'[]';
  449. }
  450. if ($this->display_only){
  451. if ($value == $checked){
  452. return 'Yes';
  453. }else{
  454. return 'No';
  455. }
  456. }
  457. $s = '<input type="checkbox" class="check '.$this->html($class, true).'" id="'.$this->html($id, true).'" name="'.$this->html($group, true).'" value="'.$this->html($this->value($value), true).'"';
  458. if ($value == $checked){
  459. $s .= ' checked="checked"';
  460. }
  461. if ($disabled) {
  462. $s .= ' disabled="disabled"';
  463. }
  464. $s .= ' />';
  465. return $s;
  466. }
  467. public function checkbox_set($id, $label, $options, $values=false)
  468. {
  469. $out = '';
  470. $out .= '<fieldset class="checkboxes"><strong>'.$this->html(PerchLang::get($label)).'</strong>';
  471. $i = 0;
  472. foreach($options as $option) {
  473. $boxid = $id.'_'.$i;
  474. $checked_value = false;
  475. if (in_array($option['value'], $values)){
  476. $checked_value = $option['value'];
  477. }
  478. if (PerchUtil::count($_POST)) {
  479. $checked_value = false;
  480. if (isset($_POST[$id]) && is_array($_POST[$id])) {
  481. if (in_array($option['value'], $_POST[$id])) {
  482. $checked_value = $option['value'];
  483. }
  484. }
  485. }
  486. if (!isset($option['disabled'])) {
  487. $option['disabled'] = false;
  488. }
  489. $out .= '<div class="checkbox">';
  490. $out .= $this->checkbox($boxid, $option['value'], $checked_value, (isset($option['class']) ? $option['class'] : false), $id, $option['disabled']);
  491. $out .= $this->label($boxid, $option['label'], '', $colon=false, $translate=false);
  492. $out .= '</div>';
  493. $i++;
  494. }
  495. $out .= '</fieldset>';
  496. return $out;
  497. }
  498. public function radio($id, $group, $value, $checked, $class='')
  499. {
  500. $this->fields[] = $id;
  501. if ($this->display_only){
  502. if ($value == $checked){
  503. return 'Yes';
  504. }else{
  505. return 'No';
  506. }
  507. }
  508. $s = '<input type="radio" class="check '.$this->html($class, true).'" id="'.$this->html($id, true).'" name="'.$this->html($group, true).'" value="'.$this->html($this->value($value), true).'"';
  509. if ($value == $checked){
  510. $s .= ' checked="checked"';
  511. }
  512. $s .= ' />';
  513. return $s;
  514. }
  515. public function textarea($id, $value='', $class='', $data_attributes=false)
  516. {
  517. $this->fields[] = $id;
  518. if ($this->display_only) return nl2br($this->html($value));
  519. $data = '';
  520. if (PerchUtil::count($data_attributes)) {
  521. foreach($data_attributes as $key=>$val) {
  522. $data .= ' data-'.$key.'="'.$this->html($val, true).'"';
  523. }
  524. }
  525. $s = '<textarea id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" class="text '.$this->html($class, true).'"'.$data.' rows="6" cols="40">'.$this->html($this->value($value)).'</textarea>';
  526. if ($this->required($id)){
  527. $s .= $this->message($id, $value);
  528. }
  529. return $s;
  530. }
  531. public function submit($id, $value, $class=false, $translate=true, $use_button=false)
  532. {
  533. $Perch = PerchAdmin::fetch();
  534. if ($this->display_only) {
  535. if ($this->allow_edits) {
  536. $segments = str_replace('/editform='.$this->name, '', split('/', $Perch->get_page(true)));
  537. $segments[] = 'editform='.$this->name;
  538. $url = implode('/', $segments);
  539. $url = str_replace('//', '/', $url);
  540. return '<a href="'.$url.'" class="button" id="'.$this->html($id, true).'">Edit</a>';
  541. }
  542. return '';
  543. }
  544. if ($translate) {
  545. $value = PerchLang::get($value);
  546. }
  547. if ($use_button) {
  548. $s = '<button type="submit" name="'.$this->html($id, true).'" id="'.$this->html($id, true).'" value="'.$this->html($value, true).'" class="'.$this->html($class, true).'"><span></span>'.$this->html($value, true).'</button>';
  549. }else{
  550. $s = '<input type="submit" name="'.$this->html($id, true).'" id="'.$this->html($id, true).'" value="'.$this->html($value, true).'" class="'.$this->html($class, true).'" />';
  551. }
  552. $s .= '<input type="hidden" name="formaction" value="'.$this->html($this->name, true).'" />';
  553. $s .= '<input type="hidden" name="token" value="'.$this->html(PerchSession::get('csrf_token'), true).'" />';
  554. return $s;
  555. }
  556. public function image($id, $value='', $basePath='', $class='')
  557. {
  558. if ($this->display_only) {
  559. if ($value) return '<img src="'.$this->html($basePath . $value, true).'" />';
  560. return '';
  561. }
  562. $s = '<input type="file" id="'.$this->html($id, true).'" name="'.$this->html($id, true).'" value="'.$this->html($this->value($value), true).'" class="'.$this->html($class, true).'" />';
  563. if ($this->required($id)){
  564. $s .= $this->message($id, $value);
  565. }
  566. return $s;
  567. }
  568. public function get_date($id, $postitems=false)
  569. {
  570. $out = '';
  571. if ($postitems === false) $postitems = $_POST;
  572. $day = (isset($postitems[$id . '_day']) ? $postitems[$id . '_day'] : false);
  573. $month = (isset($postitems[$id . '_month']) ? $postitems[$id . '_month'] : false);
  574. $year = (isset($postitems[$id . '_year']) ? $postitems[$id . '_year'] : false);
  575. $hour = (isset($postitems[$id . '_hour']) ? $postitems[$id . '_hour'] : false);
  576. $minute = (isset($postitems[$id . '_minute']) ? $postitems[$id . '_minute'] : false);
  577. if ($day!==false && $month!==false && $year!==false) {
  578. $out = "$year-$month-$day";
  579. if ($hour!==false && $minute!==false) {
  580. $out .= ' ' . PerchUtil::pad($hour) . ':' . PerchUtil::pad($minute) . ':00';
  581. }
  582. return $out;
  583. }else if($hour!==false && $minute!==false) {
  584. $out = PerchUtil::pad($hour) . ':' . PerchUtil::pad($minute) . ':00';
  585. return $out;
  586. }
  587. return false;
  588. }
  589. public function check_alpha($id)
  590. {
  591. $str = $_POST[$id];
  592. if (preg_match('/^[A-Za-z0-9_]*$/', $str)==0){
  593. return false;
  594. }
  595. return true;
  596. }
  597. public function show_fields()
  598. {
  599. $s = '<textarea rows="16" cols="80">';
  600. $s .= '$req = array();' . "\n";
  601. if (is_array($this->fields)) {
  602. foreach ($this->fields as $field){
  603. $a[] = "'" . $field . "'";
  604. $s .= '$req[\''.$field.'\'] = "Required";' . "\n";
  605. }
  606. }
  607. $s .= '$Form->set_required($req);' . "\n";
  608. $s .= 'if ($Form->posted() && $Form->validate()) {' . "\n";
  609. $s .= "\t" . '$postvars = array('.implode(', ', $a) . ');' . "\n";
  610. $s .= "\t" . '$data = $Form->receive($postvars);' . "\n";
  611. $s .= '}' . "\n";
  612. $s .= '</textarea>';
  613. return $s;
  614. }
  615. public function action()
  616. {
  617. $Perch = PerchAdmin::fetch();
  618. $url = $Perch->get_page(true);
  619. $url = str_replace('created=true&', '', $url);
  620. $url = str_replace('&created=true', '', $url);
  621. $url = str_replace('created=true', '', $url);
  622. return $url;
  623. }
  624. private function value($value)
  625. {
  626. if ($this->force_clear) return '';
  627. return stripslashes($value);
  628. }
  629. public function scaffold($DB, $table, $prefix)
  630. {
  631. $cols = $DB->get_table_meta($table);
  632. $s = '';
  633. if (is_array($cols)) {
  634. foreach($cols as $col) {
  635. if ($col->name != $prefix.'ID') {
  636. $s .= '<div>' . "\n";
  637. $s .= "\t" . '<' . '?php echo $Form->label(\'' . $col->name . '\', \'' . str_replace($prefix, '', $col->name) . '\'); ?' . '>' . "\n";
  638. switch ($col->type) {
  639. case 'blob':
  640. $s .= "\t" . '<' . '?php echo $Form->textarea(\'' . $col->name . '\', $Form->get(@$details, \'' . $col->name . '\'), \'large\'); ?' . '>' . "\n";
  641. break;
  642. default:
  643. $s .= "\t" . '<' . '?php echo $Form->text(\'' . $col->name . '\', $Form->get(@$details, \'' . $col->name . '\')); ?' . '>' . "\n";
  644. break;
  645. }
  646. $s .= '</div>' . "\n\n";
  647. }
  648. }
  649. }
  650. return '<textarea rows="16" cols="80">' . $s . '</textarea>';
  651. }
  652. public function receive($postvars)
  653. {
  654. $data = array();
  655. foreach($postvars as $val){
  656. if (isset($_POST[$val])) {
  657. if (!is_array($_POST[$val])){
  658. $data[$val] = trim($_POST[$val]);
  659. }else{
  660. $data[$val] = $_POST[$val];
  661. }
  662. }
  663. }
  664. return $data;
  665. }
  666. public function field_completed($field)
  667. {
  668. if (isset($_POST[$field]) && $_POST[$field] != '') {
  669. return true;
  670. }
  671. return false;
  672. }
  673. private function html($str, $quotes=false)
  674. {
  675. if ($this->html_encode) {
  676. return PerchUtil::html($str, $quotes);
  677. }else{
  678. return $str;
  679. }
  680. }
  681. public function disable_html_encoding()
  682. {
  683. $this->html_encode = false;
  684. }
  685. public function enable_html_encoding()
  686. {
  687. $this->html_encode = true;
  688. }
  689. public function enctype()
  690. {
  691. return 'enctype="multipart/form-data"';
  692. }
  693. public function reset()
  694. {
  695. $this->messages = array();
  696. $this->error = false;
  697. }
  698. }
  699. ?>