Projet

Général

Profil

Paste
Télécharger (7,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / plugins / FeedsTermProcessor.inc @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * FeedsTermProcessor class.
6
 */
7

    
8
/**
9
 * Feeds processor plugin. Create taxonomy terms from feed items.
10
 */
11
class FeedsTermProcessor extends FeedsProcessor {
12

    
13
  /**
14
   * Define entity type.
15
   */
16
  public function entityType() {
17
    return 'taxonomy_term';
18
  }
19

    
20
  /**
21
   * Implements parent::entityInfo().
22
   */
23
  protected function entityInfo() {
24
    $info = parent::entityInfo();
25
    $info['label plural'] = t('Terms');
26
    $info['bundle name'] = t('Vocabulary');
27
    return $info;
28
  }
29

    
30
  /**
31
   * Creates a new term in memory and returns it.
32
   */
33
  protected function newEntity(FeedsSource $source) {
34
    $vocabulary = $this->vocabulary();
35
    $term = parent::newEntity($source);
36
    $term->vid = $vocabulary->vid;
37
    $term->vocabulary_machine_name = $vocabulary->machine_name;
38

    
39
    return $term;
40
  }
41

    
42
  /**
43
   * Load an existing entity.
44
   */
45
  protected function entityLoad(FeedsSource $source, $entity_id) {
46
    $entity = parent::entityLoad($source, $entity_id);
47

    
48
    // Avoid missing bundle errors when term has been loaded directly from db.
49
    if (empty($entity->vocabulary_machine_name) && !empty($entity->vid)) {
50
      $vocabulary = taxonomy_vocabulary_load($entity->vid);
51
      $entity->vocabulary_machine_name = ($vocabulary) ? $vocabulary->machine_name : NULL;
52
    }
53

    
54
    return $entity;
55
  }
56

    
57
  /**
58
   * Validates a term.
59
   */
60
  protected function entityValidate($term, FeedsSource $source = NULL) {
61
    parent::entityValidate($term);
62

    
63
    if (drupal_strlen($term->name) == 0) {
64
      throw new FeedsValidationException(t('Term name missing.'));
65
    }
66
  }
67

    
68
  /**
69
   * Saves a term.
70
   *
71
   * We de-array parent fields with only one item.
72
   * This stops leftandright module from freaking out.
73
   */
74
  protected function entitySave($term) {
75
    if (isset($term->parent)) {
76
      if (is_array($term->parent) && count($term->parent) == 1) {
77
        $term->parent = reset($term->parent);
78
      }
79
      if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
80
        throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
81
      }
82
    }
83
    taxonomy_term_save($term);
84
  }
85

    
86
  /**
87
   * Deletes a series of terms.
88
   */
89
  protected function entityDeleteMultiple($tids) {
90
    foreach ($tids as $tid) {
91
      taxonomy_term_delete($tid);
92
    }
93
  }
94

    
95
  /**
96
   * Override parent::configDefaults().
97
   */
98
  public function configDefaults() {
99
    return array(
100
      'vocabulary' => 0,
101
    ) + parent::configDefaults();
102
  }
103

    
104
  /**
105
   * Overrides parent::setTargetElement().
106
   *
107
   * Operate on a target item that is a taxonomy term.
108
   */
109
  public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value, array $mapping = array()) {
110
    switch ($target_element) {
111
      case 'parent':
112
        if (!empty($value)) {
113
          $terms = taxonomy_get_term_by_name($value);
114
          $parent_tid = '';
115
          foreach ($terms as $term) {
116
            if ($term->vid == $target_term->vid) {
117
              $parent_tid = $term->tid;
118
            }
119
          }
120
          if (!empty($parent_tid)) {
121
            $target_term->parent[] = $parent_tid;
122
          }
123
          else {
124
            $target_term->parent[] = 0;
125
          }
126
        }
127
        else {
128
          $target_term->parent[] = 0;
129
        }
130
        break;
131

    
132
      case 'parentguid':
133
        // Value is parent_guid field value.
134
        $parent_tid = 0;
135
        $query = db_select('feeds_item')
136
          ->fields('feeds_item', array('entity_id'))
137
          ->condition('entity_type', $this->entityType());
138
        $term_ids = array_keys($query->condition('guid', $value)->execute()->fetchAllAssoc('entity_id'));
139
        if (!empty($term_ids)) {
140
          $terms = entity_load($this->entityType(), $term_ids);
141
          foreach ($terms as $term) {
142
            if ($term->vid == $target_term->vid) {
143
              $parent_tid = $term->tid;
144
              break;
145
            }
146
          }
147
        }
148
        $target_term->parent[] = $parent_tid;
149
        break;
150

    
151
      case 'weight':
152
        if (!empty($value)) {
153
          $weight = intval($value);
154
        }
155
        else {
156
          $weight = 0;
157
        }
158
        $target_term->weight = $weight;
159
        break;
160

    
161
      case 'description':
162
        if (!empty($mapping['format'])) {
163
          $target_term->format = $mapping['format'];
164
        }
165
        elseif (!empty($this->config['input_format'])) {
166
          $target_term->format = $this->config['input_format'];
167
        }
168
        else {
169
          $target_term->format = filter_fallback_format();
170
        }
171
        $target_term->description = $value;
172
        break;
173

    
174
      default:
175
        parent::setTargetElement($source, $target_term, $target_element, $value);
176
        break;
177
    }
178
  }
