avh-ec.category-group.php 11.4 KB
Newer Older
root's avatar
root committed
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
<?php

/**
 * AVH Extended Categorie Category Group Class
 *
 * @author Peter van der Does
 * @copyright 2010
 */
class AVH_EC_Category_Group
{
	/**
	 * Taxonomy name
	 * @var string
	 */
	var $taxonomy_name;
	
	var $db_options_widget_titles;
	
	var $options_widget_titles;
	
	var $widget_done_catgroup;

	/**
	 * PHP4 constructor.
	 *
	 */
	function AVH_EC_Category_Group ()
	{
		return $this->__construct();
	}

	/**
	 * PHP5 Constructor
	 * Init the Database Abstraction layer
	 *
	 */
	function __construct ()
	{
		global $wpdb;
		
		register_shutdown_function(array(&$this, '__destruct'));
		
		/**
		 * Taxonomy name
		 * @var string
		 */
		$this->taxonomy_name = 'avhec_catgroup';
		
		$this->db_options_widget_titles = 'avhec_widget_titles';
		// add DB pointer
		$wpdb->avhec_cat_group = $wpdb->prefix . 'avhec_category_groups';
		
		/**
		 * Create the table if it doesn't exist.
		 *
		 */
		if ($wpdb->get_var('show tables like \'' . $wpdb->avhec_cat_group . '\'') != $wpdb->avhec_cat_group) {
			add_action('init', array(&$this, 'doCreateTable'), 2); // Priority needs to be the same as the Register Taxonomy
		}
		add_action('init', array(&$this, 'doRegisterTaxonomy'), 2); // Priority for registering custom taxonomies is +1 over the creation of the initial taxonomies
		add_action('init', array(&$this, 'doSetupOptions'));
		
		add_action('admin_init', array(&$this, 'addMetaBoxes'));
	
	}

	/**
	 * PHP5 style destructor and will run when database object is destroyed.
	 *
	 * @return bool Always true
	 */
	function __destruct ()
	{
		return true;
	}

	/**
	 * Create Table
	 * @WordPress action init
	 *
	 */
	function doCreateTable ()
	{
		global $wpdb;
		
		// Setup the DB Tables
		$charset_collate = '';
		
		if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
			if (! empty($wpdb->charset))
				$charset_collate = 'DEFAULT CHARACTER SET ' . $wpdb->charset;
			if (! empty($wpdb->collate))
				$charset_collate .= ' COLLATE ' . $wpdb->collate;
		}
		
		$sql = 'CREATE TABLE `' . $wpdb->avhec_cat_group . '` ( `group_term_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, `term_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (`group_term_id`, `term_id`) )' . $charset_collate . ';';
		
