-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_model.php
137 lines (109 loc) · 4.02 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
<?php
class AppModel extends Model {
var $actsAs = array('Containable');
function find($type, $options = array()) {
$method = null;
if (is_string($type)) {
$method = sprintf('__find%s', Inflector::camelize($type));
}
if ($method && method_exists($this, $method)) {
$results = $this->{$method}($options);
} else {
$args = func_get_args();
$results = call_user_func_array(array('parent', 'find'), $args);
}
return $results;
}
/**
* __findRandom()
*
* Find a list of records ordered by rank.
* Instead of executing a __findList() query to get the list of IDs,
* you can pass an array of IDs via the $options['suppliedList']
* argument.
*
* Two queries are executed, first a find('list') to generate a list of primary
* keys, and then either a find('all') or find('first') depending on the return
* amount specified (default 1).
*
* Pass find options to each query using the $options['list'] and $options['find']
* arguments.
*
* Specify $options['amount'] as the maximum number of random items that should
* be returned.
*
* If you already have an array of IDs(/primary keys), you can skip the find('list')
* query by passing the array as $options['suppliedList'].
*
* @access private
* @param $options array of standard and function-specific find options.
* @return array
*/
function __findRandom($options = array()) {
if (!isset($options['amount'])) {
$amount = 1;
} else {
$amount = $options['amount'];
}
$findOptions = array();
if (isset($options['find'])) {
$findOptions = array_merge($findOptions, $options['find']);
}
if (!isset($options['suppliedList'])) {
$listOptions = array();
if (isset($options['list'])) {
$listOptions = array_merge($listOptions, $options['list']);
}
$list = $this->find('list', $listOptions);
} else {
$list = $options['suppliedList'];
$list = array_flip($list);
}
// Just a little failsafe.
if (count($list) < 1) {
return $list;
}
$originalAmount = null;
if ($amount > count($list)) {
$originalAmount = $amount;
$amount = count($list);
}
$id = array_rand($list, $amount);
if (is_array($id)) {
shuffle($id);
}
if (!isset($findOptions['conditions'])) {
$findOptions['conditions'] = array();
}
$findOptions['conditions'][$this->alias.'.'.$this->primaryKey] = $id;
if ($amount == 1 && !$originalAmount) {
return $this->find('first', $findOptions);
} else {
return $this->find('all', $findOptions);
}
}
/**
* Adds the path to the root node to the array passed.
*
* @param Array $array array the path needs to be added to
* @param String $controller name of the controller that the path is contained within.
* @return Array returns $array, but with the path added in.
*/
function addPath($array, $controller) {
// process each item in array to add the path
foreach($array as &$node) {
// get the path to the node.
// $this->$controller->displayField will be equal to the display field for the controller (IE 'name' or 'title')
$path = $this->getpath($node[$controller]['id'], array($this->displayField, 'id'));
// blank out the result array
$result = array();
// build the path array we want
foreach($path as $part) { $result[] = $part[$controller]; }
$node[$controller]['path'] = $result;
// dealing with nested data
if(!empty($node['children'])) { $node['children'] = $this->addPath($node['children'], $controller); }
}
return $array;
}
}
?>