Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ldap / ldap_query / LdapQueryAdmin.class.php @ 59ae487e

1
<?php
2

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

    
9

    
10

    
11
module_load_include('php', 'ldap_query', 'LdapQuery.class');
12

    
13
class LdapQueryAdmin extends LdapQuery {
14

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

    
49
  }
50

    
51
  function __construct($qid) {
52
    parent::__construct($qid);
53
  }
54

    
55
  protected function populateFromDrupalForm($op, $values) {
56

    
57
    foreach ($this->fields() as $field_id => $field) {
58
      if (isset($field['form']) && property_exists('LdapQueryAdmin', $field['property_name'])) {
59
        $value = $values[$field_id];
60
        if (isset($field['form_to_prop_functions'])) {
61
          foreach ($field['form_to_prop_functions'] as $function) {
62
            $value = call_user_func($function, $value);
63
          }
64
        }
65
        $this->{$field['property_name']} = $value;
66
      }
67
    }
68
    $this->inDatabase = ($op == 'edit');
69
  }
70

    
71
  public function save($op) {
72

    
73
    $op = $this->inDatabase ? 'edit' : 'insert';
74

    
75
    if (module_exists('ctools')) { // add or edit with ctolls
76

    
77
      ctools_include('export');
78
      $ctools_values = clone $this;
79

    
80
      foreach ($this->fields() as $field_id => $field) {
81
        $value = $this->{$field['property_name']};
82
        if (isset($field['exportable']) && $field['exportable'] === FALSE) { // field not exportable
83
          unset($ctools_values->{$field['property_name']});
84
        }
85
        elseif (isset($field['schema']) && $field['property_name'] != $field_id) { // field in property with different name
86
          $ctools_values->{$field_id} = $value;
87
          unset($ctools_values->{$field['property_name']});
88
        }
89
        else {
90
          // do nothing.  property is already in cloned objecat
91

    
92
        }
93
      }
94

    
95
      // Populate our object with ctool's properties.  copying all properties for backward compatibility
96
      $object = ctools_export_crud_new('ldap_query');
97

    
98
      foreach ($object as $property_name => $value) {
99
        if (!isset($ctools_values->{$property_name})) {
100
          $ctools_values->$property_name = $value;
101
        }
102
      }
103
      $result = ctools_export_crud_save('ldap_query', $ctools_values);
104
      ctools_export_load_object_reset('ldap_query'); // ctools_export_crud_save doesn't invalidate cache
105
    }
106
    else {
107
      $values = array();
108
      foreach ($this->fields() as $field_id => $field) {
109
        if (isset($field['schema'])) {
110
          $values[$field_id] = $this->{$field['property_name']};
111
        }
112
      }
113
      if ($op == 'edit') { // edit w/o ctools
114
        $result = drupal_write_record('ldap_query', $values, 'qid');
115
      }
116
      else { // insert
117
        $result = drupal_write_record('ldap_query', $values);
118
      }
119
    }
120

    
121
    if ($result) {
122
      $this->inDatabase = TRUE;
123
    }
124
    else {
125
      drupal_set_message(t('Failed to write LDAP Query to the database.'));
126
    }
127
  }
128

    
129
  public function delete($qid) {
130
    if ($qid == $this->qid) {
131
      $this->inDatabase = FALSE;
132
      if (module_exists('ctools')) {
133
        ctools_include('export');
134
        ctools_export_load_object_reset('ldap_query');
135
      }
136
      return db_delete('ldap_query')->condition('qid', $qid)->execute();
137
    }
138
    else {
139
      return FALSE;
140
    }
141
  }
