Projet

Général

Profil

Paste
Télécharger (8,59 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_query / LdapQueryAdmin.class.php @ 91af538d

1
<?php
2

    
3
/**
4
 * @file
5
 * LDAP Query Admin Class.
6
 */
7

    
8
module_load_include('php', 'ldap_query', 'LdapQuery.class');
9
/**
10
 *
11
 */
12
class LdapQueryAdmin extends LdapQuery {
13

    
14
  /**
15
   * @param string $sid
16
   *   either 'all' or the ldap server sid.
17
   * @param $type
18
   *   = 'all', 'enabled'
19
   */
20
  public static function getLdapQueryObjects($sid = 'all', $type = 'enabled', $class = 'LdapQuery') {
21
    $queries = [];
22
    if (module_exists('ctools')) {
23
      ctools_include('export');
24
      $select = ctools_export_load_object('ldap_query', 'all');
25
    }
26
    else {
27
      try {
28
        $select = db_select('ldap_query', 'ldap_query')
29
          ->fields('ldap_query')
30
          ->execute();
31
      }
32
      catch (Exception $e) {
33
        drupal_set_message(t('query index query failed. Message = %message, query= %query',
34
          ['%message' => $e->getMessage(), '%query' => $e->query_string]), 'error');
35
        return [];
36
      }
37
    }
38
    foreach ($select as $result) {
39
      $query = ($class == 'LdapQuery') ? new LdapQuery($result->qid) : new LdapQueryAdmin($result->qid);
40
      if (
41
          ($sid == 'all' || $query->sid == $sid)
42
          &&
43
          (!$type || $type == 'all' || ($query->status = 1 && $type == 'enabled'))
44
        ) {
45
        $queries[$result->qid] = $query;
46
      }
47
    }
48
    return $queries;
49

    
50
  }
51

    
52
  /**
53
   *
54
   */
55
  public function __construct($qid) {
56
    parent::__construct($qid);
57
  }
58

    
59
  /**
60
   *
61
   */
62
  protected function populateFromDrupalForm($op, $values) {
63

    
64
    foreach ($this->fields() as $field_id => $field) {
65
      if (isset($field['form']) && property_exists('LdapQueryAdmin', $field['property_name'])) {
66
        $value = $values[$field_id];
67
        if (isset($field['form_to_prop_functions'])) {
68
          foreach ($field['form_to_prop_functions'] as $function) {
69
            $value = call_user_func($function, $value);
70
          }
71
        }
72
        $this->{$field['property_name']} = $value;
73
      }
74
    }
75
    $this->inDatabase = ($op == 'edit');
76
  }
77

    
78
  /**
79
   *
80
   */
81
  public function save($op) {
82

    
83
    $op = $this->inDatabase ? 'edit' : 'insert';
84

    
85
    // Add or edit with ctolls.
86
    if (module_exists('ctools')) {
87

    
88
      ctools_include('export');
89
      $ctools_values = clone $this;
90

    
91
      foreach ($this->fields() as $field_id => $field) {
92
        $value = $this->{$field['property_name']};
93
        // Field not exportable.
94
        if (isset($field['exportable']) && $field['exportable'] === FALSE) {
95
          unset($ctools_values->{$field['property_name']});
96
        }
97
        // Field in property with different name.
98
        elseif (isset($field['schema']) && $field['property_name'] != $field_id) {
99
          $ctools_values->{$field_id} = $value;
100
          unset($ctools_values->{$field['property_name']});
101
        }
102
        else {
103
          // Do nothing.  property is already in cloned objecat.
104
        }
105
      }
106

    
107
      // Populate our object with ctool's properties.  copying all properties for backward compatibility.
108
      $object = ctools_export_crud_new('ldap_query');
109

    
110
      foreach ($object as $property_name => $value) {
111
        if (!isset($ctools_values->{$property_name})) {
112
          $ctools_values->$property_name = $value;
113
        }
114
      }
115
      $result = ctools_export_crud_save('ldap_query', $ctools_values);
116
      // ctools_export_crud_save doesn't invalidate cache.
117
      ctools_export_load_object_reset('ldap_query');
118
    }
119
    else {
120
      $values = [];
121
      foreach ($this->fields() as $field_id => $field) {
122
        if (isset($field['schema'])) {
123
          $values[$field_id] = $this->{$field['property_name']};
124
        }
125
      }
126
      // Edit w/o ctools.
127
      if ($op == 'edit') {
128
        $result = drupal_write_record('ldap_query', $values, 'qid');
129
      }
130
      // Insert.
131
      else {
132
        $result = drupal_write_record('ldap_query', $values);
133
      }
134
    }
135

    
136
    if ($result) {
137
      $this->inDatabase = TRUE;
138
    }
139
    else {
140
      drupal_set_message(t('Failed to write LDAP Query to the database.'));
141
    }
142
  }
143

    
144
  /**
145
   *
146
   */
147
  public function delete($qid) {
148
    if ($qid == $this->qid) {
149
      $this->inDatabase = FALSE;
150
      if (module_exists('ctools')) {
151
        ctools_include('export');
152
        ctools_export_load_object_reset('ldap_query');
153
      }
154
      return db_delete('ldap_query')->condition('qid', $qid)->execute();
155
    }
156
    else {
157
      return FALSE;
158
    }
159
  }
160

    
161
  /**
162
   *
163
   */
164
  public function getActions() {
165
    $switch = ($this->status) ? 'disable' : 'enable';
166
    $actions = [];
167
    $actions[] = l(t('edit'), LDAP_QUERY_MENU_BASE_PATH . '/query/edit/' . $this->qid);
168
    if (property_exists($this, 'type')) {
169
      if ($this->type == 'Overridden') {
170
        $actions[] = l(t('revert'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
171
      }
172
      if ($this->type == 'Normal') {
173
        $actions[] = l(t('delete'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
174
      }
175
    }
176
    else {
177
      $actions[] = l(t('delete'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
178
    }
179
    $actions[] = l(t('test'), LDAP_QUERY_MENU_BASE_PATH . '/query/test/' . $this->qid);
180
    $actions[] = l($switch, LDAP_QUERY_MENU_BASE_PATH . '/query/' . $switch . '/' . $this->qid);
181
    return $actions;
182
  }
183

    
184
  /**
185
   *
186
   */
187
  public function drupalForm($op) {
188
    $form['#prefix'] = t('<p>Setup an LDAP query to be used by other modules
189
      such as LDAP Feeds.</p>');
190

    
191
    $form['basic'] = [
192
      '#type' => 'fieldset',
193
      '#title' => t('Basic LDAP Query Settings'),
194
      '#collapsible' => TRUE,
195
      '#collapsed' => FALSE,
196
    ];
197

    
198
    $form['query'] = [
199
      '#type' => 'fieldset',
200
      '#title' => t('Query'),
201
      '#collapsible' => TRUE,
202
      '#collapsed' => FALSE,
203
    ];
204

    
205
    $form['query_advanced'] = [
206
      '#type' => 'fieldset',
207
      '#title' => t('Advanced Query Settings'),
208
      '#collapsible' => TRUE,
209
      '#collapsed' => TRUE,
210
    ];
211

    
212
    foreach ($this->fields() as $field_id => $field) {
213
      $field_group = isset($field['form']['field_group']) ? $field['form']['field_group'] : FALSE;
214
      if (isset($field['form'])) {
215
        $form_item = $field['form'];
216
        $form_item['#default_value'] = $this->{$field['property_name']};
217
        if ($field_group) {
218
          $form[$field_group][$field_id] = $form_item;
219
          // Sirrelevant to form api.
220
          unset($form[$field_group][$field_id]['field_group']);
221
        }
222
        else {
223
          $form[$field_id] = $form_item;
224
        }
225
      }
226
    }
227

    
228
    $form['basic']['qid']['#disabled'] = ($op == 'edit');
229

    
230
    $servers = ldap_servers_get_servers(NULL, 'enabled');
231
    if (count($servers) == 0) {
232
      drupal_set_message(t('No ldap servers configured.  Please configure a server before an ldap query.'), 'error');
233
    }
234
    foreach ($servers as $sid => $server) {
235
      $server_options[$sid] = $server->name;
236
    }
237

    
238
    $form['basic']['sid']['#options'] = $server_options;
239

    
240
    $form['submit'] = [
241
      '#type' => 'submit',
242
      '#value' => t('Save Query'),
243
    ];
244

    
245
    $action = ($op == 'add') ? 'Add' : 'Update';
246
    $form['submit'] = [
247
      '#type' => 'submit',
248
      '#value' => $action,
249
      '#weight' => 100,
250
    ];
251

    
252
    return $form;
253
  }
254

    
255
  /**
256
   *
257
   */
258
  public function drupalFormValidate($op, $values) {
259
    $errors = [];
260

    
261
    if ($op == 'delete') {
262
      if (!$this->qid) {
263
        $errors['query_name_missing'] = 'Query name missing from delete form.';
264
      }
265
    }
266
    else {
267
      $this->populateFromDrupalForm($op, $values);
268
      $errors = $this->validate($op);
269
    }
270
    return $errors;
271
  }
272

    
273
  /**
274
   *
275
   */
276
  protected function validate($op) {
277
    $errors = [];
278
    if ($op == 'add') {
279
      $ldap_queries = $this->getLdapQueryObjects('all', 'all');
280
      if (count($ldap_queries)) {
281
        foreach ($ldap_queries as $qid => $ldap_query) {
282
          if ($this->qid == $ldap_query->qid) {
283
            $errors['qid'] = t('An LDAP Query with the name %qid already exists.', ['%qid' => $this->qid]);
284
          }
285
        }
286
      }
287
    }
288

    
289
    return $errors;
290
  }
291

    
292
  /**
293
   *
294
   */
295
  public function drupalFormSubmit($op, $values) {
296

    
297
    $this->populateFromDrupalForm($op, $values);
298

    
299
    if ($op == 'delete') {
300
      $this->delete($this);
301
    }
302
    // Add or edit.
303
    else {
304
      try {
305
        $save_result = $this->save($op);
306
      }
307
      catch (Exception $e) {
308
        $this->setError('Save Error',
309
          t('Failed to save object.  Your form data was not saved.'));
310
      }
311
    }
312
  }
313

    
314
  /**
315
   *
316
   */
317
  protected function arrayToLines($array) {
318
    $lines = "";
319
    if (is_array($array)) {
320
      $lines = join("\n", $array);
321
    }
322
    elseif (is_array(@unserialize($array))) {
323
      $lines = join("\n", unserialize($array));
324
    }
325
    return $lines;
326
  }
327

    
328
  /**
329
   *
330
   */
331
  protected function arrayToCsv($array) {
332
    return join(",", $array);
333
  }
334

    
335
}