Project

General

Profile

Download (9.54 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the External Links module.
6
 */
7

    
8
/**
9
 * Page callback: Form constructor for a form to list and reorder external link templates.
10
 *
11
 * @ingroup forms
12
 * @see ext_links_menu()
13
 * @see ext_links_admin_overview_submit()
14
 */
15
function ext_links_admin_overview($form) {
16

    
17
  $link_templates = ext_links_templates();
18

    
19
  $form['#tree'] = TRUE;
20
  foreach ($link_templates as $id => $link_template) {
21
    $form['link_templates'][$id]['title'] = array('#markup' => check_plain($link_template->title));
22
    $form['link_templates'][$id]['link'] = array('#markup' => check_plain($link_template->link));
23
    $form['link_templates'][$id]['category'] = array('#markup' => check_plain($link_template->category));
24
    $form['link_templates'][$id]['configure'] = array('#type' => 'link', '#title' => t('configure'), '#href' => 'admin/config/cdm_dataportal/ext_links/' . $link_template->id);
25
    $form['link_templates'][$id]['status'] = array('#type' => 'link',
26
      '#title' => $link_template->status == 1 ? t('disable') : t('enable'),
27
      '#href' => 'admin/config/cdm_dataportal/ext_links/' . $link_template->id . '/status/' . ($link_template->status == 1 ? '0' : '1'));
28
    $form['link_templates'][$id]['weight'] = array(
29
      '#type' => 'weight',
30
      '#title' => t('Weight for @title', array('@title' => $link_template->id)),
31
      '#title_display' => 'invisible',
32
      '#default_value' => $link_template->weight,
33
      '#delta' => 100
34
    );
35
    $form['link_templates'][$id]['#attributes']['class'] = [];
36
    if($link_template->status != 1){
37
      $form['link_templates'][$id]['#attributes']['class'][] = 'disabled';
38
    }
39
  }
40
  $form['actions'] = array('#type' => 'actions');
41
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
42
  return $form;
43
}
44

    
45
/**
46
 * Form submission handler for ext_links_admin_overview().
47
 */
48
function ext_links_admin_overview_submit($form, &$form_state) {
49
  foreach ($form_state['values']['link_templates'] as $id => $data) {
50
    if (is_array($data) && isset($data['weight'])) {
51
      // Only update if this is a form element with weight.
52
      db_update('ext_links')
53
        ->fields(array('weight' => $data['weight']))
54
        ->condition('id', $id)
55
        ->execute();
56
    }
57
  }
58
  filter_formats_reset();
59
  drupal_set_message(t('The text format ordering has been saved.'));
60
}
61

    
62
/**
63
 * Page callback: Displays external link template add/edit form.
64
 *
65
 * @param object|null $link_template
66
 *   (optional) An object representing an external link template, with the following properties:
67
 *   - title: The link title
68
 *   - link: The link url template.
69
 *   - status: (optional) An integer indicating whether the ext link is
70
 *     enabled (1) or not (0). Defaults to 1.
71
 *   - weight: (optional) The weight of the external link, which controls its
72
 *     placement in external link block. If omitted, the weight is set to 0.
73
 *     Defaults to NULL.
74
 *
75
 * @return array
76
 *   A form array.
77
 *
78
 * @see filter_menu()
79
 */
80
function ext_links_admin_link_template_page($link_template = NULL) {
81
  if (!isset($link_template->id)) {
82
    drupal_set_title(t('Add external link'));
83
    $link_template = (object) array(
84
      'title' => NULL,
85
      'link' => 'https://',
86
      'weight' => 0,
87
      'status' => 1
88
    );
89
  }
90
  return drupal_get_form('ext_links_admin_link_template_form', $link_template);
91
}
92

    
93
function ext_links_admin_appearance_page(){
94

    
95
  drupal_set_title(t('Configure the appearance of external links'));
96

    
97
  $form['ext_links_options'] = array(
98
    '#type' => 'fieldset',
99
    '#title' => t('External links appearance options'),
100
    '#collapsible' => FALSE,
101
    '#collapsed' => FALSE
102
  );
103
  $form['ext_links_options']['ext_links_appearance_grouped'] = [
104
    '#type' => 'checkbox',
105
    '#title' => t('Show external links grouped by category with headings.'),
106
    '#default_value' => variable_get('ext_links_appearance_grouped', 1)
107
  ];
108

    
109
  return system_settings_form($form);
110
}
111

    
112

    
113
/**
114
 * Form constructor for the External Link add/edit form.
115
 *
116
 * @param $link_template
117
 *   A link template object having the properties:
118
 *   - id: A machine-readable name representing the ID of the external link.
119
 *     If this corresponds to an existing external link, that format will be
120
 *     updated; otherwise, a new external link will be created.
121
 *   - title: The link title
122
 *   - link: The link url template.
123
 *   - status: (optional) An integer indicating whether the ext link is
124
 *     enabled (1) or not (0). Defaults to 1.
125
 *   - weight: (optional) The weight of the external link, which controls its
126
 *     placement in external link block. If omitted, the weight is set to 0.
127
 *     Defaults to NULL.
128
 *
129
 * @see ext_links_admin_link_template_form_validate()
130
 * @see ext_links_admin_link_template_form_submit()
131
 * @ingroup forms
132
 */
133
function ext_links_admin_link_template_form($form, &$form_state, $link_template) {
134

    
135
  $form['#link_template'] = $link_template;
136
  $form['#tree'] = TRUE;
137

    
138
  $form['id'] = array(
139
    '#type' => 'machine_name',
140
    '#required' => TRUE,
141
    '#default_value' => $link_template->id,
142
    '#maxlength' => 255,
143
    '#machine_name' => array(
144
      'exists' => 'ext_links_exists',
145
    ),
146
    '#disabled' => !empty($link_template->id),
147
  );
148
  $form['category'] = array(
149
    '#type' => 'textfield',
150
    '#title' => t('Category'),
151
    '#required' => FALSE,
152
    '#default_value' => $link_template->category,
153
    '#autocomplete_path' => 'ext_links/category/autocomplete',
154
    '#maxlength' => 255,
155
  );
156
  $form['title'] = array(
157
    '#type' => 'textfield',
158
    '#title' => t('Title'),
159
    '#default_value' => $link_template->title,
160
    '#description' => t('The text of the link that will be displayed.'),
161
    '#required' => TRUE,
162
  );
163
  $token_info = cdm_tokens_token_info();
164
  $cdm_tokens = [];
165
  foreach(array_keys($token_info['tokens']['cdm']) as $token_key){
166
    $cdm_tokens[] = '[cdm:' . $token_key . ']';
167
  }
168
  $form['link'] = array(
169
    '#type' => 'textfield',
170
    '#title' => t('Link'),
171
    '#description' => t('The URL template for the link to the external resource or service.
172
        The following <i>cdm tokens</i> can be used here: <ul> !cdm_tokens </ul>',
173
      ['!cdm_tokens' => '<li><code>' . join('</code></li><li><code>', $cdm_tokens) . '</code></li>']
174
    ),
175
    '#default_value' => $link_template->link,
176
    '#maxlength' => 500,
177
    '#required' => TRUE,
178
  );
179

    
180
  $form['status'] = array(
181
    '#type' => 'checkbox',
182
    '#title' => t('Enabled'),
183
    '#default_value' => $link_template->status
184
  );
185
  $form['weight'] = array(
186
    '#type' => 'weight',
187
    '#title' => t('Weight'),
188
    '#default_value' => $link_template->weight,
189
    '#delta' => 100,
190
  );
191
  $form['actions'] = array('#type' => 'actions');
192
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
193

    
194
  return $form;
195
}
196

    
197
/**
198
 * Form submission handler for ext_links_admin_link_template_form().
199
 */
200
function ext_links_admin_link_template_form_submit($form, &$form_state) {
201
  // Remove unnecessary values.
202
  form_state_values_clean($form_state);
203

    
204
  // Add the submitted form values to the text link_template, and save it.
205
  $link_template = $form['#link_template'];
206
  foreach ($form_state['values'] as $key => $value) {
207
    $link_template->$key = $value;
208
  }
209
  $status = ext_links_save($link_template);
210

    
211
  switch ($status) {
212
    case SAVED_NEW:
213
      drupal_set_message(t('Added external link %link_template.', array('%link_template' => $link_template->id)));
214
      break;
215

    
216
    case SAVED_UPDATED:
217
      drupal_set_message(t('The text link_template %link_template has been updated.', array('%link_template' => $link_template->id)));
218
      break;
219
  }
220

    
221
  drupal_goto('admin/config/cdm_dataportal/ext_links');
222
}
223

    
224
/**
225
 * Sets the status of a link_template.
226
 *
227
 * Used as page callback in menu items.
228
 *
229
 * @param $link_template
230
 *    The link template
231
 * @param $status
232
 *    The status value to set (values: 1 or 0)
233
 */
234
function ext_links_admin_link_template_set_status($link_template, $status){
235
    $link_template->status = $status == 1 ? 1 : 0;
236
    ext_links_save($link_template);
237
    drupal_goto("admin/config/cdm_dataportal/ext_links");
238
}
239

    
240
/**
241
 * Returns HTML for the text format administration overview form.
242
 *
243
 * @param $variables
244
 *   An associative array containing:
245
 *   - form: A render element representing the form.
246
 *
247
 * @ingroup themeable
248
 */
249
function theme_ext_links_admin_overview($variables) {
250

    
251
  drupal_add_css(drupal_get_path('module', 'ext_links'). '/ext-links.admin.css');
252

    
253
  $form = $variables['form'];
254

    
255
  $rows = array();
256
  foreach (element_children($form['link_templates']) as $id) {
257
    $row_class_attributes = $form['link_templates'][$id]['#attributes']['class'];
258
    $row_class_attributes[] = 'draggable';
259
    $form['link_templates'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
260
    $rows[] = array(
261
      'data' => array(
262
        drupal_render($form['link_templates'][$id]['title']),
263
        drupal_render($form['link_templates'][$id]['link']),
264
        drupal_render($form['link_templates'][$id]['category']),
265
        drupal_render($form['link_templates'][$id]['weight']),
266
        drupal_render($form['link_templates'][$id]['configure']),
267
        drupal_render($form['link_templates'][$id]['status']),
268
      ),
269
      'class' => $row_class_attributes,
270
    );
271
  }
272
  $header = array(t('Title'), t('Link'), t('Category'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
273
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'link-templates-order')));
274
  $output .= drupal_render_children($form);
275

    
276
  drupal_add_tabledrag('link-templates-order', 'order', 'sibling', 'text-format-order-weight', null, null, TRUE);
277

    
278
  return $output;
279
}
(2-2/6)