142

    
143
  public function getActions() {
144
    $switch = ($this->status ) ? 'disable' : 'enable';
145
    $actions = array();
146
    $actions[] =  l(t('edit'), LDAP_QUERY_MENU_BASE_PATH . '/query/edit/' . $this->qid);
147
    if (property_exists($this, 'type')) {
148
      if ($this->type == 'Overridden') {
149
          $actions[] = l(t('revert'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
150
      }
151
      if ($this->type == 'Normal') {
152
          $actions[] = l(t('delete'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
153
      }
154
    }
155
    else {
156
        $actions[] = l(t('delete'), LDAP_QUERY_MENU_BASE_PATH . '/query/delete/' . $this->qid);
157
    }
158
    $actions[] = l(t('test'), LDAP_QUERY_MENU_BASE_PATH . '/query/test/' . $this->qid);
159
    $actions[] = l($switch, LDAP_QUERY_MENU_BASE_PATH . '/query/' . $switch . '/' . $this->qid);
160
    return $actions;
161
  }
162

    
163
  public function drupalForm($op) {
164
    $form['#prefix'] = t('<p>Setup an LDAP query to be used by other modules
165
      such as LDAP Feeds.</p>');
166

    
167
    $form['basic'] = array(
168
      '#type' => 'fieldset',
169
      '#title' => t('Basic LDAP Query Settings'),
170
      '#collapsible' => TRUE,
171
      '#collapsed' => FALSE,
172
    );
173

    
174
    $form['query'] = array(
175
      '#type' => 'fieldset',
176
      '#title' => t('Query'),
177
      '#collapsible' => TRUE,
178
      '#collapsed' => FALSE,
179
    );
180

    
181
    $form['query_advanced'] = array(
182
      '#type' => 'fieldset',
183
      '#title' => t('Advanced Query Settings'),
184
      '#collapsible' => TRUE,
185
      '#collapsed' => TRUE,
186
    );
187

    
188

    
189
    foreach ($this->fields() as $field_id => $field) {
190
      $field_group = isset($field['form']['field_group']) ? $field['form']['field_group'] : FALSE;
191
      if (isset($field['form'])) {
192
        $form_item = $field['form'];
193
        $form_item['#default_value'] = $this->{$field['property_name']};
194
        if ($field_group) {
195
          $form[$field_group][$field_id] = $form_item;
196
          unset($form[$field_group][$field_id]['field_group']); // sirrelevant to form api
197
        }
198
        else {
199
          $form[$field_id] = $form_item;
200
        }
201
      }
202
    }
203

    
204
    $form['basic']['qid']['#disabled'] = ($op == 'edit');
205

    
206
    $servers = ldap_servers_get_servers(NULL, 'enabled');
207
    if (count($servers) == 0) {
208
      drupal_set_message(t('No ldap servers configured.  Please configure a server before an ldap query.'), 'error');
209
    }
210
    foreach ($servers as $sid => $server) {
211
      $server_options[$sid] = $server->name;
212
    }
213

    
214
    $form['basic']['sid']['#options'] = $server_options;
215

    
216
    $form['submit'] = array(
217
      '#type' => 'submit',
218
      '#value' => t('Save Query'),
219
    );
220

    
221
    $action = ($op == 'add') ? 'Add' : 'Update';
222
      $form['submit'] = array(
223
      '#type' => 'submit',
224
      '#value' => $action,
225
      '#weight' => 100,
226
    );
227

    
228
    return $form;
229
  }
230

    
231

    
232
  public function drupalFormValidate($op, $values)  {
233
    $errors = array();
234

    
235
    if ($op == 'delete') {
236
      if (!$this->qid) {
237
        $errors['query_name_missing'] = 'Query name missing from delete form.';
238
      }
239
    }
240
    else {
241
      $this->populateFromDrupalForm($op, $values);
242
      $errors = $this->validate($op);
243
    }
244
    return $errors;
245
  }
246

    
247
  protected function validate($op) {
248
    $errors = array();
249
    if ($op == 'add') {
250
      $ldap_queries = $this->getLdapQueryObjects('all', 'all');
251
      if (count($ldap_queries)) {
252
        foreach ($ldap_queries as $qid => $ldap_query) {
253
          if ($this->qid == $ldap_query->qid) {
254
            $errors['qid'] = t('An LDAP Query with the name %qid already exists.', array('%qid' => $this->qid));
255
          }
256
        }
257
      }
258
    }
259

    
260
    return $errors;
261
  }
262

    
263
  public function drupalFormSubmit($op, $values) {
264

    
265
    $this->populateFromDrupalForm($op, $values);
266

    
267
    if ($op == 'delete') {
268
      $this->delete($this);
269
    }
270
    else { // add or edit
271
      try {
272
        $save_result = $this->save($op);
273
      }
274
      catch (Exception $e) {
275
        $this->setError('Save Error',
276
          t('Failed to save object.  Your form data was not saved.'));
277
      }
278
    }
279
  }
280

    
281
  protected function arrayToLines($array) {
282
    $lines = "";
283
    if (is_array($array)) {
284
      $lines = join("\n", $array);
285
    }
286
    elseif (is_array(@unserialize($array))) {
287
      $lines = join("\n", unserialize($array));
288
    }
289
    return $lines;
290
  }
291

    
292

    
293

    
294
  protected function arrayToCsv($array) {
295
    return join(",", $array);
296
  }
297

    
298
}