179

    
180
  /**
181
   * Return available mapping targets.
182
   */
183
  public function getMappingTargets() {
184
    $targets = parent::getMappingTargets();
185
    $targets += array(
186
      'name' => array(
187
        'name' => t('Term name'),
188
        'description' => t('Name of the taxonomy term.'),
189
        'optional_unique' => TRUE,
190
      ),
191
      'parent' => array(
192
        'name' => t('Parent: Term name'),
193
        'description' => t('The name of the parent taxonomy term.'),
194
        'optional_unique' => TRUE,
195
      ),
196
      'parentguid' => array(
197
        'name' => t('Parent: GUID'),
198
        'description' => t('The GUID of the parent taxonomy term.'),
199
        'optional_unique' => TRUE,
200
      ),
201
      'weight' => array(
202
        'name' => t('Term weight'),
203
        'description' => t('Weight of the taxonomy term.'),
204
        'optional_unique' => TRUE,
205
      ),
206
      'description' => array(
207
        'name' => t('Term description'),
208
        'description' => t('Description of the taxonomy term.'),
209
        'summary_callbacks' => array('text_feeds_summary_callback'),
210
        'form_callbacks' => array('text_feeds_form_callback'),
211
      ),
212
    );
213

    
214
    $this->getHookTargets($targets);
215

    
216
    return $targets;
217
  }
218

    
219
  /**
220
   * Get id of an existing feed item term if available.
221
   */
222
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
223
    if ($tid = parent::existingEntityId($source, $result)) {
224
      return $tid;
225
    }
226

    
227
    // The only possible unique target is name.
228
    foreach ($this->uniqueTargets($source, $result) as $target => $value) {
229
      if ($target == 'name') {
230
        $vocabulary = $this->vocabulary();
231
        if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
232
          return $tid;
233
        }
234
      }
235
    }
236
    return 0;
237
  }
238

    
239
  /**
240
   * Return vocabulary to map to.
241
   */
242
  public function vocabulary() {
243
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->bundle())) {
244
      return $vocabulary;
245
    }
246
    throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
247
  }
248

    
249
  /**
250
   * Overrides FeedsProcessor::dependencies().
251
   */
252
  public function dependencies() {
253
    $dependencies = parent::dependencies();
254
    $dependencies['taxonomy'] = 'taxonomy';
255
    return $dependencies;
256
  }
257

    
258
}