/[drupal]/contributions/modules/nodewords_bypath/nodewords_bypath.forms.inc
ViewVC logotype

Diff of /contributions/modules/nodewords_bypath/nodewords_bypath.forms.inc

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

revision 1.1, Tue Jun 17 18:36:25 2008 UTC revision 1.1.2.1, Tue Jun 17 18:36:25 2008 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file The form logic for the meta tag by path module.
6     */
7    
8    
9    /**
10     * Build the settings form for the module.
11     *
12     * @return array The form.
13     */
14    function nodewords_bypath_admin_overview() {
15      $form  = array();
16      $rules = _nodewords_bypath_get_all();
17    
18      foreach ($rules as $rule) {
19        $form[$rule->id]['id'] = array(
20          '#type'  => 'value',
21          '#value' => $rule->id,
22        );
23    
24        $form[$rule->id]['name']   = array('#value' => $rule->name);
25        $form[$rule->id]['weight'] = array('#value' => $rule->weight);
26        $form[$rule->id]['edit']   = array('#value' => l(t('Edit'), 'admin/content/nodewords/path/edit/'. $rule->id));
27        $form[$rule->id]['delete'] = array('#value' => l(t('Delete'), 'admin/content/nodewords/path/delete/'. $rule->id));
28      }
29    
30      return $form;
31    }
32    
33    
34    /**
35     * Generate the form for creating a new custom title rule or editing an
36     * existing one.
37     *
38     * @param $id int The ID of an existing title pattern to edit. If specified,
39     *        this title pattern will be used to populate the form fields.
40     *
41     * @return array The form.
42     */
43    function nodewords_bypath_create_form($id = -1) {
44      $form = array();
45    
46      $existing = ($id > -1) ? _nodewords_bypath_load_instance($id) : NULL;
47      if ($existing !== NULL) {
48        $existing->tags = _nodewords_bypath_get_tags_for($id);
49      }
50    
51      //------------------------------------------------------------------------
52      // Basic Info
53      $form['meta_rule'] = array(
54        '#type'        => 'fieldset',
55        '#title'       => t('Meta Tag Rule'),
56        '#collapsible' => TRUE,
57        '#weight'      => -5,
58      );
59    
60      $form['meta_rule']['rule_id'] = array(
61        '#type'  => 'value',
62        '#value' => ($existing !== NULL) ? $existing->id : -1,
63      );
64    
65      $form['meta_rule']['name'] = array(
66        '#type'          => 'textfield',
67        '#title'         => t('Name'),
68        '#default_value' => ($existing !== NULL) ? $existing->name : '',
69        '#description'   => t('A short name to help you identify this rule when it\'s shown in the list view.'),
70      );
71    
72      $form['meta_rule']['weight'] = array(
73        '#type'        => 'select',
74        '#title'       => t('Weight'),
75        '#default_value' => ($existing !== NULL) ? $existing->weight : 0,
76        '#options'     => drupal_map_assoc(range(-10, 10)),
77        '#description' => t('This determines the order that rules are evaluated. Rules with lower weight values are evaluated before rules with higher weights. Evaluation stops at the first rule that matches the path.'),
78      );
79    
80      $options = array(
81        1 => t('Apply rule only to the listed pages.'),
82        2 => t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).'),
83      );
84    
85      $form['meta_rule']['type'] = array(
86        '#type'        => 'radios',
87        '#title'       => t('Rule Application'),
88        '#default_value' => ($existing !== NULL) ? $existing->type : 1,
89        '#options'     => $options,
90        '#description' => t('Determines the method used apply the meta tag rule.'),
91      );
92    
93      //------------------------------------------------------------------------
94      // Path information.
95      $subs = array(
96        '%blog'          => 'blog',
97        '%blog-wildcard' => 'blog/*',
98        '%front'         => '<front>',
99        '%php'           => '<?php ?>'
100      );
101    
102      $description = t('Enter one page per line as Drupal paths. The "*" character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', $subs);
103    
104      $form['meta_rule']['paths'] = array(
105          '#type'          => 'textarea',
106          '#title'         => t('Path(s)'),
107          '#default_value' => ($existing !== NULL) ? $existing->path_expr : '',
108          '#description'   => $description,
109      );
110    
111      //------------------------------------------------------------------------
112      // The tags themsevles
113      $subs = array('%none' => NODEWORDS_NONE, '%robots' => 'robots', '%keywords' => 'keywords');
114      $description = t('The values in these fields will be assigned to the corresponding tags when a visitor loads a URI matching the path above. Leave a field blank to use the global default for that tag, or enter %none to prevent the tag from being output. Note that the %node value will not force the %robots or %keywords tags.', $subs);
115      $form['meta_tags'] = array(
116        '#type'        => 'fieldset',
117        '#title'       => t('Meta Tags'),
118        '#description' => $description,
119        '#collapsible' => TRUE,
120        '#weight'      => -5,
121      );
122    
123      $settings = _nodewords_get_settings();
124    
125      foreach (_nodewords_get_possible_tags() as $tag) {
126        $function = 'nodewords_'. $tag .'_form';
127        if ($settings['edit'][$tag] && function_exists($function)) {
128          $default = ($existing !== NULL) ? $existing->tags[$tag] : '';
129          $element = $function('page', $default, $settings);
130          if ($element) {
131            $form['meta_tags']['meta_tag_' . $tag] = $element;
132          }
133        }
134      }
135    
136      //------------------------------------------------------------------------
137      $form['submit'] = array(
138        '#type'  => 'submit',
139        '#value' => ($existing !== NULL) ? t('Update Rule') : t('Save Rule'),
140      );
141    
142      $form['cancel'] = array(
143        '#type'  => 'submit',
144        '#value' => t('Cancel'),
145      );
146    
147      return $form;
148    }
149    
150    
151    /**
152     * Save the data from the pattern editing/creation form.
153     *
154     * @param $form_id mixed The ID of the form.
155     * @param $form_values array The form values.
156     */
157    function nodewords_bypath_create_form_submit($form_id, $form_values) {
158      // -------------------------------------------------------------------------
159      // Bail out on cancel.
160      if ($form_values['op'] == t('Cancel')) {
161        return 'admin/content/nodewords/path';
162      }
163    
164      // -------------------------------------------------------------------------
165      // Build the rule object and save it.
166      $rule            = new stdClass;
167      $rule->id        = $form_values['rule_id'];
168      $rule->name      = trim($form_values['name']);
169      $rule->path_expr = trim($form_values['paths']);
170      $rule->weight    = intval($form_values['weight']);
171      $rule->type      = intval($form_values['type']);
172      $rule->tags      = array();
173    
174      $viewable_tags = _nodewords_get_viewable_tags();
175    
176      foreach ($viewable_tags as $tag_name) {
177        $rule->tags[$tag_name] = trim($form_values['meta_tag_' . $tag_name]);
178      }
179    
180      _nodewords_bypath_save($rule);
181      if ($rule->id > -1) {
182        drupal_set_message(t('Updated meta tag rule %name', array('%name' => $rule->name)));
183      }
184      else {
185        drupal_set_message(t('', array('%name' => $rule->name)));
186      }
187    
188      return 'admin/content/nodewords/path';
189    }
190    
191    
192    /**
193     * Generate the confirmation form for deleting a title.
194     *
195     * @param $id int The ID of the rule to delete.
196     *
197     * @return array The form.
198     */
199    function nodewords_bypath_delete_form($id) {
200      $form = array();
201    
202      $existing = _nodewords_bypath_load_instance($id);
203    
204      $desc = t('You are about to remove the meta tag rule "%name".',
205                array('%name' => $existing->name,));
206      $form['title_pattern'] = array(
207        '#type'        => 'fieldset',
208        '#title'       => t('Confirm Delete'),
209        '#description' => $description,
210        '#collapsible' => FALSE,
211      );
212    
213      $form['title_pattern']['warning'] = array(
214        '#value'  => '<strong>' . t('This action cannot be undone.') . '</strong>',
215        '#prefix' => '<br />',
216      );
217    
218      $form['pattern_id'] = array(
219        '#type'  => 'value',
220        '#value' => $id,
221      );
222    
223      $form['pattern_name'] = array(
224        '#type'  => 'value',
225        '#value' => $existing->name,
226      );
227    
228      $form['submit'] = array(
229        '#type'  => 'submit',
230        '#value' => t('Delete'),
231      );
232    
233      $form['cancel'] = array(
234        '#type'  => 'submit',
235        '#value' => t('Cancel'),
236      );
237    
238      return $form;
239    }
240    
241    
242    /**
243     * Handle the delete form submission.
244     *
245     * @param $form_id mixed The form's ID.
246     * @param $form_values array The form values.
247     *
248     * @return string The path to send the browser to after the form is
249     *         submitted.
250     */
251    function nodewords_bypath_delete_form_submit($form_id, $form_values) {
252      if ($form_values['op'] == t('Delete')) {
253        $id = $form_values['pattern_id'];
254    
255        db_query('DELETE FROM {nodewords_bypath_rules} WHERE id = %d', $id);
256        db_query('DELETE FROM {nodewords_bypath_tags} WHERE id = %d', $id);
257    
258        drupal_set_message(t('Meta tag path rule "%name" has been deleted.', array('%name' => $form_values['pattern_name'])));
259      }
260    
261      return 'admin/content/nodewords/path';
262    }
263    
264    
265    /**
266     * Generate the token reference.
267     *
268     * @return array The form.
269     */
270    function nodewords_bypath_tokenref_form() {
271      $form = array();
272    
273      $tokens = token_get_list();
274    
275      // -------------------------------------------------------------------------
276      // The global tokens section
277      $form['global_tokens'] = array(
278        '#type'        => 'fieldset',
279        '#title'       => t('Global Tokens'),
280        '#collapsible' => TRUE,
281        '#weight'      => -5,
282        '#description' => t('Global tokens are available site wide. They may be used for meta tag rules that affect any page.'),
283      );
284    
285      foreach($tokens['global'] as $token => $description) {
286        $form['global_tokens'][$token]['left'] = array(
287          '#type'  => 'item',
288          '#value' => '[' . $token . ']',
289        );
290    
291        $form['global_tokens'][$token]['right'] = array(
292          '#type'  => 'item',
293          '#value' => $description,
294        );
295      }
296    
297      // -------------------------------------------------------------------------
298      // The node tokens section
299      $form['node_tokens'] = array(
300        '#type'        => 'fieldset',
301        '#title'       => t('Node Tokens'),
302        '#collapsible' => TRUE,
303        '#weight'      => -3,
304        '#description' => t('Node tokens are available for page-level node presentation.'),
305      );
306    
307      foreach($tokens['node'] as $token => $description) {
308        $form['node_tokens'][$token]['left'] = array(
309          '#type'  => 'item',
310          '#value' => '[' . $token . ']',
311        );
312    
313        $form['node_tokens'][$token]['right'] = array(
314          '#type'  => 'item',
315          '#value' => $description,
316        );
317      }
318    
319      return $form;
320    }
321    
322    
323    /**
324     * Theme the token reference form.
325     *
326     * @param $form array The token reference form.
327     *
328     * @return string The themed form.
329     */
330    function theme_nodewords_bypath_tokenref_form($form) {
331      // -------------------------------------------------------------------------
332      // The global tokens.
333      $rows = array();
334      foreach ($form['global_tokens'] as $name => $element) {
335        if (isset($element['left']) && is_array($element['left'])) {
336          $rows[] = array(
337            array(
338              'data'  => drupal_render($element['left']),
339            ),
340            array(
341              'data' => drupal_render($element['right']),
342            ),
343          );
344    
345          unset($form['global_tokens'][$name]);
346        }
347      }
348    
349      $form['global_tokens']['tokens'] = array(
350        '#value' => theme('table', array(), $rows),
351      );
352    
353      // -------------------------------------------------------------------------
354      // The node tokens.
355      $rows = array();
356      foreach ($form['node_tokens'] as $name => $element) {
357        if (isset($element['left']) && is_array($element['left'])) {
358          $rows[] = array(
359            array(
360              'data' => drupal_render($element['left']),
361            ),
362            array(
363              'data' => drupal_render($element['right']),
364            ),
365          );
366    
367          unset($form['node_tokens'][$name]);
368        }
369      }
370    
371      $form['node_tokens']['tokens'] = array(
372        '#value' => theme('table', array(), $rows),
373      );
374    
375      return drupal_render($form);
376    }
377    
378    
379    /**
380     * Theme the table in the admin overview form.
381     *
382     * @param $form array The form elements to theme.
383     *
384     * @return string The rendered form elements.
385     */
386    function theme_nodewords_bypath_admin_overview($form) {
387      $rows = array();
388      foreach ($form as $name => $element) {
389        if (isset($element['name']) && is_array($element['name'])) {
390          $rows[] = array(
391            drupal_render($element['name']),
392            drupal_render($element['weight']),
393            drupal_render($element['edit']),
394            drupal_render($element['delete'])
395          );
396          unset($form[$name]);
397        }
398      }
399      $header = array(t('Name'), t('Weight'), t('Edit'), t('Delete'));
400      $output = theme('table', $header, $rows);
401      $output .= drupal_render($form);
402    
403      return $output;
404    }
405    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2