/[drupal]/contributions/modules/taxonomy_limit/taxonomy_limit.module
ViewVC logotype

Contents of /contributions/modules/taxonomy_limit/taxonomy_limit.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Fri Jun 12 16:41:47 2009 UTC (5 months, 2 weeks ago) by mvc
Branch: MAIN
CVS Tags: DRUPAL-6--0-1, HEAD
Changes since 1.6: +251 -199 lines
File MIME type: text/x-php
drupal 6 port: fixes #262408 by funana, #221749 by jesss, #130829 by mariuss, #295267 by pebosi (see README.txt for upgrade notes)
1 <?php
2 // $Id: taxonomy_limit.module,v 1.5.2.2 2007/01/30 05:51:46 codexmas Exp $
3
4 /**
5 * @file taxonomy_limit.module
6 * Allows restricting the number of taxonomy terms selected for a given
7 * vocabulary and content type.
8 */
9
10 /**
11 * Implementation of hook_help()
12 */
13 function taxonomy_limit_help($path, $arg) {
14 switch ($path) {
15 case 'admin/settings/taxonomy_limit':
16 $output .= 'Use this to set the maximum number of taxonomy terms which may be selected from a given vocabulary for a particular content type. Setting a maximum of zero for a category will disable the check.';
17 if (!module_exists('i18nstrings')) {
18 $output .= ' '. t('To make error messages translatable, install and enable the <a href="!i18n">Internationalization package</a>.', array('!i18n' => url('http://drupal.org/project/i18n', array('absolute' => TRUE))));
19 }
20 else {
21 $output .= ' '. t('Error messages may be translated by declaring them as multilingual variables; see <a href="!translate">README.txt</a> for details.',
22 array('!translate' => url(drupal_get_path('module', 'taxonomy_limit') .'/README.txt')));
23 }
24 break;
25 }
26 return $output;
27 }
28
29 /**
30 * @desc Menu hook
31 */
32 function taxonomy_limit_menu() {
33 $items['admin/settings/taxonomy_limit'] = array(
34 'title' => 'Taxonomy Limit',
35 'description' => 'Limit terms selected in a vocabulary.',
36 'page callback' => 'drupal_get_form',
37 'page arguments' => array('taxonomy_limit_settings'),
38 'access arguments' => array('administer taxonomy'),
39 'type' => MENU_NORMAL_ITEM,
40 );
41
42 return $items;
43 }
44
45 /**
46 *@desc This function preforms the magic to add the validation on the
47 * form level, not the element level. It also appends the description
48 * of the category to provide a useful hint on the limit.
49 */
50 function taxonomy_limit_form_alter(&$form, $form_state, $form_id) {
51 // Make sure this form is for editing a node
52 if (!($form['type'] && is_array($form['type']) && $form['type']['#value'])) {
53 return;
54 }
55 // Get the content types we are allowed to work with, if set
56 if (!$limit_types = variable_get('taxonomy_limit_types', NULL)) {
57 return;
58 }
59 // Is this a node type that has been selected?
60 if ($limit_types[$form['#node']->type] .'_node_form' != $form_id) {
61 return;
62 }
63
64 // Load the validation data for the content type
65 if ($tax_limit = variable_get('taxonomy_limit_'. $form['#node']->type, NULL)) {
66 $validate = FALSE;
67 // Go through each configured category
68 foreach ($tax_limit AS $vid => $data) {
69 $max = $data['max'];
70 // Only allow numbers greater than zero (disabled)
71 if (!(is_numeric($max) && $max > 0)) {
72 continue;
73 }
74 // Validate that this category still meets the criteria
75 if (taxonomy_limit_validate_vocabulary($vid)) {
76 $validate = TRUE;
77 $desc = " <strong>".
78 t("(Choose up to @max)", array("@max" => taxonomy_limit_number($max)))
79 ."</strong>";
80 if (is_array($form['taxonomy']['tags'][$vid])) {
81 $form['taxonomy']['tags'][$vid]['#description'] .= $desc;
82 }
83 else {
84 $form['taxonomy'][$vid]['#description'] .= $desc;
85 }
86 }
87 }
88 // Only set validation callback if limits have been set.
89 if ($validate) {
90 $form['#validate'][] = '_taxonomy_limit_validate_max_terms';
91 }
92 }
93 }
94
95 /**
96 *@desc Here is the function the creates the error for the category form element.
97 * We are passed the $all string, which is a concatenated vid/max series of entries
98 * seperated by a single pipe.
99 */
100 //function _taxonomy_limit_validate_max_terms($form_id, $edit, $form, $all) {
101 function _taxonomy_limit_validate_max_terms($form, &$form_state) {
102 // Fetch all limits for vocabs for this type
103 $tax_limit = variable_get('taxonomy_limit_'. $form['#node']->type, NULL);
104 $tax_limit_error = variable_get('taxonomy_limit_'. $form['#node']->type .'_error', NULL);
105 // Loop through all the vocabulary validation items
106 foreach ($tax_limit AS $vid => $data) {
107 $max = $data['max'];
108 // Only allow numbers greater than zero (disabled)
109 if (!(is_numeric($max) && $max > 0)) {
110 continue;
111 }
112 // Validate once again that this vid meets the criteria
113 if (!taxonomy_limit_validate_vocabulary($vid)) {
114 continue;
115 }
116 // Create error message
117 if ($tax_limit_error[$vid]['error']) {
118 // not calling t() because this is user input; see README.txt for how to
119 // translate this
120 $error = $tax_limit_error[$vid]['error'];
121 }
122 else {
123 // default error message
124 $vocab = taxonomy_vocabulary_load($vid);
125 $error = t('Too many terms selected for vocabulary @name', array('@name' => $vocab->name));
126 }
127 // After node has been saved, and is being edited...
128 if ($tags = $form['taxonomy']['tags'][$vid]['#value']) {
129 if (count(explode(',', $tags)) > $max) {
130 form_set_error("taxonomy][tags][$vid", $error);
131 }
132 }
133 // Non freetagging vocabulary
134 // (taxonomy super select sets the values to FALSE, instead of removing
135 // them from the array, so we filter out these values)
136 if (is_array($form['taxonomy'][$vid]['#value']) &&
137 sizeof(array_filter($form['taxonomy'][$vid]['#value'])) > $max) {
138 form_set_error("taxonomy][$vid", $error);
139 }
140 }
141 }
142
143 function taxonomy_limit_validate_vocabulary(&$vid, &$terms = array()) {
144 // Were we passed a vid?
145 if (!is_numeric($vid)) {
146 return FALSE;
147 }
148 $vocab = taxonomy_vocabulary_load($vid);
149 // did the vocab load properly?
150 if (!is_object($vocab)) {
151 return FALSE;
152 }
153 // indicate that this is a freetagging vocab, and populates the $terms by ref
154 if ($vocab->tags) {
155 if (!$terms[$vid]) {
156 $terms[$vid] = taxonomy_get_children(NULL, $vid);
157 }
158 return TRUE;
159 }
160 // must allow multiple selections (implicitly allowed for freetagging vocabs)
161 if (!$vocab->multiple) {
162 return FALSE;
163 }
164 // All good, allow this category
165 return TRUE;
166 }
167
168 function taxonomy_limit_settings() {
169 drupal_add_css(drupal_get_path('module', 'taxonomy_limit') .'/style.css');
170 $types = node_get_types('names');
171 $default_values = $types;
172 foreach ($default_values as $key => $value) {
173 $default_values[$key] = 0;
174 }
175 $limit_types = variable_get('taxonomy_limit_types', $default_values);
176 $form['taxonomy_limit_types'] = array(
177 '#type' => 'checkboxes',
178 '#options' => $types,
179 '#title' => t('Apply to content type'),
180 '#default_value' => $limit_types,
181 '#description' =>
182 t("Only content types with vocabularies that have the multiple option enabled are affected by this module. (Freetagging vocabularies have this enabled implicitly.)"),
183 );
184 if ($limit_types) {
185 foreach ($limit_types AS $key => $type) {
186 if (!is_numeric($type)) {
187 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type] = array(
188 '#type' => 'fieldset',
189 '#title' => t('Maximum number of terms for content type %type', array('%type' => $type)),
190 '#tree' => TRUE,
191 '#collapsible' => TRUE,
192 );
193 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type .'_error'] = array(
194 '#type' => 'fieldset',
195 '#title' => t('Error messages for content type %type', array('%type' => $type)),
196 '#tree' => TRUE,
197 '#collapsible' => TRUE,
198 );
199 $vocabs = taxonomy_get_vocabularies($type);
200 if ($vocabs) {
201 $tax_limit = variable_get('taxonomy_limit_'. $type, array());
202 $tax_limit_error = variable_get('taxonomy_limit_'. $type .'_error', array());
203 $invalid_count = 0;
204 foreach ($vocabs AS $vid => $vocab) {
205 $dummy = array(0 => 0);
206 if (!taxonomy_limit_validate_vocabulary($vid, $dummy)) {
207 $invalid_count++;
208 continue;
209 }
210 if (!$tax_limit[$vid]) {
211 $tax_limit[$vid] = array();
212 }
213 if (!$tax_limit_error[$vid]) {
214 $tax_limit_error[$vid] = array();
215 }
216 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type][$vid]['max'] = array(
217 '#prefix' => '<div class="taxonomy_limit_types">',
218 '#title' => t('Maximum number of terms for vocabulary %vocab', array('%vocab' => $vocab->name)),
219 '#type' => 'textfield',
220 '#size' => 4,
221 '#default_value' => $tax_limit[$vid]['max'] ? $tax_limit[$vid]['max'] : 0,
222 '#description' => t('Set to 0 to allow all terms.'),
223 '#suffix' => '</div>',
224 );
225 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type. '_error'][$vid]['error'] = array(
226 '#prefix' => '<div class="taxonomy_limit_types">',
227 '#title' => t('Error message for vocabulary %vocab', array('%vocab' => $vocab->name)),
228 '#type' => 'textfield',
229 '#size' => 60,
230 '#default_value' => $tax_limit_error[$vid]['error'] ? $tax_limit_error[$vid]['error']
231 : t('Too many terms selected for vocabulary @name', array('@name' => $vocab->name)),
232 '#description' => t('This message will be shown if the user enters more than the allowed number of terms.'),
233 '#suffix' => '</div>',
234 );
235 }
236 if ($invalid_count == count($vocabs)) {
237 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type][$vid] = array(
238 '#value' => t('None of the vocabularies defined for this content type are valid.')
239 .'<br />',
240 );
241 }
242 }
243 else{
244 $form['settings']['taxonomy_limit_types']['taxonomy_limit_'. $type][$vid] = array(
245 '#value' => t('There are no vocabularies defined for this content type.')
246 .'<br />',
247 );
248
249 }
250 }
251 }
252 }
253 return system_settings_form($form);
254 }
255
256 function taxonomy_limit_number($number) {
257 $numbers = array(
258 0 => t('zero'),
259 1 => t('one'),
260 2 => t('two'),
261 3 => t('three'),
262 4 => t('four'),
263 5 => t('five'),
264 6 => t('six'),
265 7 => t('seven'),
266 8 => t('eight'),
267 9 => t('nine'),
268 10 => t('ten'),
269 11 => t('eleven'),
270 12 => t('twelve'),
271 13 => t('thirteen'),
272 14 => t('fourteen'),
273 15 => t('fifteen'),
274 16 => t('sixteen'),
275 17 => t('seventeen'),
276 18 => t('eighteen'),
277 19 => t('nineteen'),
278 20 => t('twenty'),
279 );
280 // Return the original number if we don't have it in the list.
281 if (!$numbers[$number]) {
282 return $number;
283 }
284 // Return the word for the number provided
285 return $numbers[$number];
286 }

  ViewVC Help
Powered by ViewVC 1.1.2