|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Handles belonsto associations for creator |
|
5
|
|
|
* |
|
6
|
|
|
* @package Jam |
|
7
|
|
|
* @category Behavior |
|
8
|
|
|
* @author Ivan Kerin |
|
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
|
10
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
|
11
|
|
|
*/ |
|
12
|
|
|
class Kohana_Jam_Association_Creator extends Jam_Association_Belongsto { |
|
13
|
|
|
|
|
14
|
|
|
public $foreign_model = 'user'; |
|
15
|
|
|
public $required = TRUE; |
|
16
|
|
|
|
|
17
|
|
|
static public $current_user = NULL; |
|
18
|
|
|
|
|
19
|
|
|
static public function current($creator = NULL) |
|
20
|
|
|
{ |
|
21
|
|
|
if ($creator !== NULL) |
|
22
|
|
|
{ |
|
23
|
|
|
Jam_Association_Creator::$current_user = is_object($creator) ? $creator->id() : (int) $creator; |
|
24
|
|
|
} |
|
25
|
|
|
return Jam_Association_Creator::$current_user; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function initialize(Jam_Meta $meta, $name) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::initialize($meta, $name); |
|
31
|
|
|
|
|
32
|
|
|
if ($this->required) |
|
33
|
|
|
{ |
|
34
|
|
|
$meta->validator($name, array('present' => TRUE)); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function get(Jam_Validated $model, $value, $is_changed) |
|
39
|
|
|
{ |
|
40
|
|
|
if ( ! $is_changed AND ! $value AND ! $model->loaded() AND Jam_Association_Creator::current()) |
|
41
|
|
|
{ |
|
42
|
|
|
$value = Jam::find($this->foreign_model, Jam_Association_Creator::current()); |
|
43
|
|
|
$is_changed = TRUE; |
|
44
|
|
|
if ($this->inverse_of) |
|
45
|
|
|
{ |
|
46
|
|
|
$value->{$this->inverse_of} = $model; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return parent::get($model, $value, $is_changed); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function model_before_check(Jam_Validated $model, $value, $changed) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if ( ! $model->loaded() AND ! isset($changed[$this->name]) AND Jam_Association_Creator::current()) |
|
56
|
|
|
{ |
|
57
|
|
|
$model->{$this->name} = Jam::find($this->foreign_model, Jam_Association_Creator::current()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} // End Jam_Association_Creator |
|
62
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.