PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/com_qforms/admin/qforms.base.php

https://github.com/mcniac/mostly-joomla
PHP | 839 lines | 583 code | 67 blank | 189 comment | 111 complexity | 7a74aa45e34f640da34ca7d255866908 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of QForms
  4. *
  5. * qForms is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * qForms is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with qForms. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. define('QFORMS_ACTION_LIST', 'list');
  19. define('QFORMS_ACTION_VIEW', 'view');
  20. define('QFORMS_ACTION_INSERT', 'insert');
  21. define('QFORMS_ACTION_UPDATE', 'update');
  22. define('QFORMS_ACTION_DELETE', 'delete');
  23. define('QFORMS_ACTION_LISTFORM','listform');
  24. define('QFORMS_ACTION_EXPORT', 'export');
  25. define('QFORMS_ACTION_PRINT', 'print');
  26. define('QFORMS_ACTION_WIZARD', 'wizard');
  27. define('QFORMS_ACTION_FORM', 'form');
  28. define('QFORMS_ACTION_PREVIEW', 'preview');
  29. define('QFORMS_ACTION_PREVIEWDELETE', 'previewdelete');
  30. define('QFORMS_FILTER_EXACT', '1');
  31. define('QFORMS_FILTER_LIKE', '2');
  32. define('QFORMS_FILTER_RANGE', '3');
  33. define('QFORMS_FILTER_USER', '4');
  34. define('QFORMS_FILTER_NULL', '5');
  35. define('QFORMS_FILTER_ISZERO', '6');
  36. define('QFORMS_FILTER_EXACTSET','7');
  37. define('QFORMS_RENDER_STATIC', '1');
  38. define('QFORMS_RENDER_CONTROL', '2');
  39. define('QFORMS_RENDER_HIDDEN', '3');
  40. /**
  41. * Load the widget classes and other helpers.
  42. **/
  43. require_once(QFORMS_PATH.'qforms.widgets.php');
  44. if(!function_exists('adodb_date_test_date')) {
  45. require_once(QFORMS_PATH.'adodb-time.inc.php');
  46. }
  47. class QFormsBase {
  48. /**
  49. * Define filters, permission, etc.
  50. **/
  51. function Init() {
  52. $this->currAction = QFORMS_ACTION_LIST;
  53. $this->currRecord = null;
  54. $this->abm_fields = array();
  55. $this->abm_errors = array();
  56. $this->abm_data = array();
  57. $this->gpc_data = array();
  58. $this->abm_filters = array();
  59. $this->abm_hiddens = array();
  60. $this->abm_sorters = array();
  61. $this->abm_title = '';
  62. $this->abm_subtitle = '';
  63. $this->abm_pageNo = 1;
  64. $this->abm_pageCount = 1;
  65. $this->abm_orderBy = null;
  66. $this->abm_rowsPerPage = 50;
  67. $this->abm_submitted = false;
  68. $this->abm_limit = array();
  69. $this->abm_curr_rid = 0;
  70. if( !isset($this->abm_prefix) )
  71. $this->abm_prefix = 'xF_';
  72. #echo $this->abm_prefix;
  73. $this->perm_list = true;
  74. $this->perm_view = true;
  75. $this->perm_insert = true;
  76. $this->perm_update = true;
  77. $this->perm_delete = true;
  78. $this->perm_listform= true;
  79. $this->perm_export = true;
  80. $this->perm_print = true;
  81. $this->controls_before = true; // define que los controles del formulario esten delante en el modo grilla
  82. $this->qforms_language = $GLOBALS['qforms_default_language'] = 'en';
  83. $this->confirm_reload = 1; // tiempo en segundos (0-infinito) para recargar. -1 desactiva
  84. $this->confirm_html = null;
  85. $this->sql_engine = null;
  86. $this->allow_unchanged_submits = false;
  87. $this->abm_url = QForms::URL($_SERVER['REQUEST_URI']);
  88. $base_url = QForms::URL($this->abm_url, array(
  89. $this->abm_prefix.'action' => null,
  90. $this->abm_prefix.'record' => null,
  91. ));
  92. $this->abm_view_url = "$base_url&{$this->abm_prefix}action=". QFORMS_ACTION_VIEW;
  93. $this->abm_insert_url = "$base_url&{$this->abm_prefix}action=". QFORMS_ACTION_INSERT;
  94. $this->abm_update_url = "$base_url&{$this->abm_prefix}action=". QFORMS_ACTION_UPDATE;
  95. $this->abm_delete_url = "$base_url&{$this->abm_prefix}action=". QFORMS_ACTION_DELETE;
  96. $this->abm_previewdelete_url = "$base_url&{$this->abm_prefix}action=". QFORMS_ACTION_PREVIEWDELETE;
  97. $this->abm_back_url = null;
  98. $this->abm_confirm_url = null;
  99. $this->html_delete_confirmation = true;
  100. $this->abm_export_url = QForms::URL($this->abm_url, array(
  101. $this->abm_prefix.'action'=>QFORMS_ACTION_EXPORT,
  102. $this->abm_prefix.'Module'=>basename($_SERVER['SCRIPT_FILENAME'])
  103. ),null,'qforms.generic.export_print.php');
  104. $this->abm_print_url = QForms::URL($this->abm_url, array(
  105. $this->abm_prefix.'action'=>QFORMS_ACTION_PRINT,
  106. $this->abm_prefix.'Module'=>basename($_SERVER['SCRIPT_FILENAME'])
  107. ),null,'qforms.generic.export_print.php');
  108. $this->abm_listform_url = QForms::URL($this->abm_url,$this->abm_prefix.'action', QFORMS_ACTION_LISTFORM);
  109. $this->abm_list_url = QForms::URL($this->abm_url,$this->abm_prefix.'action', QFORMS_ACTION_LIST);
  110. $this->template_list = QFORMS_PATH_TEMPLATES.'qforms.template.list.php';
  111. $this->template_form = QFORMS_PATH_TEMPLATES.'qforms.template.form.php';
  112. $this->template_confirm = QFORMS_PATH_TEMPLATES.'qforms.template.confirm.php';
  113. $this->template_export = QFORMS_PATH_TEMPLATES.'qforms.template.export.php';
  114. $this->template_print = QFORMS_PATH_TEMPLATES.'qforms.template.print.php';
  115. $this->rows_total = 0;
  116. $this->flag_confirm = false;
  117. $this->abm_pk_expr = null;
  118. $this->wizard_count = null;
  119. $this->wizard_step = null;
  120. /**
  121. * GPC: Load action & misc info
  122. **/
  123. if( ($t=@strval($_GET[$this->abm_prefix.'action'])) ) $this->currAction = $t;
  124. if( ($t=@strval($_GET[$this->abm_prefix.'record'])) ) $this->currRecord = $t;
  125. if( ($t=@strval($_GET[$this->abm_prefix.'backUrl'])) ) $this->abm_back_url = $t;
  126. if( ($t=@strval($_GET[$this->abm_prefix.'confirmUrl'])))$this->abm_confirm_url = $t;
  127. if($this->html_delete_confirmation)
  128. $this->abm_delete_url = QForms::URL($this->abm_delete_url,$this->abm_prefix.'backUrl', urlencode($this->abm_back_url));
  129. $this->abm_submitted = !empty($_POST[$this->abm_prefix.'SubmitData']);
  130. $this->message = '';
  131. if( $this->currAction==QFORMS_ACTION_LISTFORM )
  132. $this->message = QForms::trans("Remember to click on the save button in order to apply the changes you made");
  133. }
  134. /**
  135. * Helper function
  136. **/
  137. function _widgets_sort(&$a,&$b) {
  138. $al = sprintf('%s |%4.4d',strtolower($a->field_group), $a->field_order);
  139. $bl = sprintf('%s |%4.4d',strtolower($b->field_group), $b->field_order);
  140. if($al == $bl) return 0;
  141. return (($al > $bl) ? +1 : -1);
  142. }
  143. /**
  144. * Internal initialization
  145. **/
  146. function Prepare() {
  147. uasort($this->abm_filters,array(&$this,'_widgets_sort'));
  148. /**
  149. * GPC: Load filters, pagination, order
  150. **/
  151. foreach(array_keys($this->abm_filters) as $name) {
  152. $this->abm_filters[$name]->abm = &$this;
  153. $this->abm_filters[$name]->Init();
  154. if( ($t=$this->abm_filters[$name]->loadGPC('xFF_', true))!==null ) {
  155. $this->abm_filters[$name]->value = $t;
  156. }
  157. }
  158. if( ($t=@strval($_GET[$this->abm_prefix.'pageNo'])) ) $this->abm_pageNo = $t;
  159. if( ($t=@strval($_GET[$this->abm_prefix.'RowsPerPage'])) ) $this->abm_rowsPerPage = $t;
  160. if( ($t=@strval($_GET[$this->abm_prefix.'orderBy'])) ) $this->abm_orderBy = $t;
  161. }
  162. /**
  163. * TODO: COMMENT
  164. **/
  165. function dataLoad($from_db=false, $from_defaults=false, $force_defaults=false) {
  166. if($from_db) {
  167. if($this->currRecord) {
  168. $this->abm_data = array($this->data_select(false, $this->currRecord));
  169. }else{
  170. $this->rows_total = $this->data_select(true);
  171. $this->abm_pageCount = ceil( $this->rows_total / $this->abm_rowsPerPage);
  172. if($this->abm_pageNo<1)
  173. $this->abm_pageNo=1;
  174. if($this->abm_pageNo>$this->abm_pageCount)
  175. $this->abm_pageNo=$this->abm_pageCount;
  176. $this->abm_limit = array(
  177. (($this->abm_pageNo>0)?(($this->abm_pageNo-1)*$this->abm_rowsPerPage):0),
  178. $this->abm_rowsPerPage );
  179. if($this->rows_total)
  180. $this->abm_data = $this->data_select();
  181. }
  182. }
  183. if($from_defaults) {
  184. if($force_defaults && empty($this->abm_data))
  185. $this->abm_data[0]=array();
  186. foreach($this->abm_data as $rid=>$rec) {
  187. foreach(array_keys($this->abm_fields) as $k) {
  188. if( !isset($rec[$k]) && !is_null($t=$this->abm_fields[$k]->GetDefault() ) ) {
  189. $this->abm_data[$rid][$k] = $t;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Read the name...
  197. **/
  198. function Validate() {
  199. foreach($this->gpc_data as $rid=>$rec) {
  200. foreach($this->GetFieldList( 'visible' ) as $name) {
  201. if( ($t=$this->abm_fields[$name]->Validate( $this->getFieldValue($name,$rid) )) )
  202. $this->abm_errors[] = $t;
  203. }
  204. $this->ValidateRec($this->getData($rid), $this->getDataDiff($rid), $this->getPK($rid,$rec), $rid);
  205. }
  206. return empty($this->abm_errors);
  207. }
  208. /**
  209. * GPC: Load fields data.
  210. **/
  211. function loadGPC() {
  212. $dirty=false;
  213. foreach($this->abm_data as $rid=>$rec) {
  214. $this->gpc_data[$rid]=array();
  215. foreach(array_keys($this->abm_fields) as $k) {
  216. //printf("<li>loadGPC: %s|%s</li>", $k, $this->abm_fields[$k]->loadGPC( $this->getWidgetName($k,$rid) ));
  217. if( ($t=$this->abm_fields[$k]->loadGPC( $this->getWidgetName($k,$rid) ))!==null ) {
  218. $this->gpc_data[$rid][$k] = $t;
  219. $dirty = true;
  220. }
  221. }
  222. }
  223. /**
  224. * This nifty little piece of code might look a bit complicated...
  225. **/
  226. $this->abm_result_data = array();
  227. $this->abm_result_dataDiff = array();
  228. foreach($this->gpc_data as $rid=>$rec) {
  229. $this->abm_result_data[$rid] = array();
  230. foreach(array_keys($this->abm_fields) as $name) {
  231. if(!$this->abm_fields[$name]->is_static) {
  232. $value=array('lasdhkq7812#!@#*34y78134 asdfS ]X ;sdkhhgh');
  233. if( isset($rec[$name]) ) {
  234. $value = $rec[$name];
  235. if(empty($value) && $this->abm_fields[$name]->is_null) $value=null;
  236. //printf("<li>POSTED rid=%s name=%s data=%s value=%s</li>\n", $rid, $name, @$this->abm_data[$rid][$name], @$value, @$this->abm_result_data[$rid][$name]);
  237. }else{
  238. //printf("<li>DEFAULT rid=%s name=%s data=%s value=%s</li>\n", $rid, $name, @$this->abm_data[$rid][$name], @$value, @$this->abm_result_data[$rid][$name]);
  239. $value = @$this->abm_data[$rid][$name];
  240. }
  241. if($value!==array('lasdhkq7812#!@#*34y78134 asdfS ]X ;sdkhhgh')) {
  242. if(empty($value) && $this->abm_fields[$name]->is_null) $value=null;
  243. $this->abm_fields[$name]->ProcessValue($value);
  244. @$this->abm_result_data[$rid][$name] = $value;
  245. @$this->abm_result_dataDiff[$rid][$name] = $value;
  246. //printf("<li>Change rid=%s name=%s data=%s value=%s</li>\n", $rid, $name, @$this->abm_data[$rid][$name], @$value, @$this->abm_result_data[$rid][$name]);
  247. }else{
  248. //printf("<li>.... WRONG(?) rid=%s name=%s data=%s value=%s</li>\n", $rid, $name, @$this->abm_data[$rid][$name], @$value, @$this->abm_result_data[$rid][$name]);
  249. }
  250. }
  251. }
  252. }
  253. /** Modo anterior, tratando de detectar cambios... no funciona muy bien
  254. foreach($this->gpc_data as $rid=>$rec) {
  255. $this->abm_result_data[$rid] = array();
  256. foreach(array_keys($this->abm_fields) as $name) {
  257. if(!$this->abm_fields[$name]->is_static) {
  258. $value=array('lasdhkq7812#!@#*34y78134 asdfS ]X ;sdkhhgh');
  259. $flag_changed=false;
  260. //printf("<li>Checking: |%s|%s|%s|</li>\n", $rid, $name, $this->abm_data[$rid][$name]);
  261. if( isset($rec[$name]) ) {
  262. $value = $rec[$name];
  263. if(empty($value) && $this->abm_fields[$name]->is_null)
  264. $value=null;
  265. //printf("<li>Changed: |%s|%s|%s|%s|</li>\n", $rid, $name, $this->abm_data[$rid][$name], $value);
  266. $flag_changed = (!isset($this->abm_data[$rid][$name])&&!is_null(@$this->abm_data[$rid][$name]) || @$this->abm_data[$rid][$name]!==$value);
  267. }else{
  268. $value = @$this->abm_data[$rid][$name];
  269. }
  270. if($value!==array('lasdhkq7812#!@#*34y78134 asdfS ]X ;sdkhhgh')) {
  271. if(empty($value) && $this->abm_fields[$name]->is_null)
  272. $value=null;
  273. $this->abm_fields[$name]->ProcessValue($value);
  274. @$this->abm_result_data[$rid][$name] = $value;
  275. if($flag_changed) {
  276. @$this->abm_result_dataDiff[$rid][$name] = $value;
  277. //printf("<li>Diff: |%s|%s|%s|%s|%s|</li>\n", $rid, $name, $this->abm_data[$rid][$name], $value, $this->abm_result_data[$rid][$name]);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. */
  284. return $dirty;
  285. }
  286. /**
  287. * Data validation & processing
  288. **/
  289. function Process() {
  290. /**
  291. * Initialize widgets & prepare the PK expression
  292. **/
  293. uasort($this->abm_fields, array(&$this,'_widgets_sort'));
  294. $exprs = array();
  295. foreach(array_keys($this->abm_fields) as $name) {
  296. $this->abm_fields[$name]->abm = &$this;
  297. $this->abm_fields[$name]->Init();
  298. if($this->abm_fields[$name]->abm_pk_order) {
  299. $exprs[] = "'$name'=>\$rec['$name']";
  300. }
  301. }
  302. if($exprs) {
  303. $this->abm_pk_expr = 'array('.implode(",",$exprs).')';
  304. /**
  305. * In case of a 'complex' (more than one field) PK, decode it
  306. **/
  307. if(count($exprs)>1) {
  308. $currPk = explode('|', $this->currRecord);
  309. $this->currRecord=array();
  310. $order=0;
  311. foreach(array_keys($this->abm_fields) as $k) {
  312. if($t=$this->abm_fields[$k]->abm_pk_order) {
  313. $this->currRecord[$k]=stripcslashes(@$currPk[$order++]);
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * Execute the corresponding action
  320. **/
  321. $data=false;
  322. switch($this->currAction) {
  323. case QFORMS_ACTION_INSERT:
  324. $this->ERROR_ON( !$this->perm_insert, QForms::trans("Access right error"));
  325. $this->dataLoad(false,true, true);
  326. #HACK DATAOBJECTS:
  327. if($this->abm_submitted && $this->loadGPC() && $this->Validate()) {
  328. $this->flag_confirm = $this->data_insert($this->getData());
  329. }
  330. #if($this->abm_submitted && $this->loadGPC() && $this->Validate() && (($data=$this->getData())||$this->allow_unchanged_submits) && $this->data_insert( $data ) ) {
  331. # $this->flag_confirm = true;
  332. #}elseif(is_null(@$data)) {
  333. # $this->abm_errors[] = QForms::trans("No changes to perform");
  334. #}
  335. break;
  336. case QFORMS_ACTION_UPDATE:
  337. $this->ERROR_ON( !$this->perm_update, QForms::trans("Access right error") );
  338. $this->ERROR_ON( !$this->currRecord, QForms::trans("Missing record") );
  339. $this->dataLoad(true,true);
  340. #HACK DATAOBJECTS:
  341. if($this->abm_submitted && $this->loadGPC() && $this->Validate()) {
  342. $this->flag_confirm = $this->data_update( $this->currRecord, $this->getData() );
  343. }
  344. #if($this->abm_submitted && $this->loadGPC() && $this->Validate() && (($data=$this->getDataDiff())||$this->allow_unchanged_submits) && $this->data_update( $this->currRecord, $data ) ) {
  345. # $this->flag_confirm = true;
  346. #}elseif(is_null(@$data)) {
  347. # $this->abm_errors[] = QForms::trans("No changes to perform");
  348. #}
  349. break;
  350. case QFORMS_ACTION_DELETE:
  351. $this->ERROR_ON( !$this->perm_delete, QForms::trans("Access right error") );
  352. $this->ERROR_ON( !$this->currRecord, QForms::trans("Missing record") );
  353. $this->dataLoad(true,false);
  354. if(!$this->abm_back_url) $this->abm_back_url=$this->abm_list_url;
  355. if(!$this->abm_confirm_url) $this->abm_confirm_url=$this->abm_list_url;
  356. if( !$this->html_delete_confirmation || $this->abm_submitted) {
  357. if($this->html_delete_confirmation)
  358. $this->abm_list_url = $this->abm_confirm_url;
  359. if( $this->data_delete( $this->currRecord ) )
  360. $this->flag_confirm = true;
  361. }
  362. break;
  363. case QFORMS_ACTION_VIEW:
  364. $this->ERROR_ON( !$this->perm_view, QForms::trans("Access right error") );
  365. $this->ERROR_ON( !$this->currRecord, QForms::trans("Missing record") );
  366. $this->dataLoad(true, true);
  367. break;
  368. case QFORMS_ACTION_PREVIEWDELETE:
  369. $this->ERROR_ON( !$this->perm_delete, QForms::trans("Access right error") );
  370. $this->ERROR_ON( !$this->perm_view, QForms::trans("Access right error") );
  371. $this->ERROR_ON( !$this->currRecord, QForms::trans("Missing record") );
  372. $this->dataLoad(true, true);
  373. break;
  374. case QFORMS_ACTION_FORM:
  375. $this->ERROR_ON( !$this->perm_update, QForms::trans("Access right error") );
  376. $this->dataLoad(true,true);
  377. if($this->abm_submitted && $this->loadGPC() && $this->Validate() && ($data=$this->getDataDiff()) ) {
  378. $this->data_update( $this->currRecord, $data );
  379. $this->flag_confirm = true;
  380. }elseif(empty($this->allow_empty_changes)&&is_null(@$data)) {
  381. $this->abm_errors[] = QForms::trans("No changes to perform");
  382. }else{
  383. // for forms, always try to fetch the data
  384. if($this->loadGPC())
  385. $this->Validate();
  386. }
  387. break;
  388. case QFORMS_ACTION_PREVIEW:
  389. $this->ERROR_ON( !$this->perm_view, QForms::trans("Access right error") );
  390. $this->ERROR_ON( !$this->currRecord, QForms::trans("Missing record") );
  391. $this->dataLoad(true,true);
  392. if( $this->loadGPC() && $this->Validate() && ($data=$this->getDataDiff()) ) {
  393. $this->data_preview( $this->currRecord, $data );
  394. }else{
  395. $this->abm_errors[] = QForms::trans("Fatal");
  396. }
  397. break;
  398. case QFORMS_ACTION_LISTFORM:
  399. $this->ERROR_ON( !$this->perm_listform, QForms::trans("Access right error") );
  400. $this->currRecord = null;
  401. $this->dataLoad(true, false);
  402. if($this->abm_submitted && $this->loadGPC() && $this->Validate() && ($alldata = $this->getDataDiff(null)) ) {
  403. $allok=true;
  404. foreach($alldata as $rid=>$data) {
  405. $allok = $allok && $this->data_update( $this->getPK($rid), $data , $rid);
  406. }
  407. if($allok)
  408. $this->flag_confirm = true;
  409. }
  410. break;
  411. case QFORMS_ACTION_WIZARD:
  412. $this->dataLoad(true,true);
  413. $this->loadGPC();
  414. $wizard_next = !empty($_POST[$this->abm_prefix.'WizardNext']);
  415. $wizard_prev = !empty($_POST[$this->abm_prefix.'WizardPrev']);
  416. unset($_POST[$this->abm_prefix.'WizardNext']);
  417. unset($_POST[$this->abm_prefix.'WizardPrev']);
  418. if($wizard_next && $this->wizard_step<$this->wizard_count) {
  419. if($this->Validate()) {
  420. $this->WizardData($this->getData());
  421. $this->wizard_step++;
  422. return true;
  423. }
  424. }elseif($wizard_prev && $this->wizard_step>1){
  425. $this->WizardData($this->getData());
  426. $this->wizard_step--;
  427. return true;
  428. }elseif( $this->abm_submitted && $this->Validate() ) {
  429. $this->WizardData($this->getData());
  430. $this->data_update( $this->currRecord, $this->WizardData() );
  431. $this->flag_confirm = true;
  432. return false;
  433. }
  434. break;
  435. default:
  436. $this->ERROR_ON( !$this->perm_list, QForms::trans("Access right error") );
  437. $this->currRecord = null;
  438. $this->dataLoad(true, true);
  439. }
  440. return $this->flag_confirm;
  441. }
  442. /**
  443. * TODO: COMMENT
  444. **/
  445. function GetFilterList( $type ) {
  446. $set=array();
  447. switch($type) {
  448. case 'visible':
  449. foreach(array_keys($this->abm_filters) as $name)
  450. if($this->abm_filters[$name]->is_visible && !$this->abm_filters[$name]->abm_filter_fixed)
  451. $set[] = $name;
  452. break;
  453. case 'hidden':
  454. foreach(array_keys($this->abm_filters) as $name)
  455. if(!$this->abm_filters[$name]->is_visible || $this->abm_filters[$name]->abm_filter_fixed)
  456. $set[] = $name;
  457. break;
  458. }
  459. return $set;
  460. }
  461. /**
  462. * TODO: COMMENT
  463. **/
  464. function GetSortList() {
  465. $set = $this->abm_sorters;
  466. foreach(array_keys($this->abm_fields) as $name) {
  467. $r=array();
  468. if( preg_match('/(?<![a-z0-9_])sortable(:[^\s]+)?(?![a-z0-9_])/', implode(' ',$this->abm_fields[$name]->_TAGS),$r) ) {
  469. $fname = htmlspecialchars((!empty($r[1]))?substr($r[1],1):$name);
  470. $caption = $this->abm_fields[$name]->caption;
  471. $set[ $fname ] = $caption;
  472. $set[ "$fname*" ] = "$caption (desc)";
  473. }
  474. }
  475. return $set;
  476. }
  477. /**
  478. * TODO: COMMENT
  479. **/
  480. function GetFieldList( $type ) {
  481. $set = array();
  482. $tags = array('no_'.$this->currAction);
  483. foreach(array_keys($this->abm_fields) as $name) {
  484. if( array_intersect($this->abm_fields[$name]->_TAGS,array('static_'.$this->currAction)) )
  485. $this->abm_fields[$name]->is_static=true;
  486. }
  487. switch($type) {
  488. case 'loadable':
  489. foreach(array_keys($this->abm_fields) as $name) {
  490. if($this->abm_fields[$name]->is_static && !array_intersect($this->abm_fields[$name]->_TAGS,$tags) )
  491. $set[] = $name;
  492. }
  493. break;
  494. case 'writable':
  495. foreach(array_keys($this->abm_fields) as $name) {
  496. if(!$this->abm_fields[$name]->is_static && !$this->abm_fields[$name]->is_readonly && !array_intersect($this->abm_fields[$name]->_TAGS,$tags) )
  497. $set[] = $name;
  498. }
  499. break;
  500. case 'visible':
  501. foreach(array_keys($this->abm_fields) as $name) {
  502. if($this->abm_fields[$name]->is_visible && !array_intersect($this->abm_fields[$name]->_TAGS,$tags))
  503. $set[] = $name;
  504. }
  505. break;
  506. case 'hidden':
  507. foreach(array_keys($this->abm_fields) as $name)
  508. if(!$this->abm_fields[$name]->is_visible)
  509. $set[] = $name;
  510. break;
  511. default:
  512. foreach(array_keys($this->abm_fields) as $name)
  513. $set[] = $name;
  514. break;
  515. }
  516. return $set;
  517. }
  518. /**
  519. * TODO: COMMENT
  520. **/
  521. function RenderField( $name, $rid, $mode=QFORMS_RENDER_CONTROL ) {
  522. $this->abm_curr_rid = $rid;
  523. $prefix = $this->getWidgetName($name,$rid);
  524. $value = $this->getFieldValue($name,$rid);
  525. if($mode==QFORMS_RENDER_HIDDEN) {
  526. $class = null;
  527. $html = $this->abm_fields[$name]->htmlHidden( $prefix , $value );
  528. }else{
  529. if($mode==QFORMS_RENDER_STATIC && !empty($this->abm_fields[$name]->is_list_control) )
  530. $mode=QFORMS_RENDER_CONTROL;
  531. list($class,$html) = $this->abm_fields[$name]->Display( $prefix , $value, ($mode==QFORMS_RENDER_STATIC) );
  532. }
  533. $t=(array)$this->abm_fields[$name];
  534. $t['caption' ] = $this->abm_fields[$name]->caption;
  535. $t['html' ] = $html;
  536. $t['id' ] = $prefix.$name;
  537. $t['class' ] = $class;
  538. $t['value' ] = $value;
  539. $t['_TAGS' ] = @$this->abm_fields[$name]->_TAGS;
  540. $t['type' ] = substr(strtolower(get_class($this->abm_fields[$name])),13);
  541. $t['description' ] = $this->abm_fields[$name]->description;
  542. $t['group' ] = $this->abm_fields[$name]->field_group;
  543. return $t;
  544. }
  545. /**
  546. * TODO: COMMENT
  547. **/
  548. function RenderFilter( $name, $mode=QFORMS_RENDER_CONTROL ) {
  549. $this->abm_filters[$name]->abm = &$this;
  550. $value = $this->abm_filters[$name]->value;
  551. if($mode==QFORMS_RENDER_HIDDEN) {
  552. $class = null;
  553. $html = $this->abm_filters[$name]->htmlHidden( 'xFF_' , $value );
  554. }else{
  555. list($class,$html) = $this->abm_filters[$name]->Display( 'xFF_' , $value, ($mode==QFORMS_RENDER_STATIC) );
  556. }
  557. return array(
  558. 'caption' => htmlspecialchars($this->abm_filters[$name]->caption),
  559. 'html' => $html,
  560. 'class' => $class,
  561. 'value' => $value,
  562. '_TAGS' => $this->abm_filters[$name]->_TAGS,
  563. 'type' => substr(strtolower(get_class($this->abm_filters[$name])),13),
  564. 'description' => $this->abm_filters[$name]->description,
  565. 'group' => $this->abm_filters[$name]->field_group
  566. );
  567. }
  568. /**
  569. * TODO: COMMENT
  570. **/
  571. function addField($obj, $pk_order=0) {
  572. //echo $obj->name;
  573. #$this->ERROR_ON(isset($this->abm_fields[$obj->name]), QForms::trans("Error. Duplicated field").': '.$obj->name );
  574. $obj->abm_pk_order = $pk_order;
  575. $obj->abm = & $this;
  576. if(is_null($obj->field_order)) $obj->field_order=count($this->abm_fields);
  577. if(is_null($obj->field_listord)) $obj->field_listord=count($this->abm_fields);
  578. $this->abm_fields[$obj->name] = $obj;
  579. }
  580. /**
  581. * TODO: COMMENT
  582. **/
  583. function addFilter($obj, $filter_type, $fixed_filter=false) {
  584. #$this->ERROR_ON(isset($this->abm_filters[$obj->name]), QForms::trans("Error. Duplicated filter") );
  585. $obj->abm_filter_type = $filter_type;
  586. $obj->abm_filter_fixed = $fixed_filter;
  587. if(is_null($obj->field_order)) $obj->field_order=count($this->abm_filters);
  588. $this->abm_filters[$obj->name] = $obj;
  589. }
  590. /**
  591. * TODO: COMMENT
  592. **/
  593. function addHidden($name, $value) {
  594. $this->abm_hiddens[$name] = $value;
  595. }
  596. /**
  597. * TODO: COMMENT
  598. **/
  599. function getFieldValue($name, $row=null) {
  600. if($row===null) $row=$this->abm_curr_rid;
  601. return (isset($this->abm_result_data[$row][$name])
  602. ?$this->abm_result_data[$row][$name]
  603. :@$this->abm_data[$row][$name]);
  604. }
  605. /**
  606. * TODO: COMMENT
  607. **/
  608. function getFilterValue($name) {
  609. return $this->abm_filters[$name]->value;
  610. }
  611. /**
  612. * TODO: COMMENT
  613. **/
  614. function getWidgetName($name, $rid=null) {
  615. return $this->abm_prefix.($rid?"_{$rid}_":"");
  616. }
  617. /**
  618. * TODO: COMMENT
  619. **/
  620. function getPK($rid, $rec=null, $encoded=false) {
  621. $pk = array();
  622. if(!$rec) $rec = @$this->abm_data[$rid];
  623. if($rec) {
  624. $pk = @eval("return $this->abm_pk_expr;");
  625. if(count($pk)==1) $pk = reset($pk);
  626. }
  627. if($encoded) {
  628. if(is_array($pk)){
  629. foreach($pk as $k=>$v) $pk[$k] = addcslashes($v,'|');
  630. $pk = urlencode(implode('|',$pk));
  631. }else{
  632. $pk = urlencode($pk);
  633. }
  634. }
  635. return $pk;
  636. }
  637. /**
  638. * TODO: COMMENT
  639. **/
  640. function getPKNames($pk) {
  641. foreach(array_keys($this->abm_fields) as $name) {
  642. $this->abm_fields[$name]->abm = &$this;
  643. $this->abm_fields[$name]->Init();
  644. if($this->abm_fields[$name]->abm_pk_order) {
  645. return array($name=>$pk);
  646. }
  647. }
  648. return array();
  649. }
  650. /**
  651. * TODO: COMMENT
  652. **/
  653. function getData($rid=0) {
  654. if($rid!==null) return @$this->abm_result_data[$rid];
  655. return $this->abm_result_data;
  656. }
  657. /**
  658. * TODO: COMMENT
  659. **/
  660. function getDataDiff($rid=0) {
  661. if($rid!==null) return @$this->abm_result_dataDiff[$rid];
  662. return $this->abm_result_dataDiff;
  663. }
  664. /**
  665. * TODO: COMMENT
  666. **/
  667. function WizardData($set=null, $clean=false) {
  668. if($clean||empty($_SESSION[$this->abm_prefix."WSESS $this->abm_title data"]))
  669. $_SESSION[$this->abm_prefix."WSESS $this->abm_title data"]=array();
  670. if($set)
  671. $_SESSION[$this->abm_prefix."WSESS $this->abm_title data"] = array_merge(@$_SESSION[$this->abm_prefix."WSESS $this->abm_title data"],$set);
  672. return @$_SESSION[$this->abm_prefix."WSESS $this->abm_title data"];
  673. }
  674. function ValidateRec($data, $dataDiff, $pk=null, $rid=null) {
  675. return empty($this->abm_errors);
  676. }
  677. function ValidateRecJS_callback($m) {
  678. switch($m[1]) {
  679. case 'idOf': return $this->abm_prefix.$this->abm_fields[$m[2]]->name;
  680. case 'valueOf': return str_replace('@abm_prefix@', $this->abm_prefix, $this->abm_fields[$m[2]]->js_getValue);
  681. case 'nameOf': return addslashes ( ($this->abm_fields[$m[2]]->field_group?"(".$this->abm_fields[$m[2]]->field_group.") ":"").$this->abm_fields[$m[2]]->caption );
  682. case 'fieldExists': return $this->abm_fields[$m[2]]->caption;
  683. }
  684. return null;
  685. }
  686. function ValidateRecJS( $items=array() ) {
  687. $visible_fields = $this->GetFieldList( 'visible' );
  688. foreach($visible_fields as $name) {
  689. if($t=$this->abm_fields[$name]->jsValidate()) {
  690. $items=array_merge($items,$t);
  691. }
  692. }
  693. $items = preg_replace_callback('/@(\w+) (\w+)@/', array(&$this,'ValidateRecJS_callback'),
  694. implode(";\n",$items) );
  695. //$items = preg_replace_callback('/@(nameOf) (\w+)@/',
  696. // create_function('$m', 'return $this->abm_fields[$m[1]]->caption;'),
  697. // $items );
  698. if($items)
  699. $items = "
  700. <script type=\"text/javascript\" >
  701. function xF_Value(elt,name) {var t=null; if(!name) name=elt.name; if(t=elt.form.elements[name]) { if(t.options) return t.options[t.selectedIndex].value; else return t.value; } }
  702. function xF_Validate(xF_Form) {
  703. var errores=[];
  704. $items
  705. var errorMsg='';
  706. for(var i=0 ; i<errores.length ; i++)
  707. if(errores[i]) errorMsg+=''+errores[i];
  708. if(errorMsg) {
  709. if(t=document.getElementById('xfABMErrors'))
  710. t.innerHTML=errorMsg.replace(/\\r?\\n/,'<br>');
  711. alert(errorMsg); return false;
  712. }
  713. return true;
  714. }
  715. </script>";
  716. return $items;
  717. }
  718. /**
  719. * Main loop. This is the method one has to override.
  720. **/
  721. function Run() {
  722. $this->Init();
  723. /**
  724. * Define filters, permission and options HERE
  725. **/
  726. $this->Prepare();
  727. /**
  728. * Define columns HERE
  729. **/
  730. $this->Process();
  731. /**
  732. * Usually Display is called elsewhere...
  733. **/
  734. }
  735. /**
  736. * TODO: COMMENT
  737. **/
  738. function data_select($do_count=false, $pk=null) {
  739. if($do_count) {
  740. return 0;
  741. }elseif($pk) {
  742. return array();
  743. }else{
  744. return array();
  745. }
  746. }
  747. /**
  748. * TODO: COMMENT
  749. **/
  750. function data_insert($data) {
  751. echo "<p>data_insert(".var_export($data,true).");</p>\n";
  752. }
  753. /**
  754. * TODO: COMMENT
  755. **/
  756. function data_update($pk, $data) {
  757. echo "<p>data_update(".var_export($pk,true).",".var_export($data,true).");</p>\n";
  758. }
  759. /**
  760. * TODO: COMMENT
  761. **/
  762. function data_delete($pk) {
  763. echo "<p>data_delete(".var_export($pk,true).");</p>\n";
  764. }
  765. /**
  766. * TODO: COMMENT
  767. **/
  768. function data_preview($pk, $data) {
  769. echo "<p>data_preview(".var_export($pk,true).",".var_export($data,true).");</p>\n";
  770. }
  771. }
  772. ?>