-
Notifications
You must be signed in to change notification settings - Fork 1
/
Purchase.php
70 lines (65 loc) · 1.95 KB
/
Purchase.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
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Add promo_code assocition and promo_code_text field with associated validation
*
* When you assign promo_code_text then it tries to find the appropriate promo_code and assign it.
* When you assign promo_code object, its code can be retrieved from promo_code_text (two way binding)
*
* @package openbuildings\promotions
* @author Ivan Kerin <ikerin@gmail.com>
* @copyright (c) 2013 OpenBuildings Ltd.
* @license http://spdx.org/licenses/BSD-3-Clause
*/
class Kohana_Jam_Behavior_Promotable_Purchase extends Jam_Behavior {
/**
* @codeCoverageIgnore
*/
public function initialize(Jam_Meta $meta, $name)
{
parent::initialize($meta, $name);
$meta
->fields(array(
'promo_code_text' => Jam::field('string', array('in_db' => FALSE)),
))
->associations(array(
'promo_code' => Jam::association('belongsto', array('inverse_of' => 'purchases')),
))
->validator('promo_code_text', array('purchase_promocode' => TRUE));
}
/**
* If there is a promo_code object, load it into the promo_code_text
* @param Model_Purchase $purchase
*/
public function model_after_load(Model_Purchase $purchase)
{
if ($purchase->promo_code_id)
{
$promo_code = Jam_Behavior_Paranoid::with_filter(
Jam_Behavior_Paranoid::ALL,
function () use ($purchase) {
return $purchase->promo_code;
}
);
$purchase->retrieved('promo_code_text', $promo_code->code);
}
}
/**
* If there is a new value in promo_code_text, try to load promo_code object.
* If the new value is NULL, remove it
* @param Model_Purchase $purchase
*/
public function model_after_check(Model_Purchase $purchase)
{
if ($purchase->changed('promo_code_text') AND ! $purchase->errors('promo_code_text'))
{
if ($purchase->promo_code_text)
{
$purchase->promo_code = Jam::find('promo_code', $purchase->promo_code_text);
}
else
{
$purchase->promo_code = NULL;
}
}
}
}