Project

General

Profile

Download (8.68 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]['glue'] = array('#markup' => check_plain($link_template->glue));
24
    $form['link_templates'][$id]['category'] = array('#markup' => check_plain($link_template->category));
25
    $form['link_templates'][$id]['configure'] = array('#type' => 'link', '#title' => t('configure'), '#href' => 'admin/config/cdm_dataportal/ext_links/' . $link_template->id);
26
    $form['link_templates'][$id]['status'] = array('#type' => 'link',
27
      '#title' => $link_template->status == 1 ? t('disable') : t('enable'),
28
      '#href' => 'admin/config/cdm_dataportal/ext_links/' . $link_template->id . '/status/' . ($link_template->status == 1 ? '0' : '1'));
29
    $form['link_templates'][$id]['weight'] = array(
30
      '#type' => 'weight',
31
      '#title' => t('Weight for @title', array('@title' => $link_template->id)),
32
      '#title_display' => 'invisible',
33
      '#default_value' => $link_template->weight,
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 filter_admin_overview().
47
 */
48
function ext_links_admin_overview_submit($form, &$form_state) {
49
  foreach ($form_state['values']['formats'] 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('filter_format')
53
        ->fields(array('weight' => $data['weight']))
54
        ->condition('format', $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
 *   - glue: The string to concatenate name parts in the URL query string.
70
 *   - status: (optional) An integer indicating whether the ext link is
71
 *     enabled (1) or not (0). Defaults to 1.
72
 *   - weight: (optional) The weight of the external link, which controls its
73
 *     placement in external link block. If omitted, the weight is set to 0.
74
 *     Defaults to NULL.
75
 *
76
 * @return
77
 *   A form array.
78
 *
79
 * @see filter_menu()
80
 */
81
function ext_links_admin_link_template_page($link_template = NULL) {
82
  if (!isset($link_template->id)) {
83
    drupal_set_title(t('Add external link'));
84
    $link_template = (object) array(
85
      'title' => NULL,
86
      'link' => 'https://',
87
      'glue' => '',
88
      'weight' => 0,
89
      'status' => 1
90
    );
91
  }
92
  return drupal_get_form('ext_links_admin_link_template_form', $link_template);
93
}
94

    
95

    
96
/**
97
 * Form constructor for the External Link add/edit form.
98
 *
99
 * @param $link_template
100
 *   A link template object having the properties:
101
 *   - id: A machine-readable name representing the ID of the external link.
102
 *     If this corresponds to an existing external link, that format will be
103
 *     updated; otherwise, a new external link will be created.
104
 *   - title: The link title
105
 *   - link: The link url template.
106
 *   - glue: The string to concatenate name parts in the URL query string.
107
 *   - status: (optional) An integer indicating whether the ext link is
108
 *     enabled (1) or not (0). Defaults to 1.
109
 *   - weight: (optional) The weight of the external link, which controls its
110
 *     placement in external link block. If omitted, the weight is set to 0.
111
 *     Defaults to NULL.
112
 *
113
 * @see ext_links_admin_link_template_form_validate()
114
 * @see ext_links_admin_link_template_form_submit()
115
 * @ingroup forms
116
 */
117
function ext_links_admin_link_template_form($form, &$form_state, $link_template) {
118

    
119
  $form['#link_template'] = $link_template;
120
  $form['#tree'] = TRUE;
121

    
122
  $form['id'] = array(
123
    '#type' => 'machine_name',
124
    '#required' => TRUE,
125
    '#default_value' => $link_template->id,
126
    '#maxlength' => 255,
127
    '#machine_name' => array(
128
      'exists' => 'ext_link_exists',
129
    ),
130
    '#disabled' => !empty($link_template->id),
131
  );
132
  $form['title'] = array(
133
    '#type' => 'textfield',
134
    '#title' => t('Title'),
135
    '#default_value' => $link_template->title,
136
    '#description' => t('The text of the link that will be displayed.'),
137
    '#required' => TRUE,
138
  );
139
  $form['link'] = array(
140
    '#type' => 'textfield',
141
    '#title' => t('Link'),
142
    '#description' => t('The URL of the link to the external resource or service. 
143
    The species name parts will be appended to this string.<p>E.g. 
144
    <code>https://external-service.test/images?q=</code> will result in 
145
    <code>https://external-service.test/images?q=Bellis+perennis</code> when the glue string is set to "+"</p>'),
146
    '#default_value' => $link_template->link,
147
    '#required' => TRUE,
148
  );
149
  $form['glue'] = array(
150
    '#type' => 'textfield',
151
    '#title' => t('Glue'),
152
    '#description' => t('The string used to concatenate genus and species name parts in the query string.'),
153
    '#default_value' => $link_template->glue,
154
    '#required' => false,
155
  );
156
  $form['status'] = array(
157
    '#type' => 'checkbox',
158
    '#title' => t('Enabled'),
159
    '#default_value' => $link_template->status
160
  );
161
  $form['weight'] = array(
162
    '#type' => 'weight',
163
    '#title' => t('Weight'),
164
    '#default_value' => $link_template->weight,
165
    '#delta' => 10,
166
  );
167
  $form['actions'] = array('#type' => 'actions');
168
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
169

    
170
  return $form;
171
}
172

    
173
/**
174
 * Form submission handler for ext_links_admin_link_template_form().
175
 */
176
function ext_links_admin_link_template_form_submit($form, &$form_state) {
177
  // Remove unnecessary values.
178
  form_state_values_clean($form_state);
179

    
180
  // Add the submitted form values to the text link_template, and save it.
181
  $link_template = $form['#link_template'];
182
  foreach ($form_state['values'] as $key => $value) {
183
    $link_template->$key = $value;
184
  }
185
  $status = ext_links_save($link_template);
186

    
187
  switch ($status) {
188
    case SAVED_NEW:
189
      drupal_set_message(t('Added external link %link_template.', array('%link_template' => $link_template->id)));
190
      break;
191

    
192
    case SAVED_UPDATED:
193
      drupal_set_message(t('The text link_template %link_template has been updated.', array('%link_template' => $link_template->id)));
194
      break;
195
  }
196

    
197
  drupal_goto('admin/config/cdm_dataportal/ext_links');
198
}
199

    
200
/**
201
 * Returns HTML for the text format administration overview form.
202
 *
203
 * @param $variables
204
 *   An associative array containing:
205
 *   - form: A render element representing the form.
206
 *
207
 * @ingroup themeable
208
 */
209
function theme_ext_links_admin_overview($variables) {
210

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

    
213
  $form = $variables['form'];
214

    
215
  $rows = array();
216
  foreach (element_children($form['link_templates']) as $id) {
217
    $row_class_attributes = $form['link_templates'][$id]['#attributes']['class'];
218
    $row_class_attributes[] = 'draggable';
219
    $form['link_templates'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
220
    $rows[] = array(
221
      'data' => array(
222
        drupal_render($form['link_templates'][$id]['title']),
223
        drupal_render($form['link_templates'][$id]['link']),
224
        drupal_render($form['link_templates'][$id]['glue']),
225
        drupal_render($form['link_templates'][$id]['category']),
226
        drupal_render($form['link_templates'][$id]['weight']),
227
        drupal_render($form['link_templates'][$id]['configure']),
228
        drupal_render($form['link_templates'][$id]['status']),
229
      ),
230
      'class' => $row_class_attributes,
231
    );
232
  }
233
  $header = array(t('Title'), t('Link'), t('Glue'), t('Category'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
234
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
235
  $output .= drupal_render_children($form);
236

    
237
  drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
238

    
239
  return $output;
240
}
(3-3/7)