My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
<?php
/**
* TraceableBehavior
*
* Allows the model to have traces from the user that created or modified them.
*/
class TraceableBehavior extends ModelBehavior {

/**
*
* @var array Contain settings indexed by model name.
* @access private
*/
var $__settings = array();

/**
*
* @var array Contains values to be saved in their corresponding field names.
* @access private
*/
var $__fields = array();

/**
*
* @var string the handle of the user to save
*/
var $__handle = '';

/**
* setup
*
* Initiate behavior for the model using specified settings. Available settings:
*
* - create: (boolean, optional) make the model also store the user that
* created the entry.
* DEFAULTS TO: true
*
* - created: (string, optional) name of the field that will hold the id of
* the user that created the entry.
* DEFAULTS TO: created_by
*
* - modified: (string, optional) name of the field that will hold the id of
* the user that modified the entry.
* DEFAULTS TO: modified_by
*
* @param object $Model Model using the behaviour
* @param array $config Settings to override for model
*/
function setup(&$Model, $settings = array()){
$default = array('create' => false, 'created' => 'created_by', 'modified' => 'modified_by');
if (!isset($this->__settings[$Model->alias])){
$this->__settings[$Model->alias] = $default;
}
$this->__settings[$Model->alias] = am($this->__settings[$Model->alias], ife(is_array($settings), $settings, array()));
}

/**
* trace
*
* Main function of traceable. Embeds the current authenticated (Auth)
* user to a specified field.
*
* @param object $model The Model using the behaviour
* @param string $handle The name of the authenticated user.
*/
function trace(&$Model, $handle){
$save = array();
if ($this->__settings[$Model->alias]['create'])
$fields['created'] = $this->__settings[$Model->alias]['created'];
$fields['modified'] = $this->__settings[$Model->alias]['modified'];
if (!empty($handle)){
$this->__handle = $handle;
foreach ($fields as $type => $fieldName){
if (!$Model->hasField($fieldName)) {
unset($fieldName);
}else{
$fieldName = $this->__settings[$Model->alias][$type];
$this->__fields[] = $fieldName;
}
}
}
}

/**
* beforeSave
*
* @param object $Model The model to save
* @return object Model with with new data to save
*/
function beforeSave(&$Model){
$return = parent::beforeSave($Model);
if (isset($this->__settings[$Model->alias])){
foreach($this->__fields as $field){
switch ($field){
case $this->__settings[$Model->alias]['created']:
if (empty($Model->data[$Model->alias][$field])){
$Model->data[$Model->alias][$field] = $this->__handle;
}
break;
case $this->__settings[$Model->alias]['modified']:
$Model->data[$Model->alias][$field] = $this->__handle;
break;
}
}
return $return;
}
}
}
?>

Change log

r213 by sonnygauran on Feb 8, 2009   Diff
// Merged changes from
/branches/sonny/Release-0.6/chieftain@212
to
/trunk/chieftain
Go to: 
Sign in to write a code review

Older revisions

r188 by sonnygauran on Feb 2, 2009   Diff
ps snv:eol-style native
r102 by sonnygauran on Jan 13, 2009   Diff
Moved chieftain inside trunk.
r101 by sonnygauran on Jan 13, 2009   Diff
created directory chieftain to be
moved inside trunk
created directory cake-1.2-final as an
export of cakephp svn 7962
All revisions of this file

File info

Size: 3602 bytes, 109 lines

File properties

svn:eol-style
native
svn:keywords
Date Author Rev URL Id
Powered by Google Project Hosting