/
field.incremental_number.php
executable file
·156 lines (115 loc) · 4.33 KB
/
field.incremental_number.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
<?php
Class field_incremental_number extends Field{
function __construct(&$parent){
parent::__construct($parent);
$this->_name = 'Incremental Number';
$this->_required = true;
$this->set('required', 'yes');
}
function isSortable()
{
return true;
}
function canFilter()
{
return true;
}
function allowDatasourceParamOutput()
{
return true;
}
function canPrePopulate()
{
return true;
}
public function displaySettingsPanel(&$wrapper, $errors=NULL)
{
parent::displaySettingsPanel($wrapper, $errors);
$label = Widget::Label(__('Start Number'));
$label->appendChild(WIDGET::Input('fields['.$this->get('sortorder').'][start_number]', $this->get('start_number')));
$wrapper->appendChild($label);
$this->appendShowColumnCheckbox($wrapper);
}
public function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL)
{
$value = $data['value'];
$label = Widget::Label($this->get('label'));
//var_dump($data);
//var_dump($this->get('start_number'));die;
$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : $this->getNewNumber()), null, array('readonly' => 'readonly')));
if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
else $wrapper->appendChild($label);
}
public function getNewNumber()
{
$last_num = Symphony::Database()->fetch("
SELECT `value`
FROM `tbl_entries_data_".$this->get('id')."`
ORDER BY `value` DESC LIMIT 1
");
//var_dump($last_num[0]);die;
if(!empty($last_num)){
return (int)$last_num[0]['value']+1;
}else{
return (int)$this->get('start_number');
}
}
public function commit()
{
if(!parent::commit()) return false;
$id = $this->get('id');
$value = $this->get('start_number');
if($id === false) return false;
$fields = array();
$fields['field_id'] = $id;
$fields['start_number'] = $value;
Symphony::Database()->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1");
return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());
}
public function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL)
{
$wrapper->appendChild(new XMLElement('h4', $this->get('label') . ' <i>'.$this->Name().'</i>'));
$label = Widget::Label('Value');
$label->appendChild(Widget::Input('fields[filter]'.($fieldnamePrefix ? '['.$fieldnamePrefix.']' : '').'['.$this->get('id').']'.($fieldnamePostfix ? '['.$fieldnamePostfix.']' : ''), ($data ? General::sanitize($data) : NULL)));
$wrapper->appendChild($label);
$wrapper->appendChild(new XMLElement('p', 'To filter by ranges, add <code>mysql:</code> to the beginning of the filter input. Use <code>value</code> for field name. E.G. <code>mysql: value >= 1.01 AND value <= {$price}</code>', array('class' => 'help')));
}
public function checkPostFieldData($data, &$message, $entry_id=NULL)
{
$message = NULL;
if($this->get('required') == 'yes' && strlen($data) == 0){
$message = 'This is a required field.';
return self::__MISSING_FIELDS__;
}
if(strlen($data) > 0 && !is_numeric($data)){
$message = 'Must be a number.';
return self::__INVALID_FIELDS__;
}
return self::__OK__;
}
public function createTable()
{
return Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` int(11) unsigned NOT NULL auto_increment,
`entry_id` int(11) unsigned NOT NULL,
`value` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`),
KEY `value` (`value`)
) TYPE=MyISAM;"
);
}
function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation=false)
{
if(preg_match('/^mysql:/i', $data[0])){
$field_id = $this->get('id');
$expression = str_replace(array('mysql:', 'value'), array('', " `t$field_id`.`value` " ), $data[0]);
$joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id` ON (`e`.`id` = `t$field_id`.entry_id) ";
$where .= " AND $expression ";
}
else parent::buildDSRetrivalSQL($data, $joins, $where, $andOperation);
return true;
}
}
?>