forked from neilcrookes/CakePHP-GData-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdata_app_model.php
185 lines (164 loc) · 6.52 KB
/
gdata_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
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Plugin base model. Configures all models with regards the database
* configuration (the datasource for the plugin), table to use (none), imports
* the datasource, and includes functionality for working with the custom
* pagination requirements by the web service.
*
* @author Neil Crookes <neil@neilcrookes.com>
* @link http://www.neilcrookes.com
* @copyright (c) 2010 Neil Crookes
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*/
class GdataAppModel extends AppModel {
/**
* The models in the plugin get data from the web service, so they don't need
* a table.
*
* @var string
*/
public $useTable = false;
/**
* Methods in the models result in HTTP requests using the HttpSocket. So
* rather than do all the heavy lifting in the datasource, we set the various
* params of the request in the individual model methods. This ties the model
* to the data layer, but these models are especially for this datasource.
*
* @var array
*/
public $request = array();
/**
* Since the webservice call returns the results in the current page of the
* result set and the total number of results in the whole results set, we
* need custom paginate and paginateCount methods, whereby the call to the
* web service is made in the paginateCount method, the results stored and the
* total results returned, then the actual results are returned from the
* paginate method. This way the call to the web service is only made once.
* However, in order to do this, we need to know the page and limit params in
* the paginateCount method. So these should be set in this Model::paginate
* property in the controller, before calling Controller::paginate().
*
* @var array
*/
public $paginate = array();
/**
* Temporarily stores the results after being fetched during the paginateCount
* method, before returning in the paginate method.
*
* @var array
*/
protected $_results = null;
/**
* Adds the datasource to the Connection Manager's list of sources if it is
* not already there. It would normally be there if you add the datasource
* details to your app/config/database.php file, but this code negates the
* need to do that. It adds the datasource for the current model being
* constructed with default basic configuration options, and extra options
* from the GDATA_CONFIG->{$this->useDbConfig} class property from the file in
* plugins/gdata/config/gdata_config.php if it exists, and extra options from
* Gdata.config key in the Configure class, if set. Options should include
* X-GData-Key as a minimum if required by the Gdata API, and also optionally
* oauth_consumer_key, oauth_consumer_secret, oauth_token and
* oauth_token_secret keys
*
* @param mixed $id
* @param string $table
* @param mixed $ds
*/
public function __construct($id = false, $table = null, $ds = null) {
// Get the list of datasource that the ConnectionManager is aware of
$sources = ConnectionManager::sourceList();
// If this model's datasource isn't in it, add it
if (!in_array($this->useDbConfig, $sources)) {
// Default minimum config
$config = array(
'datasource' => 'Gdata.GdataSource',
'driver' => 'Gdata.' . Inflector::camelize($this->useDbConfig),
);
// Try an import the plugins/gdata/config/gdata_config.php file and merge
// any default and datasource specific config with the defaults above
if (App::import(array('type' => 'File', 'name' => 'Gdata.GDATA_CONFIG', 'file' => 'config'.DS.'gdata_config.php'))) {
$GDATA_CONFIG = new GDATA_CONFIG();
if (isset($GDATA_CONFIG->default)) {
$config = array_merge($config, $GDATA_CONFIG->default);
}
if (isset($GDATA_CONFIG->{$this->useDbConfig})) {
$config = array_merge($config, $GDATA_CONFIG->{$this->useDbConfig});
}
}
// Add any config from Configure class that you might have added at any
// point before the model is instantiated.
if (($configureConfig = Configure::read('Gdata.config')) != false) {
$config = array_merge($config, $configureConfig);
}
// Add the datasource, with it's new config, to the ConnectionManager
ConnectionManager::create($this->useDbConfig, $config);
}
parent::__construct($id, $table, $ds);
}
/**
* Overloads the Model::find() method. Resets request array in between finds
*
* @param string $type
* @param array $options
*/
public function find($type, $options = array()) {
$this->request = array();
$options = $this->_standardGdataParams($options);
return parent::find($type, $options);
}
protected function _standardGdataParams($query) {
return $query;
}
/**
* Add pagination params from the $query to the $request
*
* @param array $query Query array sent as options to a find call
* @return array
*/
protected function _paginationParams($query) {
if (!empty($query['limit'])) {
$this->request['uri']['query']['max-results'] = $query['limit'];
} else {
$this->request['uri']['query']['max-results'] = $query['limit'] = 10;
}
if (!empty($query['page'])) {
$this->request['uri']['query']['start-index'] = ($query['page'] - 1) * $query['limit'] + 1;
} else {
$this->request['uri']['query']['start-index'] = $query['page'] = 1;
}
return $query;
}
/**
* Called by Controller::paginate(). Calls the custom find type. Stores the
* results for later returning in the paginate() method. Returns the total
* number of results from the full result set.
*
* @param array $conditions
* @param integer $recursive
* @param array $extra
* @return integer The number of items in the full result set
*/
public function paginateCount($conditions, $recursive = 1, $extra = array()) {
$response = $this->find($this->paginate[0], $this->paginate);
$this->_results = $response;
return $response['feed']['totalResults'];
}
/**
* Returns the results of the call to the web service fetched in the
* self::paginateCount() method above.
*
* @param mixed $conditions
* @param mixed $fields
* @param mixed $order
* @param integer $limit
* @param integer $page
* @param integer $recursive
* @param array $extra
* @return array The results of the call to the web service
*/
public function paginate($conditions, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null, $extra = array()) {
return $this->_results;
}
}
?>