forked from levhita/ThaFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormPattern.php
506 lines (455 loc) · 16.5 KB
/
FormPattern.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
class FormPattern Extends FieldListPattern
{
/**
* field's configuration structure
*
* fields['name'] {
* + label: The label.
* + value: The default value.
* + help: Little text to be show next to the field.
* + error_message: If set this message will be show in red below the field.
* + type: text, hidden, radio, select, textarea, date
* + parameters {}: type specific parameter.
* + input_parameters {}: Auto parsed input parameters.
* + validation: Function to be applied as validation, the function must get a
* string and return true for success or false for invalid input.
* }
* @var array
*/
protected $_fields = array();
/**
* Holds dependents information
* @var array
*/
private $_dependents = array();
/**
* Defaults to Single but can be changes to multipart with setType
* @var $type string
*/
private $_type='single';
private $_action='';
private $_method='post';
/**
* Holds conditions information
* @var array
*/
private $_conditions = array();
/**
* The id of the form, {@link Edit} forms id are hardcoded as 'main_form'
* this one is 'secondary_form' by default.
* @var string
*/
private $_form_id = "secondary_form";
protected $_default_template = '/patterns/templates/FormPattern.tpl.php';
/**
* Loads information from a config file
*
* $config_name maps to TO_ROOT/configs/models/{class_name}_{config_name}.ini by default
*
* @param string $config_name
* @param boolean $use_class_name selects if the class_name prefix should be added.
*/
public function loadConfig($config_name='default', $use_class_name = true, $vars=array() ) {
$DbConnection = DbConnection::getInstance();
if($use_class_name) {
$prefix = strtolower(get_class($this->_Row));
$file_name = TO_ROOT."/configs/models/{$prefix}_{$config_name}.yaml";
} else {
$file_name = TO_ROOT."/configs/models/{$config_name}.yaml";
}
if(!file_exists($file_name)){
Logger::log("Couldn't find config file", $file_name, LOGGER_ERROR);
return false;
}
$config = @ConfigParser::parsea_mesta($file_name, $vars);
//echo "<pre>Processed:\n".print_r($config,1)."</pre>";
if( isset($config['__general']['form_id']) ) {
$this->setFormId($config['__general']['form_id']);
}
if( isset($config['__general']['action']) ) {
$this->setAction($config['__general']['action']);
}
if( isset($config['__general']['type']) ) {
$this->setType($config['__general']['type']);
}
if( isset($config['__general']['method']) ) {
$this->setMethod($config['__general']['method']);
}
unset($config['__general']);
if( isset($config['__pattern']['add']) ) {
if ($this->_Row->getId()==0) {
foreach($config['__pattern']['add'] AS $field=>$value) {
$this->setPatternVariable($field, $value);
}
}
}
if( isset($config['__pattern']['edit']) ) {
if ($this->_Row->getId()!=0) {
foreach($config['__pattern']['edit'] AS $field=>$value) {
$this->setPatternVariable($field, $value);
}
}
}
unset($config['__pattern']);
$commands = $config['__commands'];
unset($config['__commands']);
if( isset($config['__generalAction']) ) {
$new = ($this->_Row->getId()==0)?true:false;
foreach($config['__generalAction'] AS $action=> $properties) {
$appears_in = (isset($properties['appears_in']))?$properties['appears_in']:'both';
if( $appears_in =='both' || ($new && $appears_in=='add') || (!$new && $appears_in=='edit') ) {
$this->AddGeneralAction($action, $properties['action'], $properties['title'], $properties['icon'], $properties['ajax']);
}
}
}
unset($config['__generalAction']);
foreach($config AS $field => $properties) {
if ( strpos($field, ':') !== false ) {
list($field, $action) = explode(':', $field);
if ($action == 'parameters') {
foreach($properties AS $parameter => $value) {
$this->setFieldParameter($field, $parameter, $value);
}
} else if($action == 'input_parameters') {
foreach($properties AS $parameter => $value) {
$this->setFieldInputParameter($field, $parameter, $value);
}
} else if($action == 'action') {
$this->addAction($field, $properties['action'], $properties['title'], $properties['icon'], $properties['ajax']);
} else if($action == 'splitter') {
$id = (empty($properties['id']))?$field:$properties['id'];
$position = (empty($properties['position']))?'before':$properties['position'];
$this->insertSplitter($field, $properties['content'], $position, $id);
} else if($action == 'linked') {
$this->setAsLinked($field, $properties['table_name'], $DbConnection, $properties['table_id'], $properties['name_field'], $properties['condition']);
} else if($action == 'dependent') {
$this->setFieldDependents($field, $properties['condition'], $properties['value'], $properties['dependants']);
} else if($action == 'add') {
$data = array(
'label' => ucwords(str_replace('_', ' ', $field)),
'type' => 'text',
'input_parameters'=> array('maxlength' => 45),
);
$this->insertField($field, $data, $properties['target'], $properties['position']);
} else if($action == 'move') {
$this->moveField($field, $properties['position'], $properties['target']);
}
} else {
if(is_array($properties)) {
foreach($properties AS $property => $value) {
$this->setFieldProperty($field, $property, $value);
}
}
}
}
if( isset($commands['delete']) ) {
foreach($commands['delete'] AS $delete) {
$this->deleteField($delete);
}
}
if( isset($commands['hide']) ) {
foreach($commands['hide'] AS $hide) {
$this->hideField($hide);
}
}
if( isset($commands['disable']) ) {
foreach($commands['disable'] AS $disable) {
$this->disableField($disable);
}
}
return true;
}
/**
* Parse table structure into template friendly data
*
* @return void
*/
public function parseStructure() {
$structure = $this->_Row->getStructure();
foreach($structure AS $field)
{
$aux = array();
$name = $field['Field'];
$aux['label'] = ucwords(str_replace('_', ' ', $name));
$aux['value'] = (isset($this->_Row->data[$name]))?$this->_Row->data[$name]:$field['Default'];
$this->no_fields++;
/**
* Extract type information.
* $match[0] The whole string. ie: int(11) unsigned
* $match[1] The type. ie: int
* $match[2] The type parameters ie: 11
* $match[3] Extra. ie: unsigned
*/
preg_match('/^([a-z]*)(?:\((.*)\))?\s?(.*)$/', $field['Type'], $match);
switch($match[1]){
case 'varchar':
if ( $match[2] <= 100 ) {
$aux['type'] = 'text';
$aux['size'] = $match[2];
$aux['input_parameters']['maxlength'] = $match[2];
} else {
$aux['type'] = 'textarea';
$aux['input_parameters']['cols'] = '60';
$aux['input_parameters']['rows'] = '3';
}
break;
case 'char':
if ( $match[2] <= 100 ) {
$aux['type'] = 'text';
$aux['size'] = $match[2];
$aux['input_parameters']['maxlength'] = $match[2];
} else {
$aux['type'] = 'textarea';
$aux['input_parameters']['cols'] = '60';
$aux['input_parameters']['rows'] = '3';
}
break;
case 'text':
$aux['type'] = 'textarea';
$aux['input_parameters']['cols'] = '60';
$aux['input_parameters']['rows'] = '6';
break;
case 'int':
$aux['type'] = 'text';
$aux['input_parameters']['maxlength'] = $match[2];
break;
case 'date':
$aux['type'] = 'date';
$aux['parameters']['before'] = '5';
$aux['parameters']['after'] = '5';
break;
case 'time':
$aux['type'] = 'time';
break;
case 'enum':
case 'set'://Testing
if ($match[2] == "'0','1'") {
$options = array('1'=> t('Yes'), '0'=> t('No') );
} else {
/** Retrive and parse Options **/
$options = array();
$params = explode("','", $match[2]);
$params[0] = substr($params[0], 1); //remove the first quote
$params[ count($params)-1 ] = substr($params[count($params)-1], 0, -1);//remove the second quote
$options=array_combine($params, $params);//creates a createCombox compatible array
}
$aux['type'] = 'select';
if ( count($options)<=3 ) {
$aux['type'] = 'radio';
}
$aux['parameters']['options']= $options;
break;
}
$this->_fields[$name] = $aux;
}
}
/**
* Sets the given field's parameter specific for the given type of the field
*
* Parameters are values specific for the given type of the field
* @param string $field
* @param string $parameter
* @param mixed $value
* @return bool true on success false otherwise
*/
public function setFieldParameter($field, $parameter, $value)
{
if ( isset($this->_fields[$field]) ) {
$this->_fields[$field]['parameters'][$parameter] = $value;
return true;
}
return false;
}
/**
* Sets the given field's input parameter
*
* Input parameters are pasted as is inside the html tag
* @param string $field
* @param string $input_parameter
* @param mixed $value
* @return bool true on success false otherwise
*/
public function setFieldInputParameter($field, $input_parameter, $value)
{
if ( isset($this->_fields[$field]) ) {
$this->_fields[$field]['input_parameters'][$input_parameter] = $value;
return true;
}
return false;
}
/**
* Unsets the given field's input parameter
* @param string $field
* @param string $input_parameter
* @return void
*/
public function unsetFieldInputParameter($field, $input_parameter)
{
unset($this->_fields[$field]['input_parameters'][$input_parameter]);
}
/**
* Sets a help text that will be put besides the field
*
* @param string $field the field where the help text will be added
* @param string $help_text the text
* @return bool true on success false otherwise
*/
public function setHelpText($field, $help_text)
{
return $this->setFieldProperty($field, 'help_text', $help_text);
}
public function setAsLinked($field, $table_name, DbConnection $DbConnection=null, $table_id='', $name_field='', $condition='')
{
if ( !isset($DbConnection) ) {
$DbConnection = DbConnection::getInstance();
}
if ($table_id=='') {
$table_id = "{$table_name}_id";
}
if ($name_field=='') {
$Config = Config::getInstance();
$name_field = $Config->name_field;
}
if($condition=='') {
$condition='1';
}
$sql = "SELECT $table_id, $name_field
FROM $table_name
WHERE $condition
ORDER BY $name_field";
if ( !$options = $DbConnection->getArrayPair($sql) ) {
$options = array();
}
$this->setFieldProperty($field, 'type', 'select');
$this->unsetFieldInputParameter($field, 'maxlength');
if ( count($options)<=3 ) {
$this->setFieldProperty($field, 'type', 'radio');
}
$this->setFieldParameter($field, 'options', $options);
}
/**
* Set as dependent of certain field condition a set of fields.
* @param string $field The field wich they depend.
* @param string $condition JavaScript valid condition(==, >=).
* @param string $value The value that must match (javascript, use quotes if string!).
* @param string $dependents Comma separated list of fields that depend on
* this field value.
* @return bool true on success and false otherwise.
*/
public function setFieldDependents($field, $condition, $value, $dependents)
{
$aux = array();
$aux['condition'] = $condition;
$aux['value'] = $value;
/** Transverse comma separated values into an Array **/
$aux['dependents'] = array_reverse( array_map('trim', explode(',', $dependents) ) );
/** Locate the dependents after their parent field **/
foreach ( $aux['dependents'] AS $dependent )
{
if( !$this->moveAfter($dependent, $field) ){
return false;
}
$this->setFieldProperty($dependent, 'dependent', true);
}
$this->setFieldProperty($field, 'parent', true);
$this->_dependents[$field]['all_fields'] = array_unique(array_merge((array)$this->_dependents[$field]['all_fields'] , $aux['dependents']));
$this->_dependents[$field]['conditions'][] = $aux;
return true;
}
/**
* Creates the javascript that powers the dependent engine
* @return string the code that should be added to the template using {@link addJavascript()}
*/
public function createDependentJavascript($run_update=false)
{
$code = false;
$dependant_functions=array();
if( count($this->_dependents) ) {
$dependant_function = "update" . str_replace(' ', '',ucwords(str_replace('_', ' ', $this->_form_id))) . "Dependents";
$dependant_functions[] = $dependant_function;
$code .= "\n function $dependant_function()\n {";
foreach($this->_dependents as $field => $parameters)
{
switch( $this->_fields[$field]['type'])
{
case 'select':
$get_value_string = "valSelect(document.forms['{$this->_form_id}'].$field)";
break;
case 'radio':
$get_value_string = "valRadioButton(document.forms['{$this->_form_id}'].$field)";
break;
default:
$get_value_string = "document.forms['{$this->_form_id}'].$field.value";
}
$code .= "\n field_value = $get_value_string;\n";
$first_run = true;
foreach ( $parameters['conditions'] AS $condition )
{
$Condition = (object)$condition;
$code .= ($first_run)?' if':' else if';
$code .= " ( field_value $Condition->condition $Condition->value ) {\n";
$hide_fields = array_diff($parameters['all_fields'], $Condition->dependents);
foreach ( $hide_fields AS $hide)
{
$code .= " dependent = document.getElementById('{$hide}_dependent');\n";
$code .= " dependent.style.display = 'none';\n";
}
foreach ( $Condition->dependents AS $show)
{
$code .= " dependent = document.getElementById('{$show}_dependent');\n";
$code .= " dependent.style.display = 'block';\n";
}
$code .= " } ";
$first_run = false;
}
$code .= "else {\n";
foreach ( $parameters['all_fields'] AS $all)
{
$code .= " dependent = document.getElementById('{$all}_dependent');\n";
$code .= " dependent.style.display = 'none';\n";
}
$code .= " }\n";
}
$code .=" }\n";
}
/**Run the updates when document is ready **/
if ( $run_update==true ) {
$code .= ' $(document).ready(function(){';
foreach($dependant_functions AS $dependant_function) {
$code .= "\n $dependant_function();";
}
$code .= "\n });\n";
}
return $code ;
}
/**
* Gives an unique id to the html's form markup
* @param string $form_id
* @return void
*/
public function setFormId($form_id){
$this->_form_id = $form_id;
}
public function setType($type) {
$this->_type=$type;
}
public function setAction($action) {
$this->_action=$action;
}
public function setMethod($method) {
$this->_method=$method;
}
/**
* Display the selected template with the given data and customization
* @return void
*/
public function getAsString() {
$this->assign('__dependents', $this->_dependents);
$this->assign('__form_id', $this->_form_id);
$this->assign('__action', $this->_action);
$this->assign('__type', $this->_type);
$this->assign('__method', $this->_method);
return parent::getAsString();
}
}