Block class to agregate html blocks of code with php vars and compile it for Mailbot Project

  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
<?php
/**
* Block class to agregate html blocks of code with php vars and compile it
* Note: PHP4 compatible
*
* @name Mailbot_Block
* @author Sasha Molodtsov <asid@mail.ru>
* @since 27.04.2009
* @package Mailbot
*/
class Mailbot_Block
{
var $_id = null;
var $_dataKeys = array('name','summary','body','update_ts');
var $_dataContiue = array(
'update'=>array('update_ts'),
'create'=>array('update_ts')
);
var $_isValid = false;
var $_data = array();
var $_db = null;
var $_Compiller = null;
var $_childs = array();
var $_childsRecursive = array();
var $_registeredVars = array();
var $_varsByBlock = array();
var $_varsById = array();
var $_errors = array();
/**
* Contstructor
*
* @param int $id
* @param Adapter_Db_Abstract $db
* @return Mailbot_Block
*/
function Mailbot_Block($id = 0,$db = null)
{
require_once('system/Mailbot/Adapter/Compiller/Smarty.php');
$this->_Compiller = new Adapter_Compiller_Smarty();
$this->setDb($db);
if ((int)$id)
{
$this->load((int)$id);
}
}
/**
* Load block
*
* @param int $key - block_id
* @return array
*/
function load($key)
{
$sql = "SELECT id, ".implode(', ', $this->_dataKeys)." FROM mailbot_block WHERE ";
$db = $this->getDb();
if(is_numeric($key))
{
$sql .= " id = ".(int)$key;
}else{
$sql .= " name = '".$db->quote($key)."'";
}
$sql .= " LIMIT 1";
$data = $db->fetch($sql);
if(is_array($data))
{
$this->_id = $data['id'];
unset($data['id']);
$this->_data = $data;
return $data;
}
return false;
}
/**
* Returns block data
*
* @return array
*/
function getData()
{
return $this->_data;
}
/**
* Delete Block
*
* @return bool
*/
function delete()
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$sql = "DELETE FROM mailbot_block WHERE id = ".(int)$this->_id;
$db = $this->getDb();
$db->query($sql);
return true;
}
/**
* Update block
*
* @param array $array - array of data to update block
* @return bool
*/
function update($array)
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$str = array();
$db = $this->getDb();
foreach ($array as $key=>$val)
{
if(in_array($key,$this->_dataContiue['update'])) continue;
$str[] = $key.' = "'.$db->quote($val).'"';
}
$sql = "UPDATE mailbot_block SET ".implode(' , ', $str)." WHERE id = ".(int)$this->_id;
$db->query($sql);
if($db->affected_rows() > 0)
{
$this->load(reset($db->fetch('SELECT LAST_INSERT_ID()')));
return true;
}
return false;
}
/**
* Create block
*
* @param array $array - array of data to create block
* @return bool
*/
function create($array)
{
$str = array();
$db = $this->getDb();
foreach ($array as $key=>$val)
{
if(in_array($key, $this->_dataContiue['create'])) continue;
$str[] = $key.' = "'.$db->quote($val).'"';
}
$sql = "INSERT INTO mailbot_block SET ".implode(' , ', $str);
$db->query($sql);
if($db->affected_rows() > 0)
{
$this->load(reset($db->fetch('SELECT LAST_INSERT_ID()')));
return true;
}
return false;
}
/**
* Get compiller registered vars
*
* @return array
*/
function getRegisteredVars()
{
return $this->_registeredVars;
}
/**
* get errors
*
* @return array
*/
function getErrors()
{
return $this->_errors;
}
/**
* get vars from this block
*
* @param int $id - block_id
* @return array
*/
function getVars($id = false)
{
if($id == false)
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$id = $this->_id;
}
if(!isset($this->_varsByBlock[$id]))
{
$db = $this->getDb();
$sql = "
SELECT
v.id, v.name, v.summary, v.is_mandatory, v.default, bv.block_id
FROM mailbot_variable AS v
INNER JOIN mailbot_block_var AS bv ON bv.variable_id = v.id
WHERE block_id = ".(int)$id."
";
$data = $db->fetch($sql);
if(is_array($data) and !empty($data))
{
$this->_setVarsAssoc(array($data));
}else{
$this->_varsByBlock[$id] = array();
}
}
return $this->_varsByBlock[$id];
}
/**
* get vars from block and his subblocks
*
* @param bool $by_id - is key of vars will be int or string
* @return array
*/
function getAllVars($by_id = false)
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$childs = $this->getChilds();
$childs[] = $this->_id;
$data = array();
foreach ($childs as $block_id)
{
$data = array_merge($data, $this->getVars($block_id, $by_id));
}
if($by_id)
{
return $this->_varsById;
}
return $data;
}
/**
* get subblocks of this block
*
* @param int $id - block_id
* @return array
*/
function getChilds($id = false)
{
if($id == false)
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$id = $this->_id;
if(isset($this->_childsRecursive[$id]) && is_array($this->_childsRecursive[$id]))
{
return $this->_childs;
}
}
if(!isset($this->_childsRecursive[$id]))
{
$db = $this->getDb();
$sql = "SELECT include_id FROM mailbot_block_inc WHERE block_id = ".(int)$id;
$data = $db->fetchAll($sql);
if(is_array($data) and !empty($data))
{
foreach ($data as $val)
{
$this->_childsRecursive[$id][] = $val['include_id'];
}
}
}
if(isset($this->_childsRecursive[$id]) && is_array($this->_childsRecursive[$id]))
{
foreach ($this->_childsRecursive[$id] as $val)
{
$this->_childs[] = $val;
$this->getChilds($val);
}
}
natcasesort($this->_childsRecursive);
sort($this->_childs);
return array_unique($this->_childs);
}
/**
* gets database adapter
*
* @return Adapter_Db_Abstract
*/
function getDb()
{
if(is_object($this->_db))
{
return $this->_db;
}else{
die('DB ADAPTER IS NOT SET IN '.__METHOD__);
}
}
/**
* Set the database adapter
*
* @param Adapter_Db_Abstract $db
* @return bool
*/
function setDb(&$db)
{
if(is_object($db))
{
$this->_db = $db;
$this->_Compiller->setDb($db);
return true;
}
return false;
}
/**
* Set vars to compiller with validation
*
* @param array $data - hash with key=>val data
* @param bool $by_id - set is key is int or string
* @return bool
*/
function setVars($data, $by_id = false)
{
foreach ($this->getAllVars($by_id) as $key => $var)
{
// if field is mandatory, it is not set or it's emptry and default field is empty, then set errors mes that filen is not valid
if($var['is_mandatory'] && (!isset($data[$key]) || !strlen($data[$key])) && !strlen($var['default']))
{
if(!isset($data[$key]))
{
$var[errors][] = 'not set and there is no default value';
}elseif(!strlen($data[$key]))
{
$var[errors][] = 'is empty and there is no default value';
}
$this->_errors['lostvars'][] = $var;
$this->_isValid = false;
return $this->_isValid;
}
}
$this->_isValid = true;
foreach ($this->getAllVars($by_id) as $key => $var)
{
$assign_key = $key;
if($by_id)
{
$assign_key = $var['name'];
}
if(isset($data[$key]) && strlen($data[$key]))
{
$this->_Compiller->assign($assign_key, $data[$key]);
$this->_registeredVars[$assign_key] = $data[$key];
}else if(strlen($var['default']))
{
$this->_Compiller->assign($assign_key, $var['default']);
$this->_registeredVars[$assign_key] = $var['default'];
}
}
return $this->_isValid;
}
/**
* set vars to assoc arrays
*
* @access private method
* @param array $data
*/
function _setVarsAssoc($data)
{
foreach ($data as $arr)
{
$this->_varsById[$arr[id]] = $arr;
$this->_varsByBlock[$arr[block_id]][$arr[name]] = $arr;
}
}
/**
* answer if block is valid
*
* @return bool
*/
function isValid()
{
return $this->_isValid;
}
/**
* compile and return the block
*
* @return string
*/
function fetch()
{
if(!$this->_id)
{
$this->_errors[] = 'id is not set in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
if(!$this->isValid())
{
$this->_errors[] = 'block is not valid in '.__FILE__.' :: '.__CLASS__.' :: '.__FUNCTION__.' :: line '.__LINE__; return false;
}
$out = $this->_Compiller->fetch($this->_data['name']);
$this->_Compiller->clearAssigned();
return $out;
}
}