Projet

Général

Profil

Paste
Télécharger (2,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_feeds / FeedsLdapQueryFetcher.inc @ 5136ce55

1
<?php
2

    
3
/**
4
 * @file
5
 * FeedsLdapQueryFetcher
6
 */
7

    
8

    
9
/**
10
 * FeedsLdapQueryFetcherResult
11
 */
12
class FeedsLdapQueryFetcherResult extends FeedsFetcherResult {
13
  public $query_ids = array();
14
  public $ldap_result;
15

    
16
  /**
17
   * Constructor.
18
   */
19
  public function __construct($source_config) {
20
    $this->query_ids = (isset($source_config['query_ids']) && is_array($source_config['query_ids'])) ? $source_config['query_ids'] : array();
21
    parent::__construct('');
22
    $this->ldap_result = $this->getRaw();
23
    return $this->ldap_result;
24
  }
25

    
26
  /**
27
   * Overrides parent::getRaw();
28
   */
29
  public function getRaw() {
30

    
31
    $results = array();
32
    foreach ($this->query_ids as $i => $query_id) {
33
      $ldapQuery = ldap_query_get_queries($query_id, 'enabled', TRUE);
34
      $more_results = $ldapQuery->query();
35
      if (is_array($more_results)) {
36
        $results = array_merge($results, $more_results);
37
      }
38
    }
39
    return $results;
40
  }
41
}
42

    
43
/**
44
 * Fetches data via LDAP Query.
45
 */
46
class FeedsLdapQueryFetcher extends FeedsFetcher {
47

    
48
  /**
49
   * Implements FeedsFetcher::fetch().
50
   */
51
  public function fetch(FeedsSource $source) {
52
    $source_config = $source->getConfigFor($this);
53
    $result = new FeedsLdapQueryFetcherResult($source_config);
54
    return $result;
55
  }
56

    
57

    
58
  /**
59
   * Override parent::configDefaults().
60
   */
61
  public function configDefaults() {
62
    return array(
63
      'query_ids' => array(),
64
    );
65
  }
66

    
67
    /**
68
   * Override parent::configForm().
69
   */
70
  public function configForm(&$form_state) {
71
    $queries = ldap_query_get_queries(NULL, 'enabled');
72
    $query_options = array(0 => '--- select one or more queries ---');
73
    foreach ($queries as $qid => $query) {
74
      $query_options[$qid] = $query->name;
75
    }
76
    $form = array();
77
    $form['query_ids'] = array(
78
      '#type' => 'select',
79
      '#title' => t('LDAP Query'),
80
      '#multiple' => TRUE,
81
      '#size' => min(10, count($query_options)),
82
      '#required' => TRUE,
83
      '#default_value' => $this->config['query_ids'],
84
      '#description' => t('If more than one query is selected, results from all the queries will be returned.') .
85
        ' ' .
86
        t('Queries can be added and edited at !link', array('!link' => l(t('LDAP Query Admin'), LDAP_QUERY_INDEX_BASE_PATH))),
87
      '#options' => $query_options,
88
    );
89

    
90
    return $form;
91
  }
92

    
93
  /**
94
   * Override parent::sourceForm().
95
   */
96
  public function sourceForm($source_config) {
97

    
98
    $tokens = array(
99
      '!edit_link' => l(t('Edit Feed'), 'admin/structure/feeds/edit/' . $this->id),
100
    );
101

    
102
    $form_state = array();
103
    $form = $this->configForm($form_state);
104
    $form['preamble'] = array(
105
      '#type' => 'markup',
106
      '#markup' => t('This import is configured at !edit_link.', $tokens),
107
    );
108

    
109
    return $form;
110
  }
111

    
112
  /**
113
   * Override parent::sourceFormValidate().
114
   */
115
  public function sourceFormValidate(&$values) {
116
     // could execute query and see if it returns anything for validation
117
  }
118

    
119
}