/
app_model.php
174 lines (141 loc) · 5.14 KB
/
app_model.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
<?php
/**
* Comment Template.
*
* @todo -c Implement .this needs to be sorted out.
*
* Copyright (c) 2009 Carl Sutton ( dogmatic69 )
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2009 Carl Sutton ( dogmatic69 )
* @link http://www.dogmatic.co.za
* @package sort
* @subpackage sort.comments
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.5a
*/
class AppModel extends Model
{
var $useDbConfig = 'default';
var $actsAs = array(
'Containable',
// 'Core.Logable' some wierd issues
);
function lock( $fields = null, $id = null )
{
$old_recursive = $this->recursive;
$this->recursive = -1;
$data = parent::read( array( 'locked', 'locked_by', 'id' ), $id );
$this->Session = new CakeSession();
if ( $data[$this->name]['locked'] && $data[$this->name]['locked_by'] != $this->Session->read( 'Auth.User.id' ) )
{
return false;
}
$data[$this->name]['locked'] = 1;
$data[$this->name]['locked_by'] = $this->Session->read( 'Auth.User.id' );
$data[$this->name]['locked_since'] = date( 'Y-m-d H:i:s' );
parent::save( $data, array( 'validation' => false ) );
$this->recursive = $old_recursive;
$data = $this->read( $fields, $id );
return $data;
}
/**
* For unlocking records.
*
* sets the lock to false and sets the date and person to null
*
* @param array $data
* @param array $options
* @return {@see Model::save}
*/
function save( $data = null, $validate = true, $fieldList = array() )
{
$data[$this->name]['locked'] = 0;
$data[$this->name]['locked_by'] = null;
$data[$this->name]['locked_since'] = null;
return parent::save( $data, $validate, $fieldList );
}
function find( $conditions = null, $fields = array(), $order = null, $recursive = null )
{
switch( $conditions )
{
case 'feed':
return $this->__feedableFind( $this, $fields );
break;
default:
return parent::find( $conditions, $fields, $order, $recursive );
} // switch
}
function __feedableFind(&$Model, $query)
{
if ( !isset( $query['feed'] ) )
{
return $query;
}
$sql = 'SELECT ';
if ( isset( $query['setup'] ) )
{
$fields = array();
foreach( $query['setup'] as $name => $value )
{
$fields[] = "'$value' AS $name";
}
$sql .= implode( ', ', $fields );
}
if ( isset( $query['fields'] ) )
{
$_fields = array();
foreach( $query['fields'] as $field )
{
$__fields = explode( '.', $field );
$_fields[] = $__fields[1];
}
$sql .= ', '.implode( ', ', $_fields );
}
$sql .= ' FROM '.$Model->tablePrefix.$Model->useTable;
if ( isset( $query['feed'] ) )
{
foreach( $query['feed'] as $key => $feed )
{
$sql .= ' UNION SELECT ';
if ( isset( $feed['setup'] ) )
{
$fields = array();
foreach( $feed['setup'] as $name => $value )
{
$fields[] = "'$value' AS $name";
}
$sql .= implode( ', ', $fields );
}
if ( isset( $feed['fields'] ) )
{
$_fields = array();
foreach( $feed['fields'] as $field )
{
$__fields = explode( '.', $field );
$_fields[] = $__fields[1];
}
$sql .= ', '.implode( ', ', $_fields );
}
$__key = explode( '.', $key );
$__key[0] = strtolower( $__key[0] );
$__key[1] = strtolower( Inflector::pluralize( $__key[1] ) );
$sql .= ' FROM '.implode( '_', $__key );
}
}
if ( isset( $query['order'] ) )
{
$ordering = array();
foreach( $query['order'] as $key => $value )
{
$ordering[] = $key.' '.$value;
}
$sql .= ' ORDER BY '.implode( ', ', $ordering );
}
return $this->query( $sql );
}
}
?>