forked from levhita/ThaFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListingPattern.php
591 lines (532 loc) · 17 KB
/
ListingPattern.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
<?php
/**
* Holds {@link ListingPattern} 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 PagePattern} that shows an item's list.
*
* Trough several methods it allows you to add links and actions asociated
* with each row, as well as formating
* @package ThaFrame
* @todo paging , column re-ordering
*/
class ListingPattern extends PagePattern
{
/**
* Holds all the raw data that will be listed
* @var array
*/
private $_rows = array();
/**
* Holds the field names that will form the table header
* @var array
*/
private $_fields = array();
/**
* Holds the field names for wich the query can be ordered by
* @var array
*/
private $_order_by = array();
/**
* Holds the links that will be embedded into some fields
* @var array
*/
private $_links = array();
/**
* Holds the links that will be added in the last column
* @var array
*/
private $_actions = array();
/**
* Holds the tooltips that will be attached to the fields.
* @var array
*/
private $_tooltips = array();
/**
* Holds the prefix that'll be used to create an unique id for every row
* @var string
*/
private $_prefix = '';
/**
* Points to the field that will be used to create the unique id
* @var string
*/
private $_row_id = '';
/**
* Holds actions that will be rendered at begining or/and end of the list
* actions that belong to the paga, and not to a specific row
* @var array
*/
private $_general_actions = array();
/**
* Holds filter options and configuration
* @var array
*/
private $_filters = array();
/**
* Holds classes to be applied to the columns
* @var array
*/
private $_classes = array();
/**
* Stores if the page should be paginated
*
* @var boolean
*/
private $_paginate = false;
/**
* Holds the number of elements for each page
*
* @var integer
*/
private $_page_size = 50;
/**
* Which page to show
*
* @var integer
*/
private $_page_number = 0;
/**
* Number of pages
*
* @var integer
*/
private $_pages = 0;
/**
* Wich is the final SQL query to be called
*
* @var string
*/
private $_sql = '';
/**
* The conditions string that is generated after take in account the applied filters
*
* @var string
*/
private $_conditions = '';
/**
* Construct a {@link Listing} page
* @param string $page_name the page name to be shown
* @param string $template by default it uses Listing.tpl.php
* @return Listing
*/
public function __construct($page_name='', $template='', $layout='')
{
if ( empty($template) ) {
$this->setTemplate(THAFRAME . '/patterns/templates/ListingPattern.tpl.php', true);
} else {
$this->setTemplate($template);
}
$this->setPageName($page_name);
$this->setLayout($layout);
}
/**
* Set the raw data that will be show.
*
* Field names to be used as table headers are extracted and formatted in this
* phase, they can of course be overrride using {@link setName}
* @param array $rows the raw data
* @return void
*/
public function setRows($rows) {
$this->_rows = $rows;
if ( $rows ) {
$fields_names = array_keys($rows[0]);
foreach($fields_names AS $field_name)
{
$this->_fields[$field_name] = ucwords(str_replace('_', ' ',$field_name));
}
}
}
/**
* Names each row with an unique id.
*
* It's formed with the prefix + the given field's value. Used to provide
* named items that can be referred easily with {@link xajax} and javascript
* @param string $prefix the prefix
* @param string $field the field
* @return void
*/
public function setRowId($prefix, $field )
{
$this->_prefix = $prefix;
$this->_row_id = $field;
}
/**
* Sets the name of a field as will be show in the table header.
*
* 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 void
*/
public function setName($field, $name)
{
if ( isset($this->_fields[$field]) ) {
$this->_fields[$field] = $name;
}
}
/**
* Adds an action link embedded into the given field.
* @param string $field The field that will have the embedded link (ie name).
* @param string $value The field that will serve as the value (ie item_id).
* @param string $action The action to be performed (aka URL).
* @param string $title An optional title for the link.
* @return void
*/
public function addLink($field, $value, $action, $title='' ) {
$aux = array (
'value' => $value ,
'action' => $action ,
'title' => $title
);
$this->_links[$field] = $aux;
}
/**
* Adds an action link at the end of the row
* @param string $value The field that will serve as the value (ie item_id)
* @param string $action The action to be performed, can be xajax or an URL
* @param string $title The title of the link
* @param string $icon An optional icon, if not provided a regular link is created
* @param bool $ajax Tells if the action is xajax or a regular URL
* @return void
* @todo create a multiple parameter action creator
*/
public function addAction($value, $action, $title, $icon='', $ajax=false)
{
$aux = array (
'value' => $value ,
'action' => $action ,
'title' => $title,
'icon' => $icon,
'ajax' => $ajax,
);
if(strpos($value,',')!==false){
$single_values = explode(',', $value);
$values = array();
foreach($single_values AS $single_value){
$values[]=trim($single_value);
}
$aux['value'] = $values;
}
$this->_actions[] = $aux;
}
/**
* Adds a tooltip to a field
*
* @param string $field the field that'll have the tooltip attached
* @param string $text Can be someting as "tooltip: %field%", where field can
* be any field in the query.
* @todo Make this work
* @return void
*/
public function addToolTip($field, $text) {
$text = t($text);
preg_match_all("/\%([A-Za-z0-9_\-]+)\%/", $text, $matches);
$fields = $matches[1];
$aux = array (
'text' => $text,
'fields' => $fields,
);
$this->_tooltips[$field] = $aux;
$javascript = <<<EOT
$(document).ready(function(){
$(".tooltip_$field").each(function(i){
$(this).simpletip({
boundryCheck: true,
fixed: true,
position: 'left',
showEffect: "", /*slide, fade*/
hideEffect: "",
content: $(this).attr('title')
});
});
});
EOT;
//{ $("JQUERY SELECTOR").each(function(i){
// $(this).simpletip({ content: arrayData[i] });
//});
//bodyHandler: function() {
// return $($(this).attr("title")).html();
//}
$this->addJavascript($javascript);
}
/**
* Avoid the given field to be show.
*
* Commonly used to hide the table id, but still allow its use in
* {@link addAction()} or {@link addLink()} as $value.
* @param string $field the name of the field to hide
* @return void
*/
public function hideField($field) {
unset( $this->_fields[$field] );
}
/**
* Add an action to the end & start of the Listing, commonly used to add a
* "Create new item" 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 Tn optional icon that could go with the text
* @return void
*/
public function addGeneralAction($action, $title, $field='', $value='', $icon='', $ajax=false)
{
$aux = array (
'action' => $action ,
'title' => $title,
'field' => $field ,
'value' => $value ,
'icon' => $icon,
'ajax' => $ajax,
);
$this->_general_actions[] = $aux;
}
/**
* Adds a filter form to the list
* @param $field
* @param $label
* @param $type
* @return bool
*/
public function addFilter($field, $label, $type='custom')
{
$aux = array (
'label' => $label,
'type' => $type,
'empty' => $empty,
'options' => array()
);
$this->_filters[$field] = $aux;
}
public function addHiddenFilter($field, $value, $condition){
$aux = array (
'type' => 'hidden',
'value' => $value,
'condition' => $condition
);
$this->_filters[$field] = $aux;
}
/**
* Adds a filter option
* @param $field
* @param $value
* @param $label
* @param $default
* @param $condition
* @return bool
*/
public function addFilterOption($field, $value, $label, $default=FALSE, $condition='')
{
$aux = array (
'label' => $label,
'value' => $value,
'condition' => $condition,
);
if($default) {
$this->_filters[$field]['default']= $value;
}
$this->_filters[$field]['options'][] = $aux;
}
public function setClass($field, $value) {
$this->_classes[$field] = $value;
}
/**
* Adds a group of options to a filter
*
* @param $field Field where the filter will be applied.
* @param $values Array in the form of array(value=>label, value=>label);
* @param $condition condition to be applied, {value}s and {label}s in the string will be
* replaced with the corresponding value and label.
*/
public function addFilterOptions($field, $values, $condition){
if (is_array($values) ) {
foreach($values as $value=>$label) {
$search = array('{value}', '{label}');
$replace = array(mysql_escape_string($value), mysql_escape_string($label));
$replaced_condition = str_replace($search, $replace, $condition);
$this->addFilterOption($field, $value, $label, FALSE, $replaced_condition);
}
}
}
public function setQuery($sql, DbConnection $DbConnection=null, $paginate = false) {
if (is_null($DbConnection) ) {
$DbConnection = DbConnection::getInstance();
}
if ($paginate) {
$this->_paginate = true;
//Get a Grip of the whole thing
$sql_without_conditions = str_replace('{conditions}','',$sql);
$sql_without_conditions = str_replace('{order_by}','',$sql_without_conditions);
$count_sql = "SELECT count(*) FROM ($sql_without_conditions) AS count_table;";
$total_rows = $DbConnection->getOneValue($count_sql);
//Create some basic pattern variables
$this->_page_number = (empty($_GET['__page_number']))?'0':$_GET['__page_number'];
$this->_page_size = (empty($_GET['__page_size']))?$this->_page_size:$_GET['__page_size'];
$this->_pages = ceil($total_rows/$this->_page_size);
if($this->_page_number > $this->_pages){
$this->_page_number = $this->_pages-1;
}
//Reformat the query to use MySQL Limit clause
$page_start = $this->_page_number * $this->_page_size;
$sql = "$sql
LIMIT $page_start, $this->_page_size";
$this->setPatternVariable('paginate', $this->_paginate);
$this->setPatternVariable('page_number', $this->_page_number);
$this->setPatternVariable('page_size',$this->_page_size);
$this->setPatternVariable('pages', $this->_pages);
}
$conditions = '';
if ( count($this->_filters) ) {
foreach($this->_filters AS $field => $filter) {
$Filter = (object)$filter;
if($Filter->type=='custom') {
//echo "Checking for filter on '$field'\n";
if( isset($_GET[$field]) ) {
$selected = stripslashes($_GET[$field]);
} else {
$selected = $this->_filters[$field]['default'];
}
//echo "Selected:$selected\n";
foreach($Filter->options AS $option){
//echo "Comparing value: {$option['value']}\n";
if ( $option['value'] == $selected) {
//echo "Match!, condition added '{$option['condition']}'\n\n";
$conditions .= "\nAND ";
$conditions .= $option['condition'];
$this->_filters[$field]['selected']= $selected;
}
}
} else if($Filter->type=='hidden') {
$conditions .= "\nAND ";
$conditions .= $Filter->condition;
}
}
}
if ( empty($conditions) ) {
$sql = str_replace('{conditions}','',$sql);
} else {
$sql = str_replace('{conditions}', $conditions, $sql);
}
$this->_conditions = $conditions;
$this->_sql = $sql;
$rows = $DbConnection->getAllRows($sql);
$this->setRows($rows);
}
public function setFormat($field, $function)
{
if( function_exists($function) && is_array($this->_rows) ) {
for($i=0; $i<count($this->_rows); $i++)
{
$this->_rows[$i][$field]=$function($this->_rows[$i][$field]);
}
}
}
/**
* Display the selected template with the given data and customization
* @return void
*/
public function display() {
$this->assign('__rows' , $this->_rows);
$this->assign('__fields' , $this->_fields);
$this->assign('__links' , $this->_links);
$this->assign('__actions' , $this->_actions);
$this->assign('__classes' , $this->_classes);
$this->assign('__tooltips' , $this->_tooltips);
$this->assign('__prefix' , $this->_prefix);
$this->assign('__row_id' , $this->_row_id);
$this->assign('__filters' , $this->_filters);
$this->assign('__general_actions' , $this->_general_actions);
$this->assign('__order_by', $this->_order_by);
$this->assign('__selected_order_by', $this->_selected_order_field);
$this->assign('__selected_order', $this->_selected_order);
parent::display();
}
public function getConditions() {
return $this->_conditions;
}
public function getQuery(){
return $this->_sql;
}
/**
* 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') {
$DbConnection = DbConnection::getInstance();
$file_name = TO_ROOT."/configs/models/{$config_name}_list.ini";
if(!file_exists($file_name)){
Logger::log("Couldn't find config file", $file_name, LOGGER_ERROR);
return false;
}
$config = parse_ini_file($file_name, true);
if( isset($config['__general']['page_name']) ) {
$this->setPageName($config['__general']['page_name']);
}
unset($config['__general']);
if( isset($config['__query']['sql']) ) {
$paginate = (isset($config['__query']['paginate']))?$config['__query']['paginate']:false;
$this->setQuery($config['__query']['sql'], DbConnection::getInstance(), $paginate);
}
if( isset($config['__query']['order_by']) ) {
$this->orderBy($config['__query']['order_by']);
}
unset($config['__query']);
if( isset($config['__pattern']) ) {
foreach($config['__pattern'] AS $field=>$value) {
$this->setPatternVariable($field, $value);
}
}
unset($config['__pattern']);
if( isset($config['__commands']['hide']) ) {
foreach($config['__commands']['hide'] AS $hide) {
$this->hideField($hide);
}
}
unset($config['__commands']);
foreach($config AS $field => $properties){
if ( strpos($field, ':')!==false ) {
list($field, $action) = explode(':', $field);
if ( $field=='__generalAction' ) {
$this->AddGeneralAction($properties['action'], $properties['title'], $properties['field'], $properties['value'], $properties['icon'], $properties['ajax']);
} else {
if($action == 'action') {
$this->addAction($field, $properties['action'], $properties['title'], $properties['icon'], $properties['ajax']);
} else if($action == 'link') {
$this->addLink($field, $properties['value'], $properties['action']);
}
}
} else {
foreach($properties AS $property => $value) {
if ($property=='format') {
$this->setFormat($field, $value);
} else if($property=='name') {
$this->setName($field, $value);
} else if($property=='class') {
$this->setClass($field, $value);
}
}
}
}
return true;
}
public function orderBy($fields) {
if ( !is_array($fields) ) {
throw new InvalidArgumentException('Fields are supposed to be an array');
}
$this->_order_by = $fields;
}
}
?>