		$result = $wpdb->query($sql);
	}

	/**
	 * Setup Group Categories Taxonomy
	 * @WordPress action init
	 *
	 */
	function doRegisterTaxonomy ()
	{
		/**
		 * As we don't want to see the Menu Item we have to disable show_ui. This also disables the metabox on the posts and pages, so we add thse manually instead.
		 * We remove the capabilities to manage, edit and delete the terms. We have written this part ourselves and don't use WordPress for these functions. The only one we use is the assign_terms.
		 */
		$labels = array('name'=>__('Category Groups', 'avh-ec'), 'singular_name'=>__('Category Group', 'avh-ec'), 'search_items'=>__('Search Category Groups', 'avh-ec'), 'popular_items'=>__('Popular Category Groups'), 'all_items'=>__('All Category Groups'), 'parent_item'=>__('Parent Category Group'), 'parent_item_colon'=>__('Parent Category Group:'), 'edit_item'=>__('Edit Category Group'), 'update_item'=>__('Update Category Group'), 'add_new_item'=>__('Add New Category Group'), 'new_item_name'=>__('New Category Group Name'));
		$caps = array('manage_terms'=>null, 'edit_terms'=>null, 'delete_terms'=>null, 'assign_terms'=>'edit_posts');
		register_taxonomy($this->taxonomy_name, array('post', 'page'), array('hierarchical'=>TRUE, 'labels'=>$labels, 'query_var'=>TRUE, 'rewrite'=>TRUE, 'show_in_nav_menus'=>FALSE, 'public'=>TRUE, 'show_ui'=>FALSE, 'capabilities'=>$caps));
	
	}

	/**
	 * Setup the options for the widget titles
	 * @WordPress action init
	 *
	 */
	function doSetupOptions ()
	{
		// Setup the standard groups if the none group does not exists.
126
		$all_categories = $this->getAllCategoriesTermID();
root's avatar
root committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		if (false === $this->getTermIDBy('slug', 'none')) {
			$none_group_id = wp_insert_term('none', $this->taxonomy_name, array('description'=>__('This group will not show the widget.', 'avh-ec')));
			$all_group_id = wp_insert_term('All', $this->taxonomy_name, array('description'=>__('Holds all the categories.', 'avh-ec')));
			$home_group_id = wp_insert_term('Home', $this->taxonomy_name, array('description'=>__('This group will be shown on the front page.', 'avh-ec')));
			
			//	Fill the standard groups with all categories
			$this->setCategoriesForGroup($home_group_id['term_id'], $all_categories);
			$this->setWidgetTitleForGroup($all_group_id['term_id'], '');
			$this->setWidgetTitleForGroup($home_group_id['term_id'], '');
		}
		
		$options = get_option($this->db_options_widget_titles);
		if (! $options) {
			$options = array();
			$id = $this->getTermIDBy('slug', 'all');
			$options[$id] = '';
			$id = $this->getTermIDBy('slug', 'home');
			$options[$id] = '';
			update_option($this->db_options_widget_titles, $options);
		}
		$this->options_widget_titles = $options;
148
		$this->setCategoriesForGroup($this->getTermIDBy('slug', 'all'), $all_categories);
root's avatar
root committed
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
	}

	/**
	 * Add the metaboxes for the pots and page pages.
	 * @WordPress action admin_init
	 *
	 */
	function addMetaBoxes ()
	{
		add_meta_box($this->taxonomy_name . 'div', __('Category Groups', 'avh-ec'), 'post_categories_meta_box', 'post', 'side', 'core', array('taxonomy'=>$this->taxonomy_name));
		add_meta_box($this->taxonomy_name . 'div', __('Category Groups', 'avh-ec'), 'post_categories_meta_box', 'page', 'side', 'core', array('taxonomy'=>$this->taxonomy_name));
	}

	/**
	 * Get all groups term_id
	 *
	 * @return array Term_id
	 */
	function getAllCategoriesTermID ()
	{
		$all_cat_id = array();
		$categories = get_categories();
		if (! is_wp_error($categories)) {
			foreach ($categories as $category) {
				$all_cat_id[] = $category->term_id;
			}
		}
		return ($all_cat_id);
	}

	/**
	 * Get the categories from the given group from the DB
	 *
	 * @param int $group_id The Taxonomy Term ID
	 * @return Array|False categories. Will return FALSE, if the row does not exists.
	 *
	 */
	function getCategoriesFromGroup ($group_id)
	{
		global $wpdb;
		
		// Query database
		$result = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->terms . ' t, ' . $wpdb->avhec_cat_group . ' cg WHERE t.term_id = cg.term_id AND cg.group_term_id = %d', $group_id));
		
		if (is_array($result)) { // Call succeeded
			if (empty($result)) { // No rows found
				$return = array();
			} else {
				foreach ($result as $row) {
					$return[] = $row->term_id;
				}
			}
		} else {
			$return = false;
		}
		return ($return);
	}

	/**
	 * Set the categories for the given group from the DB. Insert the group if it doesn't exists.
	 *
	 * @param int $group_id The Taxonomy Term ID
	 * @param array $categories The categories
	 * @return Object (false if not found)
	 *
	 */
	function setCategoriesForGroup ($group_id, $categories = array())
	{
		global $wpdb;
		
		$old_categories = $this->getCategoriesFromGroup($group_id);
		
		if (! is_array($categories)) {
			$categories = array();
		}
		$new_categories = $categories;
		sort($old_categories);
		sort($new_categories);
		// If the new and old values are the same, no need to update.
		if ($new_categories === $old_categories) {
			return false;
		}
		
		$new = array_diff($new_categories, $old_categories);
		$removed = array_diff($old_categories, $new_categories);
		
		if (! empty($new)) {
			foreach ($new as $cat_term_id) {
				$insert[] = '(' . $group_id . ',' . $cat_term_id . ')';
			}
			$value = implode(',', $insert);
			$sql = 'INSERT INTO ' . $wpdb->avhec_cat_group . ' (group_term_id, term_id) VALUES ' . $value;
			$result = $wpdb->query($sql);
		
		}
		
		if (! empty($removed)) {
			$delete = implode(',', $removed);
			$sql = $wpdb->prepare('DELETE FROM ' . $wpdb->avhec_cat_group . ' WHERE group_term_id=%d and term_id IN (' . $delete . ')', $group_id);
			$result = $wpdb->query($sql);
		
		}
		
		return $result;
	}

	/**
	 * Set the Widget Title for a Group
	 * @param int $group_id
	 * @param string $widget_title
	 *
	 */
	function setWidgetTitleForGroup ($group_id, $widget_title = '')
	{
		$this->options_widget_titles[$group_id] = $widget_title;
		update_option($this->db_options_widget_titles, $this->options_widget_titles);
	}

	/**
	 * Return the title for a group_id if exsist otherwise return false
	 * @param $group_id
	 *
	 */
	function getWidgetTitleForGroup ($group_id)
	{
		if (isset($this->options_widget_titles[$group_id])) {
			return ($this->options_widget_titles[$group_id]);
		}
		return false;
	}

	/**
	 * Delete the Widget Title for a group
	 *
	 * @param $group_id
	 */
	function doDeleteWidgetTitle ($group_id)
	{
		if (isset($this->db_options_widget_titles[$group_id])) {
			unset($this->db_options_widget_titles[$group_id]);
		}
		update_option($this->db_options_widget_titles, $this->options_widget_titles);
	}

	/**
	 * Same as get_term_by, but returns the ID only if found, else false
	 * @param string $field
	 * @param string $value
	 * @return int|boolean
	 */
	function getTermIDBy ($field, $value)
	{
		$row = get_term_by($field, $value, $this->taxonomy_name);
		if (false === $row) {
			$return = false;
		} else {
			$return = (int) $row->term_id;
		}
		return ($return);
	}

	/**
	 * Gets all information of a group
	 *
	 * @param $group_id
	 * @return Object|False Returns False when the group doesn't exists.
	 */
	function getGroup ($group_id)
	{
		global $wpdb;
		
		$result = get_term((int) $group_id, $this->taxonomy_name);
		if (null === $result) {
			$result = false;
		}
		return ($result);
	}

	function getGroupByCategoryID($category_id){
		$return = get_term_by('slug', 'none', $this->taxonomy_name);
		$cat_groups = get_terms($this->taxonomy_name, array('hide_empty'=>FALSE));
		
		foreach ($cat_groups as $group) {
			$cats = $this->getCategoriesFromGroup($group->term_id);
			if ($group->slug != 'all' && in_array($category_id,$cats)) {
				$return = $group;
				break;
			}
		}
		return $return;
	}
	/**
	 * Inserts a new group
	 *
	 * @param $term
	 * @param array $args
	 */
	function doInsertGroup ($term, $args = array(), $widget_title = '')
	{
		$row = wp_insert_term($term, $this->taxonomy_name, $args);
		$this->setWidgetTitleForGroup($term, $widget_title);
		return ($row['term_id']);
	}

	/**
	 * Deletes a group
	 *
	 * @param $group_id
	 */
	function doDeleteGroup ($group_id)
	{
		
		global $wpdb;
		
		$group = $this->getGroup($group_id);
		$result = $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->avhec_cat_group . ' WHERE group_term_id=%d', $group_id));
		$this->doDeleteWidgetTitle($group_id);
		$return = wp_delete_term($group->term_id, $this->taxonomy_name);
		
		return ($return);
	}

	/**
	 * Update a group
	 *
	 * @param $group_id
	 * @param $selected_categories
	 * @param $widget_title
	 *
	 * return -1,0,1 Unknown Group, Duplicate Slug, Succesfull
	 */
	function doUpdateGroup ($group_id, $args = array(), $selected_categories, $widget_title = '')
	{
		
		$group = $this->getGroup($group_id);
		if (is_object($group)) {
			$id = wp_update_term($group->term_id, $this->taxonomy_name, $args);
			if (! is_wp_error($id)) {
				$this->setWidgetTitleForGroup($group_id, $widget_title);
				$this->setCategoriesForGroup($group_id, $selected_categories);
				$return = 1; // Succesful
			} else {
				$return = 0; // Duplicate Slug
			}
		} else {
			$return = - 1; // Unknown group
		}
		return ($return);
	}

	/**
	 * Deletes the given category from all groups
	 *
	 * @param $category_id
	 */
	function doDeleteCategoryFromGroup ($category_id)
	{
		global $wpdb;
		$result = $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->avhec_cat_group . ' WHERE term_id=%d', $category_id));
	}
}
?>