/[drupal]/contributions/modules/reptag/reptag_admin_table.inc
ViewVC logotype

Contents of /contributions/modules/reptag/reptag_admin_table.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Jun 4 12:21:06 2008 UTC (17 months, 3 weeks ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
- task: initial version for Drupal 6
1 <?php
2 // $Id: reptag_admin_table.inc,v 1.0 2007/07/25 16:14:31 profix898 Exp $
3
4 define('RETAG_PAGER_LENGTH', 2);
5
6 /**
7 * Function _reptag_admin_site_form().
8 * (tags administration: site-wide tags)
9 */
10 function _reptag_admin_site_form(&$form_state) {
11 $form['site'] = array(
12 '#type' => 'fieldset',
13 '#title' => t('SiteWide - Rep[lacement]Tags'),
14 '#collapsible' => FALSE,
15 '#collapsed' => FALSE
16 );
17 $form['site'][] = _reptag_admin_tags_form($form_state);
18
19 return _reptag_admin_form($form);
20 }
21
22 /**
23 * Function _reptag_admin_user_form().
24 * (tags administration: user tags)
25 */
26 function _reptag_admin_user_form(&$form_state) {
27 global $user;
28 $form['user'] = array(
29 '#type' => 'fieldset',
30 '#title' => t('User - Rep[lacement]Tags'),
31 '#collapsible' => FALSE,
32 '#collapsed' => FALSE
33 );
34 $form['user'][] = _reptag_admin_tags_form($form_state, $user->uid);
35
36 return _reptag_admin_form($form);
37 }
38
39 /**
40 * Function _reptag_admin_table_form().
41 * (tags administration: table form)
42 */
43 function _reptag_admin_tags_form(&$form_state, $uid = 0, $lang = NULL) {
44 // Language switcher
45 $lang = isset($lang) ? $lang : arg(4);
46 $languages = language_list('enabled');
47 $lang = array_key_exists($lang, $languages[1]) ? $lang : language_default('language');
48 $form['headbar'] = array('#theme' => 'reptag_table_headbar');
49 if (variable_get('reptag_locale_enable', 0)) {
50 foreach ($languages[1] as $language) {
51 $links[$language->language] = l(
52 $language->native,
53 'admin/settings/reptag/'. ($uid ? 'user' : 'site') . (($language->language == language_default('language')) ? '' : '/'. $language->language)
54 );
55 }
56 $form['headbar'] = array(
57 '#value' => theme('submenu', $links),
58 '#prefix' => '<div class=\'reptag-headbar\'>',
59 '#suffix' => '</div>',
60 );
61 }
62
63 // Include JS (for js-based tags administration)
64 if (variable_get('reptag_javascript', 1)) {
65 drupal_add_js('misc/jquery.form.js');
66 drupal_add_js(drupal_get_path('module', 'reptag') .'/js/jquery.blockUI.js');
67 $settings = array(
68 'mode' => $uid ? 'user' : 'site',
69 'language' => isset($lang) ? $lang : ''
70 );
71 drupal_add_js(array('reptag' => $settings), 'setting');
72 }
73
74 // Include the actual table
75 $form = array_merge($form, _reptag_admin_table_builder($form_state, $uid, $lang));
76 $form['tags']['#prefix'] = '<div id=\'reptag-tags-admin-wrapper\'>';
77 $form['tags']['#suffix'] = '</div>';
78
79 $form['addbtn'] = array(
80 '#type' => 'image_button',
81 '#title' => t('Add'),
82 '#src' => drupal_get_path('module', 'reptag') .'/images/add.png',
83 '#attributes' => array('class' => 'reptag-tags-add'),
84 '#submit' => array('_reptag_admin_tags_nojs'),
85 '#url' => 'admin/settings/reptag/nojs/tagadd/'. ($uid ? 'user' : 'site')
86 );
87
88 return $form;
89 }
90
91 /**
92 * Function _reptag_admin_table_builder().
93 */
94 function _reptag_admin_table_builder(&$form_state, $uid = 0, $lang = NULL) {
95 $form['tags'] = array(
96 '#tree' => TRUE,
97 '#parents' => array()
98 );
99 $form['tags']['table'] = array(
100 '#theme' => 'reptag_table',
101 '#header' => array(t('Tag'), '', t('Replacement'), '', ''),
102 '#empty' => t('There are currently no tags defined.'),
103 '#attributes' => array('id' => 'reptag-tags-admin-table')
104 );
105
106 $multilang = variable_get('reptag_locale_enable', 0) && (variable_get('language_count', 1) >= 2);
107
108 // Load tags table
109 $reptag_table = _reptag_table_load(array($uid), REPTAG_LOADTAG_EDIT, $lang, RETAG_PAGER_LENGTH);
110 foreach ($reptag_table as $rtid => $item) {
111 $form['tags']['table'][$rtid]['tag'] = array(
112 '#type' => 'textfield',
113 '#default_value' => $item['tag'],
114 '#size' => 20,
115 '#maxlength' => 255,
116 '#attributes' => array('readonly' => 'readonly')
117 );
118 $form['tags']['table'][$rtid]['arrow'] = array(
119 '#value' => '<img src="'. base_path() . drupal_get_path('module', 'reptag') .'/images/arrow.png" alt="" />'
120 );
121 if (variable_get('reptag_textarea', 1)) {
122 $form['tags']['table'][$rtid]['replacement'] = array(
123 '#type' => 'textarea',
124 '#default_value' => $item['replacement'],
125 '#rows' => 1,
126 '#cols' => 60,
127 '#maxlength' => 1024,
128 '#attributes' => $multilang ? array('readonly' => 'readonly') : array()
129 );
130 }
131 else {
132 $form['tags']['table'][$rtid]['replacement'] = array(
133 '#type' => 'textfield',
134 '#default_value' => $item['replacement'],
135 '#size' => 65,
136 '#maxlength' => 1024,
137 '#attributes' => $multilang ? array('readonly' => 'readonly') : array()
138 );
139 }
140 $form['tags']['table'][$rtid]['edit'] = array(
141 '#type' => 'image_button',
142 '#title' => t('Edit'),
143 '#src' => drupal_get_path('module', 'reptag') .'/images/edit.png',
144 '#attributes' => array('class' => 'reptag-tags-edit'),
145 '#submit' => $multilang ? array('_reptag_admin_tags_nojs') : array('_reptag_admin_tags_edit'),
146 '#url' => 'admin/settings/reptag/nojs/tagedit/'. $rtid,
147 '#rtid' => $rtid
148 );
149 $form['tags']['table'][$rtid]['delete'] = array(
150 '#type' => 'image_button',
151 '#title' => t('Delete'),
152 '#src' => drupal_get_path('module', 'reptag') .'/images/delete.png',
153 '#attributes' => array('class' => 'reptag-tags-delete'),
154 '#submit' => array('_reptag_admin_tags_delete'),
155 '#rtid' => $rtid
156 );
157 }
158
159 if (count($reptag_table)) {
160 $form['tags']['pager'] = array('#value' => theme('pager', array(), RETAG_PAGER_LENGTH));
161 }
162
163 $form['uid'] = array('#type' => 'value', '#value' => $uid);
164
165 return $form;
166 }
167
168 /**
169 * Function _reptag_admin_addedit_form().
170 * (tags administration: add/edit tags)
171 */
172 function _reptag_admin_addedit_form(&$form_state, $rtid = NULL) {
173 //
174 $type = 'add';
175 $title = t('Add');
176 //
177 if (isset($rtid)) {
178 $type = 'edit';
179 $title = t('Edit');
180 $reptag = _reptag_table_load_tag($rtid, REPTAG_LOADTAG_EDIT);
181 }
182
183 $form[$type] = array(
184 '#tree' => TRUE,
185 '#parents' => array()
186 );
187
188 $multilang = variable_get('reptag_locale_enable', 0) && (variable_get('language_count', 1) >= 2);
189 if (!$multilang) {
190 $form[$type]['table'] = array(
191 '#theme' => 'reptag_table',
192 '#header' => array(t('Tag'), '', t('Replacement')),
193 '#attributes' => array('id' => 'reptag-tags-admin-'. $type)
194 );
195 $form[$type]['table'][$type]['tag'] = array(
196 '#type' => 'textfield',
197 '#default_value' => isset($reptag) ? $reptag['tag'] : '',
198 '#size' => 20,
199 '#maxlength' => 255
200 );
201 $form[$type]['table'][$type]['arrow'] = array(
202 '#value' => '<img src="'. base_path() . drupal_get_path('module', 'reptag') .'/images/arrow.png" alt="" />'
203 );
204 if (variable_get('reptag_textarea', 1)) {
205 $form[$type]['table'][$type]['replacement'] = array(
206 '#type' => 'textarea',
207 '#default_value' => isset($reptag) ? $reptag['replacements']['en']['replacement'] : '',
208 '#rows' => 3,
209 '#cols' => 60,
210 '#maxlength' => 1024
211 );
212 }
213 else {
214 $form[$type]['table'][$type]['replacement'] = array(
215 '#type' => 'textfield',
216 '#default_value' => isset($reptag) ? $reptag['replacements']['en']['replacement'] : '',
217 '#size' => 65,
218 '#maxlength' => 1024
219 );
220 }
221 $form[$type]['table'][$type]['rtid'] = array(
222 '#type' => 'value',
223 '#value' => isset($reptag) ? $reptag['replacements']['en']['rtid'] : '',
224 );
225 }
226 else {
227 $form[$type]['table'] = array('#theme' => 'reptag_table_multi');
228 $form[$type]['table']['tag']['tag']['label'] = array('#value' => t('Tag'));
229 $form[$type]['table']['tag']['tag']['arrow'] = array(
230 '#value' => '<img src="'. base_path() . drupal_get_path('module', 'reptag') .'/images/arrow.png" alt="" />'
231 );
232 $form[$type]['table']['tag']['tag']['tag'] = array(
233 '#type' => 'textfield',
234 '#default_value' => isset($reptag) ? $reptag['tag'] : '',
235 '#size' => 20,
236 '#maxlength' => 255,
237 '#parents' => array('table', $type, 'tag')
238 );
239 $languages = language_list('enabled');
240 foreach ($languages[1] as $language) {
241 $form[$type]['table']['replacements']['#section'] = t('Replacements');
242 $form[$type]['table']['replacements'][$language->language]['label'] = array(
243 '#value' => $language->native
244 );
245 $form[$type]['table']['replacements'][$language->language]['arrow'] = array(
246 '#value' => '<img src="'. base_path() . drupal_get_path('module', 'reptag') .'/images/arrow.png" alt="" />'
247 );
248 if (variable_get('reptag_textarea', 1)) {
249 $form[$type]['table']['replacements'][$language->language]['replacement'] = array(
250 '#type' => 'textarea',
251 '#default_value' => isset($reptag['replacements'][$language->language]) ? $reptag['replacements'][$language->language]['replacement'] : '',
252 '#rows' => 3,
253 '#cols' => 60,
254 '#maxlength' => 1024,
255 '#parents' => array('table', $type, 'replacements', $language->language, 'replacement')
256 );
257 }
258 else {
259 $form[$type]['table']['replacements'][$language->language]['replacement'] = array(
260 '#type' => 'textfield',
261 '#default_value' => isset($reptag['replacements'][$language->language]) ? $reptag['replacements'][$language->language]['replacement'] : '',
262 '#size' => 65,
263 '#maxlength' => 1024,
264 '#parents' => array('table', $type, 'replacements', $language->language, 'replacement')
265 );
266 }
267 $form[$type]['table']['replacements'][$language->language]['rtid'] = array(
268 '#type' => 'value',
269 '#value' => isset($reptag['replacements'][$language->language]) ? $reptag['replacements'][$language->language]['rtid'] : '',
270 '#parents' => array('table', $type, 'replacements', $language->language, 'rtid')
271 );
272 }
273 }
274
275 $form['uid'] = array('#type' => 'value', '#value' => $reptag['uid']);
276 $form[$type .'btn'] = array(
277 '#type' => 'image_button',
278 '#title' => $title,
279 '#src' => drupal_get_path('module', 'reptag') .'/images/'. $type .'.png',
280 '#attributes' => array('class' => 'reptag-tags-'. $type),
281 '#submit' => array('_reptag_admin_tags_'. $type)
282 );
283
284 return $form;
285 }
286
287 /**
288 * Function _reptag_admin_tags_nojs().
289 */
290 function _reptag_admin_tags_nojs($form, &$form_state) {
291 if (isset($form_state['clicked_button']['#url'])) {
292 drupal_goto($form_state['clicked_button']['#url'], drupal_get_destination());
293 }
294 }
295
296 /**
297 * Function _reptag_admin_tags_save().
298 */
299 function _reptag_admin_tags_save($form, &$form_state) {
300 _reptag_admin_tags_add($form, &$form_state);
301 _reptag_admin_tags_edit($form, &$form_state);
302 }
303
304 /**
305 * Function _reptag_admin_tags_reset().
306 */
307 function _reptag_admin_tags_reset($form, &$form_state) {
308 _reptag_table_clear($form_state['values']['uid']);
309 }
310
311 /**
312 * Function _reptag_admin_tags_add().
313 */
314 function _reptag_admin_tags_add($form, &$form_state) {
315 if (isset($form_state['values']['table']['add']) && !empty($form_state['values']['table']['add']['tag'])) {
316 $reptag = $form_state['values']['table']['add'];
317 if (isset($reptag['replacement'])) {
318 _reptag_table_add('#\$'. strtoupper($reptag['tag']) .'\$#si', $reptag['replacement'], $form_state['values']['uid']);
319 }
320 else {
321 foreach ($reptag['replacements'] as $lang => $replacement) {
322 _reptag_table_add('#\$'. strtoupper($reptag['tag']) .'\$#si', $replacement['replacement'], $form_state['values']['uid'], $lang);
323 }
324 }
325 }
326 }
327
328 /**
329 * Function _reptag_admin_tags_edit().
330 */
331 function _reptag_admin_tags_edit($form, &$form_state) {
332 if (isset($form_state['values']['table'])) {
333 foreach ($form_state['values']['table'] as $key => $reptag) {
334 $reptag['rtid'] = isset($reptag['rtid']) ? $reptag['rtid'] : $key;
335 if (isset($reptag['replacement'])) {
336 _reptag_table_update($reptag['rtid'], '#\$'. strtoupper($reptag['tag']) .'\$#si', $reptag['replacement'], $form_state['values']['uid']);
337 }
338 else {
339 foreach ($reptag['replacements'] as $lang => $replacement) {
340 if ($replacement['rtid']) {
341 if (empty($reptag['tag']) || empty($replacement['replacement'])) {
342 _reptag_table_del($replacement['rtid']);
343 }
344 else {
345 _reptag_table_update($replacement['rtid'], '#\$'. strtoupper($reptag['tag']) .'\$#si', $replacement['replacement'], $form_state['values']['uid'], $lang);
346 }
347 }
348 else {
349 _reptag_table_add('#\$'. strtoupper($reptag['tag']) .'\$#si', $replacement['replacement'], $form_state['values']['uid'], $lang);
350 }
351 }
352 }
353 }
354 }
355 }
356
357 /**
358 * Function _reptag_admin_tags_delete().
359 */
360 function _reptag_admin_tags_delete($form, &$form_state) {
361 if (isset($form_state['clicked_button']['#rtid'])) {
362 _reptag_table_del($form_state['clicked_button']['#rtid']);
363 }
364 }
365
366 /**
367 * Function theme_reptag_table_multi().
368 */
369 function theme_reptag_table_multi($form) {
370 $header = isset($form['#header']) ? $form['#header'] : array();
371 $attributes = isset($form['#attributes']) ? $form['#attributes'] : array();
372
373 $rows = array();
374 foreach (element_children($form) as $section) {
375 if (isset($form[$section]['#section'])) {
376 $rows[] = array(array('data' => check_plain($form[$section]['#section']), 'class' => 'region', 'colspan' => count($header)));
377 }
378 foreach (element_children($form[$section]) as $key) {
379 $row = array();
380 foreach (element_children($form[$section][$key]) as $item) {
381 $row[] = drupal_render($form[$section][$key][$item]);
382 }
383 $rows[] = $row;
384 }
385 }
386
387 return count($rows) ? theme('table', $header, $rows, $attributes) : '';
388 }

  ViewVC Help
Powered by ViewVC 1.1.2