Projet

Général

Profil

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

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

1
<?php
2

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

    
9
module_load_include('php', 'ldap_query', 'LdapQuery.class');
10

    
11
class LdapQueryAdmin extends LdapQuery {
12

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

    
47
  }
48

    
49
  function __construct($qid) {
50
    parent::__construct($qid);
51
  }
52

    
53
  protected function populateFromDrupalForm($op, $values) {
54

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

    
69
  public function save($op) {
70

    
71
    $op = $this->inDatabase ? 'edit' : 'insert';
72

    
73
    if (module_exists('ctools')) { // add or edit with ctolls
74

    
75
      ctools_include('export');
76
      $ctools_values = clone $this;
77

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

    
90
        }
91
      }
92

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

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

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

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

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

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

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

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

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

    
186

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

    
202
    $form['basic']['qid']['#disabled'] = ($op == 'edit');
203

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

    
212
    $form['basic']['sid']['#options'] = $server_options;
213

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

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

    
226
    return $form;
227
  }
228

    
229

    
230
  public function drupalFormValidate($op, $values)  {
231
    $errors = array();
232

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

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

    
258
    return $errors;
259
  }
260

    
261
  public function drupalFormSubmit($op, $values) {
262

    
263
    $this->populateFromDrupalForm($op, $values);
264

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

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

    
290

    
291

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

    
296
}