-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.taxonomies.php
executable file
·201 lines (161 loc) · 5.6 KB
/
hooks.taxonomies.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Taxonomies: Hooks
* @ author: Mike Wenger, Q Digital Studio
* @ version: 1.0.0
* @author_url: http://qdigitalstudio.com
*/
// https://v1.statamic.com/learn/documentation/hooks
class Hooks_taxonomies extends Hooks {
// class properties
private $pkg;
public $key;
/**
* Constructor
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
$this->pkg = $this->core->pkg;
$this->key = $this->core->key;
}// end: __construct
//-----------------------------------------------------------
/**
* control_panel__add_routes hook
*
* @access public
* @return void
*/
public function control_panel__add_routes()
{
$app = \Slim\Slim::getInstance();
// -------------------------------------
// View(s)
// -------------------------------------
$app->get('/' . $this->pkg, function() use ($app) {
authenticateForRole('admin');
doStatamicVersionCheck($app);
Statamic_View::set_templates(array('manage'), __DIR__ . '/views');
// fetch errors
$errors = $this->flash->get($this->pkg . '_errors');
// set view data
$data = $this->core->getTaxonomies();
$data['field_config'] = $this->core->getRelicatorConfig();
$data['pkg'] = $this->pkg;
$data['fieldname'] = 'taxonomy';
$data['errors'] = $errors ? implode('<br>', $errors) : '';
// render
$app->render(null, array('route' => $this->pkg, 'app' => $app) + $data);
})->name( $this->pkg );
// -------------------------------------
// Update
// -------------------------------------
$app->post('/' . $this->pkg . '/update', function() use ($app) {
authenticateForRole('admin');
doStatamicVersionCheck($app);
$errors = array();
// fetch the yaml array (yes, this isn't cleansed - yet)
$taxonomies[$this->key] = $_POST['page']['yaml']['taxonomy'];
// add the type for replicator
foreach($taxonomies[$this->key] as $gi => $group)
{
$taxonomy_group_slug_name = 'taxonomy_group_slug';
$taxonomies_name = 'taxonomies';
$taxonomy_name_name = 'taxonomy_name';
$taxonomy_slug_name = 'taxonomy_slug';
if (!isset($group[$taxonomy_group_slug_name]) OR empty($group[$taxonomy_group_slug_name]))
{
$errors[$taxonomy_group_slug_name . '_display_taxonomies'] = Localization::fetch($taxonomy_group_slug_name . '_display_taxonomies') . ': taxonomy group slugs may not be empty.';
}
// group data assurance
$taxonomies[$this->key][$gi][$taxonomy_group_slug_name] = $this->core->slugify($group[$taxonomy_group_slug_name]);
$taxonomies[$this->key][$gi]['type'] = 'taxonomy_group';
// loop each taxonomy
foreach($group[$taxonomies_name] as $ti => $taxonomy)
{
if (!isset($taxonomy[$taxonomy_name_name]) OR empty($taxonomy[$taxonomy_name_name]))
{
$errors[$taxonomy_name_name . '_display_taxonomies'] = Localization::fetch($taxonomy_name_name . '_display_taxonomies') . ': taxonomy values may not be empty.';
}
if (!isset($taxonomy[$taxonomy_slug_name]) OR empty($taxonomy[$taxonomy_slug_name]))
{
$errors[$taxonomy_slug_name . '_display_taxonomies'] = Localization::fetch($taxonomy_slug_name . '_display_taxonomies') . ': taxonomy values may not be empty.';
}
// taxonomy data assurance
$taxonomies[$this->key][$gi][$taxonomies_name][$ti][$taxonomy_slug_name] = $this->core->slugify($taxonomy[$taxonomy_slug_name]);
}
}
// echo '<pre>';
// print_r($taxonomies);
// exit;
// set flash errors
$this->flash->set($this->pkg . '_errors', (!empty($errors) ? $errors : FALSE));
// only save if we do not have errors
if (empty($errors))
{
// dump it
File::put($this->core->getTaxonomiesPath(), YAML::dump($taxonomies, 1));
$app->flash('success', Localization::fetch('success_taxonomies'));
}
$app->redirect($app->urlFor( $this->pkg ));
// $this->field_data = Helper::ensureArray($this->field_data);
// foreach ($taxonomies as $key => $value) {
// $taxonomies[$key] = strtolower($value);
// }
});
}// end: control_panel__add_routes
//-----------------------------------------------------------
/**
* control_panel__add_to_head hook
*
* @access public
* @return string
*/
public function control_panel__add_to_head()
{
$locations = array(
'/publish',
'/' . $this->pkg
);
if (in_array(URL::getCurrent(false), $locations))
{
$return = '';
$css = array(
'taxonomies.css'
);
foreach($css as $file)
{
$return .= $this->css->link($file)."\n";
}
return $return;
}
}// end: control_panel__add_to_head
//-----------------------------------------------------------
/**
* control_panel__add_to_foot hook
*
* @access public
* @return string
*/
public function control_panel__add_to_foot()
{
$locations = array(
'/' . $this->pkg
);
if (in_array(URL::getCurrent(false), $locations))
{
$return = '';
$js = array(
'taxonomies.js'
);
foreach($js as $file)
{
$return .= $this->js->link($file)."\n";
}
return $return;
}
}// end: control_panel__add_to_foot
}// end: class