Project

General

Profile

Download (9.24 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
    );
34
    $form['link_templates'][$id]['#attributes']['class'] = [];
35
    if($link_template->status != 1){
36
      $form['link_templates'][$id]['#attributes']['class'][] = 'disabled';
37
    }
38
  }
39
  $form['actions'] = array('#type' => 'actions');
40
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
41
  return $form;
42
}
43

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

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

    
92
function ext_links_admin_appearance_page(){
93

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

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

    
108
  return system_settings_form($form);
109
}
110

    
111

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

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

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

    
170
  $form['status'] = array(
171
    '#type' => 'checkbox',
172
    '#title' => t('Enabled'),
173
    '#default_value' => $link_template->status
174
  );
175
  $form['weight'] = array(
176
    '#type' => 'weight',
177
    '#title' => t('Weight'),
178
    '#default_value' => $link_template->weight,
179
    '#delta' => 10,
180
  );
181
  $form['actions'] = array('#type' => 'actions');
182
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
183

    
184
  return $form;
185
}
186

    
187
/**
188
 * Form submission handler for ext_links_admin_link_template_form().
189
 */
190
function ext_links_admin_link_template_form_submit($form, &$form_state) {
191
  // Remove unnecessary values.
192
  form_state_values_clean($form_state);
193

    
194
  // Add the submitted form values to the text link_template, and save it.
195
  $link_template = $form['#link_template'];
196
  foreach ($form_state['values'] as $key => $value) {
197
    $link_template->$key = $value;
198
  }
199
  $status = ext_links_save($link_template);
200

    
201
  switch ($status) {
202
    case SAVED_NEW:
203
      drupal_set_message(t('Added external link %link_template.', array('%link_template' => $link_template->id)));
204
      break;
205

    
206
    case SAVED_UPDATED:
207
      drupal_set_message(t('The text link_template %link_template has been updated.', array('%link_template' => $link_template->id)));
208
      break;
209
  }
210

    
211
  drupal_goto('admin/config/cdm_dataportal/ext_links');
212
}
213

    
214
/**
215
 * Sets the status of a link_template.
216
 *
217
 * Used as page callback in menu items.
218
 *
219
 * @param $link_template
220
 *    The link template
221
 * @param $status
222
 *    The status value to set (values: 1 or 0)
223
 */
224
function ext_links_admin_link_template_set_status($link_template, $status){
225
    $link_template->status = $status == 1 ? 1 : 0;
226
    ext_links_save($link_template);
227
    drupal_goto("admin/config/cdm_dataportal/ext_links");
228
}
229

    
230
/**
231
 * Returns HTML for the text format administration overview form.
232
 *
233
 * @param $variables
234
 *   An associative array containing:
235
 *   - form: A render element representing the form.
236
 *
237
 * @ingroup themeable
238
 */
239
function theme_ext_links_admin_overview($variables) {
240

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

    
243
  $form = $variables['form'];
244

    
245
  $rows = array();
246
  foreach (element_children($form['link_templates']) as $id) {
247
    $row_class_attributes = $form['link_templates'][$id]['#attributes']['class'];
248
    $row_class_attributes[] = 'draggable';
249
    $form['link_templates'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
250
    $rows[] = array(
251
      'data' => array(
252
        drupal_render($form['link_templates'][$id]['title']),
253
        drupal_render($form['link_templates'][$id]['link']),
254
        drupal_render($form['link_templates'][$id]['category']),
255
        drupal_render($form['link_templates'][$id]['weight']),
256
        drupal_render($form['link_templates'][$id]['configure']),
257
        drupal_render($form['link_templates'][$id]['status']),
258
      ),
259
      'class' => $row_class_attributes,
260
    );
261
  }
262
  $header = array(t('Title'), t('Link'), t('Category'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
263
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'link-templates-order')));
264
  $output .= drupal_render_children($form);
265

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

    
268
  return $output;
269
}
(3-3/7)