/
Abstract.php
651 lines (564 loc) · 18.2 KB
/
Abstract.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<?php
/**
* MagPleasure Ltd.
*
* NOTICE OF LICENSE
*
* This source file is subject to the EULA
* that is bundled with this package in the file LICENSE-CE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magpleasure.com/LICENSE-CE.txt
*
* =================================================================
* MAGENTO EDITION USAGE NOTICE
* =================================================================
* This package designed for Magento COMMUNITY edition
* MagPleasure does not guarantee correct work of this extension
* on any other Magento edition except Magento COMMUNITY edition.
* Magpleasure does not provide extension support in case of
* incorrect edition usage.
* =================================================================
*
* @category MagPleasure
* @package Magpleasure_Common
* @version 0.8.1
* @copyright Copyright (c) 2012-2015 MagPleasure Ltd. (http://www.magpleasure.com)
* @license http://www.magpleasure.com/LICENSE-CE.txt
*/
/**
* Abstract Resource Model
*/
class Magpleasure_Common_Model_Resource_Abstract extends Mage_Core_Model_Mysql4_Abstract
{
const LINK_DATA_KEY = 'data_key';
const LINK_DB_POSTFIX = 'db_postfix';
const LINK_DB_DATA_KEY = 'db_data_key';
protected $_useStoreLabels = false;
protected $_useUpdateDatetimeHelper = false;
protected $_useHashHelper = false;
protected $_hashHelperChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
protected $_hashHelperLength = 160;
protected $_links = array();
/**
* @var array List of file fields
*/
protected $_fileFields = array();
/**
* @var array List of Serializable Fields
*/
protected $_serializable = array();
protected $_imageFields = array();
protected $_secureFields = array();
protected function _construct(){}
/**
* Helper
*
* @return Magpleasure_Common_Helper_Data
*/
protected function _commonHelper()
{
return Mage::helper('magpleasure');
}
/**
* Register field to be automatically serialized
*
* @param $fieldName
* @return $this
*/
protected function _addSerializableField($fieldName)
{
$this->_serializable[] = $fieldName;
return $this;
}
/**
* Register field to be saved and loaded securly
*
* @param string $fieldName
* @return $this
*/
protected function _addSecureField($fieldName)
{
$this->_secureFields[] = $fieldName;
return $this;
}
/**
* Register field to be automatically processed as image
*
* @param $fieldName
* @return $this
*/
protected function _addImageField($fieldName)
{
$this->_imageFields[] = $fieldName;
return $this;
}
protected function _addFileFields($fieldName)
{
$this->_fileFields[] = $fieldName;
return $this;
}
/**
* Perform actions after object load
*
* @param Mage_Core_Model_Abstract $object
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _afterLoad(Mage_Core_Model_Abstract $object)
{
parent::_afterLoad($object);
# Load Store Labels
if ($this->getUseStoreLabels()){
$labels = $this->_loadLabels($object->getId());
foreach ($labels as $storeId => $label){
$object->setData("store_label_".$storeId, $label);
}
}
# Load Linked Data
if (count($this->_links)){
foreach ($this->_links as $key=>$params){
$this->_loadAbstractLink($object, @$params[self::LINK_DATA_KEY], @$params[self::LINK_DB_POSTFIX], @$params[self::LINK_DB_DATA_KEY]);
}
}
# Unserialize Serializable Fields
foreach ($this->_serializable as $fieldName){
if ($object->hasData($fieldName)){
try {
$object->setData($fieldName, unserialize($object->getData($fieldName)));
} catch (Exception $e){
$this->_commonHelper()->getException()->logException($e);
}
}
}
# Open secured fields
$this->_decodeSecureFields($object);
return $this;
}
/**
* Get Use Store Labels
*
* @return bool
*/
public function getUseStoreLabels()
{
return $this->_useStoreLabels;
}
/**
* Set Use Store Labels
*
* @param $value
* @return Magpleasure_Common_Model_Resource_Abstract
*/
public function setUseStoreLabels($value)
{
$this->_useStoreLabels = $value;
return $this;
}
/**
* Set Use Update DateTime Helper
*
* @param $value
* @return Magpleasure_Common_Model_Resource_Abstract
*/
public function setUseUpdateDatetimeHelper($value)
{
$this->_useUpdateDatetimeHelper = $value;
return $this;
}
/**
* Generate Unique Hash for some field
*
* @param $useHashHelper Field generate unique hash for
* @param null $hashChars
* @param null $hashLength
* @return $this
*/
public function setUseHashHelper($useHashHelper, $hashChars = null, $hashLength = null)
{
$this->_useHashHelper = $useHashHelper;
if ($hashChars){
$this->_hashHelperChars = $hashChars;
}
if ($hashLength){
$this->_hashHelperLength = $hashLength;
}
return $this;
}
/**
* @return boolean
*/
public function getUseHashHelper()
{
return $this->_useHashHelper;
}
/**
* Get Use Update DateTime Helper
*
* @return bool
*/
public function getUseUpdateDatetimeFilter()
{
return $this->_useUpdateDatetimeHelper;
}
/**
* Perform actions after object save
*
* @param Mage_Core_Model_Abstract $object
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
parent::_afterSave($object);
# Save Store Labels
if ($this->getUseStoreLabels()){
$this->_saveLabels($object->getId(), $object->getStoreLabels());
}
# Save Linked Data
if (count($this->_links)){
foreach ($this->_links as $key=>$params){
$this->_saveAbstractLink($object, @$params[self::LINK_DATA_KEY], @$params[self::LINK_DB_POSTFIX], @$params[self::LINK_DB_DATA_KEY]);
}
}
return $this;
}
protected function _generateUniqueHash()
{
$randStr = "";
$strLenght = strlen($this->_hashHelperChars);
for ($i = 1;$i <= $this->_hashHelperLength; $i++){
$chNum = rand(0, $strLenght - 1);
$randStr .= $this->_hashHelperChars[$chNum];
}
return $randStr;
}
/**
* Perform actions before object save
*
* @param Mage_Core_Model_Abstract $object
* @return Mage_Core_Model_Resource_Db_Abstract|Magpleasure_Common_Model_Resource_Abstract
*/
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
# Update Datetime Proceed
if ($this->getUseUpdateDatetimeFilter()){
$now = new Zend_Date();
$now = $now->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
if (!$object->getId()){
$object->setCreatedAt($now);
}
$object->setUpdatedAt($now);
}
# Serialize Serializable Fields
foreach ($this->_serializable as $fieldName){
if ($object->hasData($fieldName)){
try {
$object->setData($fieldName, serialize($object->getData($fieldName)));
} catch (Exception $e){
$this->_commonHelper()->getException()->logException($e);
}
}
}
# Image Fields
foreach ($this->_imageFields as $fieldName){
if ($object->hasData($fieldName."_delete")){
$toDelete = $object->getData($fieldName."_delete");
if ($toDelete && is_array($toDelete) && count($toDelete)){
foreach ($toDelete as $fileName){
try {
$fileName = str_replace("/", DS, $fieldName);
$fullPath = Mage::getBaseDir('media').$fieldName;
if (file_exists($fullPath)){
unlink($fullPath);
}
} catch (Exception $e){
$this->_commonHelper()->getException()->logException($e);
}
}
}
}
}
# Generate Unique HASH if need
if ($this->getUseHashHelper() && !$object->getId()){
$object->setData($this->getUseHashHelper(), $this->_generateUniqueHash());
}
# Protect secured fields
$this->_encodeSecureFields($object);
parent::_beforeSave($object);
return $this;
}
/**
* Save Labels to Table <main_table>_label
*
* @param int $entityId
* @param array $labels
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _saveLabels($entityId, array $labels = null)
{
if (!$labels || !is_array($labels)){
return $this;
}
$labelTable = $this->getMainTable()."_label";
$keyField = $this->getIdFieldName();
# Delete Old Labels
$deleted = $this
->_getWriteAdapter()
->beginTransaction()
->delete($labelTable, "{$keyField} = '{$entityId}'")
;
# Insert new ones
$data = array();
foreach ($labels as $storeId => $label) {
if (empty($label)) {
continue;
}
$data[] = array(
$keyField => $entityId,
'store_id' => $storeId,
'label' => $label
);
}
try {
if (!empty($data)) {
foreach ($data as $row){
$this->_getWriteAdapter()->insert($labelTable, $row);
}
}
} catch (Exception $e){
$this
->_commonHelper()
->getException()
->logException($e)
;
}
$this->_getWriteAdapter()->commit();
return $this;
}
/**
* Load Labels from Table <main_table>_label
*
* @param int $entityId
* @return array
*/
protected function _loadLabels($entityId)
{
$labelsData = array();
$labelTable = $this->getMainTable()."_label";
$keyField = $this->getIdFieldName();
$select = $this->_getReadAdapter()->select();
$select
->from($labelTable, array('store_id', 'label'))
->where("{$keyField} = ?", $entityId)
;
try {
foreach ($this->_getReadAdapter()->fetchAll($select) as $row){
$labelsData[$row['store_id']] = $row['label'];
}
} catch (Exception $e){
$this
->_commonHelper()
->getException()
->logException($e)
;
}
return $labelsData;
}
/**
* Add Abstract Data Link
*
* @param $name
* @param $dataKey
* @param $dbPostfix
* @param $dbDataKey
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _addDataLink($name, $dataKey, $dbPostfix, $dbDataKey)
{
$this->_links[$name] = array(
self::LINK_DATA_KEY => $dataKey,
self::LINK_DB_POSTFIX => $dbPostfix,
self::LINK_DB_DATA_KEY => $dbDataKey,
);
return $this;
}
/**
* Remove Abstract Data Link
*
* @param $name
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _removeDataLink($name)
{
if (isset($this->_links[$name])){
unset($this->_links[$name]);
}
return $this;
}
/**
* Save Linked Abstract Data
*
* @param Mage_Core_Model_Abstract $object
* @param string $dataKey
* @param string $dbPostfix
* @param string $dbDataKey
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _saveAbstractLink(Mage_Core_Model_Abstract $object, $dataKey, $dbPostfix, $dbDataKey)
{
if (is_array($object->getData($dataKey))){
$storeTable = $this->getMainTable().$dbPostfix;
$write = $this->_getWriteAdapter();
$write->beginTransaction();
$write->delete($storeTable, "`{$this->getIdFieldName()}` = '{$object->getId()}'");
if (is_array($object->getData($dataKey))){
foreach ($object->getData($dataKey) as $linkValue){
$write->insert($storeTable, array(
$this->getIdFieldName() => $object->getId(),
$this->_getLocalDataKey($dbDataKey) => $linkValue,
));
}
}
$write->commit();
}
return $this;
}
protected function _getLocalDataKey($value)
{
if (is_array($value)){
foreach ($value as $local=>$remote){
return $local;
}
} else {
return $value;
}
return false;
}
protected function _getRemoteDataKey($value)
{
if (is_array($value)){
foreach ($value as $local=>$remote){
return $remote;
}
} else {
return $value;
}
return false;
}
protected function _loadDirectLink(Mage_Core_Model_Abstract $object, $dataKey, $tableName, $dbDataKey)
{
if ($object->getData($this->getIdFieldName())){
$read = $this->_getReadAdapter();
$select = new Zend_Db_Select($read);
$select
->from($tableName, array($this->_getLocalDataKey($dbDataKey)))
->where($this->getIdFieldName()." = ?", $object->getId());
;
$result = array();
foreach ($read->fetchAll($select) as $row){
$result[] = $row[$this->_getLocalDataKey($dbDataKey)];
}
$object->setData($dataKey, $result);
$object->setOrigData($dataKey, $result);
}
return $this;
}
/**
* Load Linked Abstract Data
*
* @param Mage_Core_Model_Abstract $object
* @param string $dataKey
* @param string $dbPostfix
* @param string $dbDataKey
* @return Magpleasure_Common_Model_Resource_Abstract
*/
protected function _loadAbstractLink(Mage_Core_Model_Abstract $object, $dataKey, $dbPostfix, $dbDataKey)
{
$tableName = $this->getMainTable().$dbPostfix;
$this->_loadDirectLink($object, $dataKey, $tableName, $dbDataKey);
return $this;
}
/**
* Write Adapter
*
* @return Varien_Db_Adapter_Interface
*/
public function getWriteAdapter()
{
return $this->_getWriteAdapter();
}
/**
* Get Read Adapter
*
* @return Varien_Db_Adapter_Interface
*/
public function getReadAdapter()
{
return $this->_getReadAdapter();
}
/**
* Delete all linked records
*
* @param $fieldName
* @param $fieldValue
* @return Magpleasure_Common_Model_Resource_Abstract
*/
public function deleteRowsByLink($fieldName, $fieldValue)
{
$mainTable = $this->getMainTable();
if ($mainTable){
$write = $this->getWriteAdapter();
$write
->beginTransaction()
->delete($mainTable, "`{$fieldName}` = '{$fieldValue}'")
;
$write->commit();
}
return $this;
}
/**
* Load Absctract Collection by few key fields
*
* @param Mage_Core_Model_Abstract $object
* @param array $data
* @return Magpleasure_Common_Model_Resource_Abstract
*/
public function loadByFewFields(Mage_Core_Model_Abstract $object, array $data)
{
/** @var $collection Mage_Core_Model_Resource_Db_Collection_Abstract */
$collection = $object->getCollection();
if ($collection){
foreach ($data as $field=>$value){
$collection->addFieldToFilter($field, $value);
}
foreach ($collection as $item){
/** @var $item Mage_Core_Model_Abstract */
if ($itemId = $item->getId()){
$itemModel = Mage::getModel($object->getResourceName())->load($itemId);
$object->setData($itemModel->getData());
}
}
}
return $this;
}
protected function _encodeSecureFields(Mage_Core_Model_Abstract $object)
{
foreach ($this->_secureFields as $field) {
if ($object->hasData($field)){
$value = $object->getData($field);
$value = $this->_commonHelper()->getCore()->encrypt($value);
$object->setData($field, $value);
}
}
return $this;
}
protected function _decodeSecureFields(Mage_Core_Model_Abstract $object)
{
foreach ($this->_secureFields as $field) {
if ($object->hasData($field)){
$value = $object->getData($field);
$value = $this->_commonHelper()->getCore()->decrypt($value);
$object->setData($field, $value);
}
}
return $this;
}
}