forked from levhita/ThaFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditPattern.php
612 lines (556 loc) · 18.6 KB
/
EditPattern.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
<?php
/**
* Holds {@link Edit} class
* @author Argel Arias <[email protected]>
* @package ThaFrame
* @copyright Copyright (c) 2007, Argel Arias <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* Provides a {@link Page} that shows a form to edit a {@link Row}
*
* @package ThaFrame
*/
class Edit extends Page
{
/**
* This is the Row to be edited
* @var Row
*/
private $Row = null;
/**
* Holds the 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
*/
private $fields = array();
/**
* Holds the number of fields that this {@link Edit} has
* @var int
*/
private $no_fields = 0;
/**
* Holds actions that will be rendered at begining or/and end of the form
* @var array
*/
private $general_actions = array();
/**
* Holds dependents information
* @var array
*/
private $dependents = array();
/**
* Holds conditions information
* @var array
*/
private $conditions = array();
/**
* Construct a {@link Edit} page
* @param string $page_name the page name to be shown
* @param string $template by default it uses Edit.tpl.php
* @return Edit
*/
public function __construct($page_name, $template='')
{
if ( empty($template) ) {
$this->setTemplate(THAFRAME . '/patterns/templates/Edit.tpl.php', true);
} else {
$this->setTemplate( $template);
}
$this->assign('page_name', $page_name);
}
/**
* Set the Row Object to be edited
*
* @param Row $Row the Row to be edited
* @return void
*/
public function setRow(Row $Row) {
$this->Row = $Row;
$this->no_fields = 0;
/** Parse table structure into template friendly data **/
$structure = $Row->getStructure();
foreach($structure AS $field)
{
$aux = array();
$name = $field['Field'];
$aux['label'] = ucwords(str_replace('_', ' ', $name));
$aux['value'] = (isset($Row->data[$name]))?$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['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['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 '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;
}
}
/**
* Moves the given field to the start of the form
* @param string $field the field to be moved
* @return bool true in success and false otherwise
*/
public function moveToStart($field)
{
if ( isset($this->fields[$field]) ) {
$aux = array ( $field => $this->fields[$field] );
unset($this->fields[$field]);
$this->fields = $aux + $this->fields;
return true;
}
return false;
}
/**
* Moves the given field to the end of the form
* @param string $field the field to be moved
* @return bool true in success and false otherwise
*/
public function moveToEnd($field)
{
if ( isset($this->fields[$field]) ) {
$aux = array ( $field => $this->fields[$field] );
unset($this->fields[$field]);
$this->fields = $this->fields + $aux;
return true;
}
return false;
}
/**
* Moves the given field before another field
* @param string $field The field to move
* @param string $before_field The field before the $field will be located
* @return bool true on success and false otherwise
*/
public function moveBefore($field, $before_field)
{
if ( isset($this->fields[$field]) ) {
$field_data = $this->fields[$field];
unset($this->fields[$field]);
return $this->insertField($field, $field_data, $before_field, 'before');
}
return false;
}
/**
* Moves the given field before another field
* @param string $field The field to move
* @param string $after_field The field after the $field will be located
* @return bool true on success and false otherwise
*/
public function moveAfter($field, $after_field)
{
if ( isset($this->fields[$field]) ) {
$field_data = $this->fields[$field];
unset($this->fields[$field]);
return $this->insertField($field, $field_data, $after_field, 'after');
}
return false;
}
/**
* 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).
* @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;
}
/**
* Insert a Field after or before the given target
* @param string $field_name How will be named the field
* @param array $field_data a complete field array
* @param string $target The name of the field after or before we'll
* insert the new field.
* @param string $position 'after' or 'before', Default: 'after'
* @return bool true on success false otherwise
*/
public function insertField($field_name, $field_data, $target, $position='after')
{
$success = false;
/** there is no easy way to insert an element into an array, so we need to
recreate it, inserting the field when we detect the $target **/
$new_fields = array();
reset($this->fields);
while (list($key, $value) = each($this->fields) ) {
if($position=='after') {
$new_fields[$key] = $value;
}
if ( $key === $target) {
if ( $field_name!='' ) {
$new_fields[$field_name] = $field_data;
} else {
$new_fields[] = $field_data;
}
$success = true;
}
if($position=='before') {
$new_fields[$key] = $value;
}
}
$this->fields = $new_fields;
return $success;
}
public function setFieldOrder($fields)
{
$fields = explode(',', $fields);
$fields = array_map('trim', $fields);
if ( count($fields)!=$this->no_fields) {
throw new LogicException("The number of fields doesn't match the ones in the Row, you are missing some fields");
}
$new_fields= array();
foreach($fields as $field)
{
if ( !isset($this->fields[$field]) ) {
throw new LogicException("The given field '$field' doesn't exist");
}
$new_fields[$field] =$this->fields[$field];
}
$this->fields = $new_fields;
}
/**
* Inserts a separator (with optional content) at the given position.
* @param string $target The field after the separator will be created
* @param string $content The content that will be inside the separator
* @param string $position 'after' or 'before', Default: 'after'
* @return bool true on success false otherwise
* @deprectated in favor of insertSplitter.
*/
public function insertSeparator($target, $content='', $position='after', $name='')
{
return $this->insertSplitter($target, $content, $position, $name);
}
/**
* Inserts an splitter (with optional content) at the given position.
* @param string $target The field after the separator will be created
* @param string $content The content that will be inside the splitter
* @param string $position 'after' or 'before', Default: 'after'
* @return bool true on success false otherwise
*/
public function insertSplitter($target, $content='', $position='after', $name='')
{
$aux= array('type' => 'separator', 'content' => $content);
return $this->insertField("{$name}_splitter", $aux, $target, $position);
}
/**
* Sets the given field's property
* @param string $field
* @param string $property
* @param mixed $value
* @return bool true on success false otherwise
*/
public function setFieldProperty($field, $property, $value)
{
if ( isset($this->fields[$field]) ) {
$this->fields[$field][$property] = $value;
return true;
}
return false;
}
/**
* Sets the given field's parameter
* @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
* @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 the name of a field as will be show in the Label
*
* If not customized this name is created by replacing underscores with espaces
* and capitalizing each word in the field name.
* @param string $field the field where the name will be changed
* @param string $name the new name
* @return bool true on success false otherwise
*/
public function setName($field, $name)
{
return $this->setFieldProperty($field, 'label', $name);
}
/**
* 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);
}
/**
* Sets the field as hidden
*
* Commonly used with the row id.
* To really delete the field from the Form use {@link deleteField}.
* @param string $field the name of the field to hide
* @return bool true on success false otherwise
*/
public function hideField($field)
{
return $this->setFieldProperty($field, 'type', 'hidden');
}
/**
* Sets a field as dependent
*
* @param string $field The field that will be set as dependent
* @return bool true on success false otherwise
*
public function setAsDependent($field)
{
return $this->setFieldProperty($field, 'dependent', true);
}*/
/**
* Deletes a field from the form
*
* If you only wish to hide a field use {@link hideField}
* @param string $field the name of the field to be deleted
* @return void
*/
public function deleteField($field) {
if ( isset($this->fields[$field]) ) {
unset( $this->fields[$field] );
$this->no_fields--;
return true;
}
return false;
}
public function setAsLinked($field, $table_name, $DbConnection, $table_id='', $name_field='', $conditions='')
{
if ($table_id=='') {
$table_id = "{$table_name}_id";
}
if ($name_field=='') {
$Config = Config::getInstance();
$name_field = $Config->name_field;
}
if ($conditions=='') {
$conditions = '1';
}
$sql = "SELECT $table_id, $name_field
FROM $table_name
WHERE $conditions
ORDER BY $name_field";
$options = $DbConnection->getArrayPair($sql);
$this->setFieldProperty($field, 'type', 'select');
$this->unsetFieldInputParameter($field, 'maxlength');
if ( count($options)<=3 ) {
$this->setFieldProperty($field, 'type', 'radio');
}
$this->setFieldParameter($field, 'options', $options);
}
/**
* Add an action to the end & start of the Form, commonly used to add a
* "Delete" link
*
* @param string $action The action that will be called after clicking (url)
* @param string $title The text to show and will be added to the url title as well
* @param string $field The field to add into de URL
* @param string $value The value that such field should take usally 0 for new elements
* @param string $icon The optional icon that could go with the text
* @return void
*/
public function AddGeneralAction($action, $title, $icon='', $ajax=false)
{
$aux = array (
'action' => $action ,
'title' => $title,
'icon' => $icon,
'ajax' => $ajax,
);
$this->general_actions[] = $aux;
}
/**
* Display the selected template with the given data and customization
* @return void
*/
public function display() {
if ( count($this->dependents) ) {
$this->addJavascript( $this->createDependentJavascript() );
}
$this->assign('data' , $this->Row->data);
$this->assign('dependents', $this->dependents);
$this->assign('fields' , $this->fields);
$this->assign('links' , $this->links);
$this->assign('general_actions', $this->general_actions);
parent::display();
}
/**
* Creates the javascript that powers the depedent engine
* @return string the code that should be added to the template using {@link addJavascript()}
*/
private function createDependentJavascript()
{
$code = false;
if( count($this->dependents) ) {
$code .= "\n function updateDependents()\n {";
foreach($this->dependents as $field => $parameters)
{
switch( $this->fields[$field]['type'])
{
case 'select':
$get_value_string = "valSelect(document.forms['main_form'].$field)";
break;
case 'radio':
$get_value_string = "valRadioButton(document.forms['main_form'].$field)";
break;
default:
$get_value_string = "document.forms['main_form'].$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";
}
return $code;
}
public function disableField($field)
{
return $this->setFieldProperty($field, 'disabled', 'true');
}
}