/[drupal]/contributions/modules/vocabindex/vocabindex.admin.inc
ViewVC logotype

Contents of /contributions/modules/vocabindex/vocabindex.admin.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Jan 7 19:34:19 2008 UTC (22 months, 3 weeks ago) by xano
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Fixed issues #204027 and #206837 (thanks to Surprise) as well as some other bugs  and added some new features
1 <?php
2 /**
3 *Admin general settings page
4 */
5 function vocabindex_admin()
6 {
7 $form['vocabindex_terms_order']=array(
8 '#type'=>'select',
9 '#title'=>t('Terms order'),
10 '#description'=>t('Select the order in which the terms should be displayed.'),
11 '#default_value'=>variable_get('vocabindex_terms_order', 'weight'),
12 '#options'=>array(
13 'weight'=>t('By weight'),
14 'name'=>t('By name'),
15 ),
16 );
17
18 $form['vocabindex_list_style']=array(
19 '#type'=>'select',
20 '#title'=>t('List style'),
21 '#description'=>t('Select the display style for the terms lists.'),
22 '#default_value'=>variable_get('vocabindex_list_style', 'threaded'),
23 '#options'=>array(
24 'threaded'=>t('Threaded'),
25 'flat'=>t('Flat'),
26 'flat-toplevel'=>t('Flat (top-level terms only)'),
27 ),
28 );
29
30 $form['vocabindex_stylesheet']=array(
31 '#type'=>'checkbox',
32 '#title'=>t('Use default stylesheet'),
33 '#description'=>t('Uncheck if you don\'t want to use the default Vocabindex stylesheet.'),
34 '#default_value'=>variable_get('vocabindex_stylesheet', TRUE),
35 );
36
37 $form['vocabindex_vocab_auto_refresh']=array(
38 '#type'=>'checkbox',
39 '#title'=>t('Vocabulary auto-refresh'),
40 '#description'=>t('Whether Vocabindex should automatically refresh vocabulary index pages or not. <strong>Warning:</strong> Refreshing the index pages causes all dynamically generated links to those pages to disappear from the menu.'),
41 '#default_value'=>variable_get('vocabindex_vocab_auto_refresh', FALSE),
42 );
43
44 return system_settings_form($form);
45 }
46
47 /**
48 *Admin paths page
49 */
50 function vocabindex_page_admin_paths()
51 {
52 $count=db_result(db_query("SELECT COUNT(*) FROM {vocabulary}"));
53 if($count!=0)
54 {
55 $output=t('Paths may only contain alphanumeric characters and dashes. For best SEO results, use dashes as word separators. Paths will automatically be converted to lowercase.');
56 $output.=drupal_get_form('vocabindex_form_admin_paths');
57 }
58 else
59 {
60 $output=t('There are no vocabularies to create index pages for. You can create vocabularies at <a href="!link">the Taxonomy page</a>.', array('!link'=>url(_vocabindex_menu_paths('taxonomy'))));
61 }
62
63 return $output;
64 }
65 function vocabindex_form_admin_paths()
66 {
67 $result=db_query("SELECT vi.path, v.name, v.vid FROM {vocabulary} v LEFT JOIN {vocabindex} vi ON v.vid=vi.vid ORDER BY v.name ASC");
68 while($row=db_fetch_object($result))
69 {
70 if($row->path)
71 {
72 $description=t('Currently located at <a href="!url">/!relative_url</a>', array('!url'=>url($row->path), '!relative_url'=>$row->path));
73 }
74 else
75 {
76 $description=t('There is currently no index page set for this vocabulary.');
77 }
78
79 $form['vocabindex_path_'.$row->vid]=array(
80 '#type'=>'textfield',
81 '#title'=>t('Path for %title index page', array('%title'=>$row->name)),
82 '#default_value'=>$row->path,
83 '#maxlength'=>'128',
84 '#description'=>$description,
85 );
86 }
87
88 $form['submit']=array(
89 '#type'=>'submit',
90 '#value'=>t('Save')
91 );
92
93 return $form;
94 }
95
96 function vocabindex_form_admin_paths_validate($form, &$form_state)
97 {
98 foreach($form_state['values'] as $element=>$path)
99 {
100 if(strpos($element, 'vocabindex_path_')!==FALSE)
101 {
102 $vid=str_replace('vocabindex_path_', '', $element);
103 if(!empty($path) && !preg_match('#[a-z0-9-]#i', $path))
104 {
105 form_set_error($element, t('Paths may only contain alphanumeric characters and dashes.'));
106 }
107 else
108 {
109 $result=db_result(db_query("SELECT vid FROM {vocabindex} WHERE path = '%s'", $path));
110 $message=vocabindex_check_index($result->vid, $path);
111 if($message=='used')
112 {
113 form_set_error($element, t('Path already exists.'));
114 }
115 }
116 }
117 }
118 }
119
120 function vocabindex_form_admin_paths_submit($form, &$form_state)
121 {
122 foreach($form_state['values'] as $element=>$path)
123 {
124 if(strpos($element, 'vocabindex_path_')!==FALSE)
125 {
126 $vid=str_replace('vocabindex_path_', '', $element);
127
128 $old_path=db_result(db_query("SELECT path FROM {vocabindex} WHERE vid = %d", $vid));
129 vocabindex_create_index($vid, $path);
130
131 //Present the user with a confirmation message
132 drupal_set_message(t('The paths have been updated.'));
133 }
134 }
135 }
136
137 /**
138 *Admin refresh page
139 */
140 function vocabindex_page_admin_refresh()
141 {
142 $count=db_result(db_query("SELECT COUNT(*) FROM {vocabulary}"));
143 if($count>0)
144 {
145 $output=t('When you have updated the descriptions of your vocabularies you can refresh all the index pages here to make sure the index pages show the new vocabulary descriptions. <strong>Warning:</strong> Refreshing the index pages causes all dynamically generated links to those pages to disappear from the menu.');
146
147 if(variable_get('vocabindex_vocab_auto_refresh', FALSE))
148 {
149 $output.=' '.t('<strong>You have enabled auto-refresh for the index pages. Therefore manual refresh is disabled. To disable auto-refresh, please proceed to <a href="!link">the settings page</a></strong>.', array('!link'=>url('admin/content/vocabindex/settings')));
150 }
151 else
152 {
153 $output.=drupal_get_form('vocabindex_form_admin_refresh', $disabled);
154 }
155 }
156 else
157 {
158 $output=t('There are no vocabularies to refresh. You can create vocabularies at <a href="!link">the Taxonomy page</a>.', array('!link'=>url(_vocabindex_menu_paths('taxonomy'))));
159 }
160
161 return $output;
162 }
163
164 function vocabindex_form_admin_refresh()
165 {
166
167 $form['cancel']=array(
168 '#type'=>'submit',
169 '#value'=>t('Cancel'),
170 );
171 $form['refresh']=array(
172 '#type'=>'submit',
173 '#value'=>t('Refresh'),
174 );
175
176 return $form;
177 }
178
179 function vocabindex_form_admin_refresh_submit($form, &$form_state)
180 {
181 if($form_state['clicked_button']['#id']=='edit-refresh')
182 {
183 $result=db_query("SELECT * FROM {vocabindex}");
184 while($row=db_fetch_object($result))
185 {
186 vocabindex_create_index($row->vid, $row->path);
187 }
188 menu_rebuild();
189
190 drupal_set_message(t('All the index pages have been refreshed.'));
191 }
192
193 return _vocabindex_menu_paths('admin_main');
194 }
195
196 /**
197 *Check if the given path is already being used. Returns 'vocab' if the path for an index hasn't changed, 'unused' if it isn't used or 'used' when it is in use.
198 */
199 function vocabindex_check_index($vid, $path)
200 {
201 //Check if path is already used for this vocabulary. If false, check if it's already being used by other items or nodes. If true, do nothing.
202 $count=db_result(db_query("SELECT COUNT(*) FROM {vocabindex} WHERE vid = %d AND path = '%s'", $vid, $path));
203 if($count==0)
204 {
205 //Check for existing aliases and menu paths
206 $count=db_result(db_query("SELECT COUNT(*) FROM {menu_links} WHERE link_path LIKE '%s'", $path));
207 if(drupal_lookup_path('source', $path)==TRUE || $count>0)
208 {
209 $message='used';
210 }
211 else
212 {
213 $message='unused';
214 }
215 }
216 else
217 {
218 $message='vocab';
219 }
220
221 return $message;
222 }
223
224 /**
225 *Creates or deletes the paths for the index pages
226 */
227 function vocabindex_create_index($vid, $path)
228 {
229 //Delete the old vocabindex path
230 vocabindex_delete_index(NULL, $vid);
231
232 if($vid && $path)
233 {
234 //Create the new path
235 $vocab=taxonomy_vocabulary_load($vid);
236 $description=$vocab->description;
237 $title=$vocab->name;
238 $path=strtolower($path);
239
240 db_query("INSERT INTO {vocabindex} (vid, path) VALUES (%d, '%s')", $vid, $path);
241
242 //Rebuild the menu
243 menu_rebuild();
244 }
245 }
246
247 /**
248 *Deletes index pages.
249 */
250 function vocabindex_delete_index($path=NULL, $vid=NULL)
251 {
252 if(!$path && $vid)
253 {
254 $path=db_result(db_query("SELECT path FROM {vocabindex} WHERE vid = %d", $vid));
255 }
256
257 //Delete the index page
258 db_query("DELETE FROM {vocabindex} WHERE path = '%s'", $path);
259
260 _vocabindex_menu_delete_item($path);
261 }
262
263 /**
264 *Deletes menu items by force. Some parts are a rough copy of _menu_delete_item(), but without the check for System links or updated items.
265 */
266 function _vocabindex_menu_delete_item($path)
267 {
268 //Select the menu item that matches the path.
269 $result=db_query("SELECT * FROM {menu_links} WHERE link_path = '%s'", $path);
270 while($link=db_fetch_array($result))
271 {
272 //Check for child elements and append them to their new parent
273 if($link['has_children'])
274 {
275 $result=db_query("SELECT mlid FROM {menu_links} WHERE plid = %d", $link['mlid']);
276 while($m=db_fetch_array($result))
277 {
278 $child=menu_link_load($m['mlid']);
279 $child['plid']=$link['plid'];
280 menu_link_save($child);
281 }
282 }
283 db_query("DELETE FROM {menu_links} WHERE mlid = %d", $link['mlid']);
284
285 // Update the has_children status of the parent.
286 _menu_update_parental_status($link);
287 menu_cache_clear($link->menu_name);
288 _menu_clear_page_cache();
289 }
290
291 }

  ViewVC Help
Powered by ViewVC 1.1.2