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

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

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


Revision 1.3 - (show annotations) (download) (as text)
Thu Oct 29 13:52:05 2009 UTC (3 weeks, 6 days ago) by njt1982
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-7--1
Changes since 1.2: +8 -8 lines
File MIME type: text/x-php
Added a specific Field Template + some revised the Setting paths
1 <?php
2
3 /**
4 * Settings page form
5 *
6 * This function provides the module settings page form which is used for enabling content type blocks.
7 */
8 function relevant_content_admin() {
9 $settings = variable_get('relevant_content', array());
10
11 $content_types = node_type_get_names();
12 $vocabularies = taxonomy_get_vocabularies();
13
14 $variables = array(
15 'header' => array(t('Block ID'), t('Enabled Types'), t('Enabled Vocabularies'), t('Max Results'), t('Actions')),
16 'rows' => array(),
17 );
18 if (empty($settings)) {
19 $variables['rows'][] = array(array('colspan' => 5, 'data' => t('There are no configured blocks yet - add one using the form below.')));
20 }
21 else {
22 foreach ($settings as $delta => $info) {
23 array_walk($info['types'], '_relevant_content_array_map_key_to_values', $content_types);
24 array_walk($info['vocabs'], '_relevant_content_array_map_key_to_values', $vocabularies);
25
26 $ops = array(
27 l(t('Edit'), 'admin/config/relevant_content/blocks/'. $delta),
28 l(t('Delete'), 'admin/config/relevant_content/blocks/'. $delta .'/delete'),
29 );
30
31 $variables['rows'][] = array(
32 check_plain($delta),
33 theme('item_list', array('items' => $info['types'])),
34 theme('item_list', array('items' => $info['vocabs'])),
35 check_plain($info['limit']),
36 theme('item_list', array('items' => $ops)),
37 );
38 }
39 }
40
41 // Render the output into an array and return.
42 $output['blocks'] = array('#type' => 'markup', '#markup' => theme('table', $variables));
43 $output['new_block'] = drupal_get_form('relevant_content_admin_block_form');
44
45 return $output;
46 }
47
48
49 /**
50 * Form API based function which generates the add AND edit forms. The form is an 'edit' form if a delta is passed in for editing.
51 *
52 * @param $delta
53 * mixed - The 'delta' (or ID) of the block being editted. Will effectively be the array key in the $settings array.
54 */
55 function relevant_content_admin_block_form($form, &$form_state, $delta = NULL) {
56 $settings = variable_get('relevant_content', array());
57 $form = array();
58
59 $form['settings'] = array(
60 '#type' => 'fieldset',
61 '#title' => isset($delta) ? t('Edit Block') : t('Add Block'),
62 '#tree' => TRUE,
63 '#collapsible' => !isset($delta),
64 '#collapsed' => !isset($delta),
65 );
66
67 if (!isset($delta)) {
68 $form['settings']['delta'] = array(
69 '#type' => 'textfield',
70 '#title' => t('Block <em>Delta</em>'),
71 '#description' => t('The <em>Delta</em> is used to uniquely identify a block. Please user uppercase & lowercase characters, numbers and underscores only'),
72 '#required' => TRUE,
73 '#maxlength' => 12,
74 '#size' => 12,
75 );
76 }
77 else {
78 $form['settings']['delta'] = array(
79 '#type' => 'value',
80 '#value' => $delta,
81 );
82 $form['settings']['delta_field'] = array(
83 '#type' => 'item',
84 '#title' => t('Block <em>Delta</em>'),
85 '#description' => t('The <em>Delta</em> is used to uniquely identify a block'),
86 '#markup' => check_plain($delta),
87 );
88 }
89
90 $form['settings']['types'] = array(
91 '#type' => 'checkboxes',
92 '#title' => t('Enabled Content Types'),
93 '#description' => t('Check the content types you would like to search for.'),
94 '#options' => node_type_get_names(),
95 '#default_value' => isset($delta) ? $settings[$delta]['types'] : array(),
96 );
97
98
99 $vocabs = array();
100 foreach (taxonomy_get_vocabularies() as $vid => $voc) {
101 $vocabs[$vid] = $voc->name;
102 }
103
104 $form['settings']['vocabs'] = array(
105 '#type' => 'checkboxes',
106 '#title' => t('Enabled Vocabularies'),
107 '#description' => t('Check the vocabularies you would like to search for'),
108 '#options' => $vocabs,
109 '#default_value' => isset($delta) ? $settings[$delta]['vocabs'] : array(),
110 );
111
112 $form['settings']['limit'] = array(
113 '#type' => 'textfield',
114 '#title' => t('Limit'),
115 '#description' => t('What is the maximum number of results would you like returned?'),
116 '#size' => 2,
117 '#maxlength' => 2,
118 '#required' => TRUE,
119 '#default_value' => isset($delta) ? $settings[$delta]['limit'] : 5,
120 );
121
122 $form['settings']['header_text'] = array(
123 '#type' => 'textarea',
124 '#title' => t('Header Text'),
125 '#description' => t('Optionally provide some text to appear above the listing'),
126 '#rows' => 3,
127 '#default_value' => isset($delta) ? $settings[$delta]['header_text'] : '',
128 );
129
130 $form['settings']['token_settings'] = array(
131 '#type' => 'textfield',
132 '#title' => t('Token pattern'),
133 '#description' => t('Optionally define a token pattern here to override the default output. Please use plain text only. Leave blank to use hyper links to nodes.'),
134 '#default_value' => isset($delta) ? $settings[$delta]['token_settings'] : '',
135 );
136
137 $form['settings']['token_help'] = array(
138 '#type' => 'fieldset',
139 '#title' => t('Token Help'),
140 '#collapsible' => TRUE,
141 '#collapsed' => TRUE,
142 );
143 $form['settings']['token_help']['body'] = array(
144 '#type' => 'markup',
145 '#markup' => theme('relevant_content_token_help'),
146 );
147
148 $form['settings']['op'] = array(
149 '#type' => 'value',
150 '#value' => isset($delta) ? 'edit' : 'add',
151 );
152 $form['settings']['submit'] = array(
153 '#type' => 'submit',
154 '#value' => t('Save Settings'),
155 );
156
157 return $form;
158 }
159
160
161 /**
162 * Validate function for the above form
163 */
164 function relevant_content_admin_block_form_validate($form, &$form_state) {
165 if ($form_state['values']['settings']['op'] == 'add') {
166 if (preg_match('#[^a-zA-Z0-9_]#', $form_state['values']['settings']['delta'])) {
167 form_set_error('settings][delta', t('Invalid character detected. Please only use uppercase and lowercase characters, numbers and underscores'));
168 return;
169 }
170
171 $settings = variable_get('relevant_content', array());
172 if (isset($settings[$form_state['values']['settings']['delta']])) {
173 form_set_error('settings][delta', t('This <em>Delta</em> has already been used, please try another'));
174 }
175 }
176
177 if (!is_numeric($form_state['values']['settings']['limit']) || $form_state['values']['settings']['limit'] <= 0) {
178 form_set_error('settings][limit', t('The limit must be a positive numeric value, eg 5.'));
179 }
180
181 $a = array_filter($form_state['values']['settings']['types']);
182 if (empty($a)) {
183 form_set_error('settings][types', t('You must select at least one content type to display.'));
184 }
185
186 $a = array_filter($form_state['values']['settings']['vocabs']);
187 if (empty($a)) {
188 form_set_error('settings][vocabs', t('You must select at least one vocabulary to limit your term searching to.'));
189 }
190 }
191
192
193 /**
194 * Submit handler for the block add/edit form.
195 */
196 function relevant_content_admin_block_form_submit($form, &$form_state) {
197 $settings = variable_get('relevant_content', array());
198
199 $settings[$form_state['values']['settings']['delta']] = array(
200 'types' => array_filter($form_state['values']['settings']['types']),
201 'vocabs' => array_filter($form_state['values']['settings']['vocabs']),
202 'limit' => (int)$form_state['values']['settings']['limit'],
203 'header_text' => $form_state['values']['settings']['header_text'],
204 'token_settings' => $form_state['values']['settings']['token_settings'],
205 );
206
207 variable_set('relevant_content', $settings);
208 $form_state['redirect'] = 'admin/config/relevant_content/blocks';
209 }
210
211
212 /**
213 * Menu callback for deleting a variable. Does some basic validation first before calling the Form API.
214 */
215 function relevant_content_admin_delete($delta) {
216 $settings = variable_get('relevant_content', array());
217
218 return drupal_get_form('relevant_content_admin_delete_confirm', $delta);
219 }
220
221
222 /**
223 * Form API based function for geneating the confirmation page for deleting a block from the settings.
224 */
225 function relevant_content_admin_delete_confirm($form, &$form_state, $delta) {
226 $form = array();
227
228 $form['delta'] = array(
229 '#type' => 'hidden',
230 '#value' => $delta
231 );
232
233 return confirm_form(
234 $form,
235 t('Are you sure you want to delete block "%delta"?', array('%delta' => $delta)),
236 'admin/config/relevant_content/blocks',
237 t('Note: This action cannt be undone')
238 );
239 }
240
241
242 /**
243 * Delete confirmation form submission handler. Usets the block settings from the settings array based on the hidden delta field passed in from the previous funciton.
244 */
245 function relevant_content_admin_delete_confirm_submit($form, &$form_state) {
246 $settings = variable_get('relevant_content', array());
247
248 // Remove the settings from the array and save
249 unset($settings[$form_state['values']['delta']]);
250 variable_set('relevant_content', $settings);
251
252 // Tidy up the blocks table...
253 foreach (array('block', 'block_node_type', 'block_role') as $table) {
254 db_delete($table)
255 ->condition('module', 'relevant_content')
256 ->condition('delta', $form_state['values']['delta'])
257 ->execute();
258 }
259
260 $form_state['redirect'] = 'admin/config/relevant_content/blocks';
261 }
262
263
264

  ViewVC Help
Powered by ViewVC 1.1.2