-
Notifications
You must be signed in to change notification settings - Fork 14
/
SitemapBehavior.php
104 lines (86 loc) · 2.91 KB
/
SitemapBehavior.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
<?php
class SitemapBehavior extends ModelBehavior {
/**
* _CacheKey - Cache Key
*
* @var string
*/
protected $_CacheKey = 'Sitemap.ModelData.';
/**
* setup
*
* @param Model $Model [description]
* @param array $settings [description]
* @return [type] [description]
*/
public function setup(Model $Model, $settings = array()) {
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = array(
'primaryKey' => 'id',
'loc' => 'buildUrl',
'lastmod' => 'modified',
'changefreq' => 'daily',
'priority' => '0.9',
'conditions' => array(),
);
}
$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
}
/**
* buildUrl - basic build URL function for the model behavior, basic URL using action => 'view'
*
* @return [type] [description]
*/
public function buildUrl(Model $Model, $primaryKey) {
return Router::url(array('plugin' => NULL, 'controller' => Inflector::tableize($Model->name), 'action' => 'view', $primaryKey), TRUE);
}
/**
* generateSitemapData - generate the sitemap data, attempting to hit the cache for this data
*
* @param Model $Model [description]
* @return [type] [description]
*/
public function generateSitemapData(Model $Model) {
//Attempt to hit the Model Cache for data
$sitemapData = Cache::read($this->_CacheKey . $Model->name);
if ($sitemapData !== false) {
return $sitemapData;
}
//Load the Model Data
$modelData = $Model->find('all', array(
'conditions' => $this->settings[$Model->alias]['conditions'],
'recursive' => -1,
));
//Build the sitemap elements
$sitemapData = $this->_buildSitemapElements($Model, $modelData);
//Write to the Cache
Cache::write($this->_CacheKey . $Model->name, $sitemapData);
return $sitemapData;
}
/**
* _buildSitemapElements - build the sitemap elements
*
* @param Model $Model [description]
* @param [type] $modelData [description]
* @return [type] [description]
*/
protected function _buildSitemapElements(Model $Model, $modelData) {
$sitemapData = array();
//Loop through the Model data and create the array of elements for the sitemap
foreach($modelData as $key => $data) {
$sitemapData[$key] = array();
$sitemapData[$key]['loc'] = call_user_func(array($Model, $this->settings[$Model->alias]['loc']), $data[$Model->alias][$this->settings[$Model->alias]['primaryKey']]);
if($this->settings[$Model->alias]['lastmod'] !== FALSE) {
$sitemapData[$key]['lastmod'] = $data[$Model->alias][$this->settings[$Model->alias]['lastmod']];
}
if($this->settings[$Model->alias]['changefreq'] !== FALSE) {
$sitemapData[$key]['changefreq'] = $this->settings[$Model->alias]['changefreq'];
}
if($this->settings[$Model->alias]['priority'] !== FALSE) {
$sitemapData[$key]['priority'] = $this->settings[$Model->alias]['priority'];
}
}
return $sitemapData;
}
}
?>