|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct access allowed.'); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Add promo_code assocition and promo_code_text field with associated validation |
|
5
|
|
|
* |
|
6
|
|
|
* When you assign promo_code_text then it tries to find the appropriate promo_code and assign it. |
|
7
|
|
|
* When you assign promo_code object, its code can be retrieved from promo_code_text (two way binding) |
|
8
|
|
|
* |
|
9
|
|
|
* @package openbuildings\promotions |
|
10
|
|
|
* @author Ivan Kerin <[email protected]> |
|
11
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
|
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
|
13
|
|
|
*/ |
|
14
|
|
|
class Kohana_Jam_Behavior_Promotable_Purchase extends Jam_Behavior { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @codeCoverageIgnore |
|
18
|
|
|
*/ |
|
19
|
|
|
public function initialize(Jam_Meta $meta, $name) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::initialize($meta, $name); |
|
22
|
|
|
|
|
23
|
|
|
$meta |
|
24
|
|
|
->fields(array( |
|
25
|
|
|
'promo_code_text' => Jam::field('string', array('in_db' => FALSE)), |
|
26
|
|
|
)) |
|
27
|
|
|
->associations(array( |
|
28
|
|
|
'promo_code' => Jam::association('belongsto', array('inverse_of' => 'purchases')), |
|
29
|
|
|
)) |
|
30
|
|
|
->validator('promo_code_text', array('purchase_promocode' => TRUE)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* If there is a promo_code object, load it into the promo_code_text |
|
35
|
|
|
* @param Model_Purchase $purchase |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function model_after_load(Model_Purchase $purchase) |
|
38
|
|
|
{ |
|
39
|
1 |
|
if ($purchase->promo_code_id) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$promo_code = Jam_Behavior_Paranoid::with_filter( |
|
42
|
1 |
|
Jam_Behavior_Paranoid::ALL, |
|
43
|
|
|
function () use ($purchase) { |
|
44
|
1 |
|
return $purchase->promo_code; |
|
45
|
1 |
|
} |
|
46
|
|
|
); |
|
47
|
1 |
|
$purchase->retrieved('promo_code_text', $promo_code->code); |
|
48
|
|
|
} |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* If there is a new value in promo_code_text, try to load promo_code object. |
|
53
|
|
|
* If the new value is NULL, remove it |
|
54
|
|
|
* @param Model_Purchase $purchase |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function model_after_check(Model_Purchase $purchase) |
|
57
|
|
|
{ |
|
58
|
1 |
|
if ($purchase->changed('promo_code_text') AND ! $purchase->errors('promo_code_text')) |
|
59
|
|
|
{ |
|
60
|
1 |
|
if ($purchase->promo_code_text) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$purchase->promo_code = Jam::find('promo_code', $purchase->promo_code_text); |
|
63
|
|
|
} |
|
64
|
|
|
else |
|
65
|
|
|
{ |
|
66
|
1 |
|
$purchase->promo_code = NULL; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
1 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|