Projet

Général

Profil

Paste
Télécharger (5,78 ko) Statistiques
| Branche: | Révision:

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

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
   * Define entity type.
14
   */
15
  public function entityType() {
16
    return 'taxonomy_term';
17
  }
18

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

    
29
  /**
30
   * Creates a new term in memory and returns it.
31
   */
32
  protected function newEntity(FeedsSource $source) {
33
    $vocabulary = $this->vocabulary();
34
    $term = new stdClass();
35
    $term->vid = $vocabulary->vid;
36
    $term->vocabulary_machine_name = $vocabulary->machine_name;
37
    $term->format = isset($this->config['input_format']) ? $this->config['input_format'] : filter_fallback_format();
38
    return $term;
39
  }
40

    
41
  /**
42
   * Validates a term.
43
   */
44
  protected function entityValidate($term) {
45
    if (drupal_strlen($term->name) == 0) {
46
      throw new FeedsValidationException(t('Term name missing.'));
47
    }
48
  }
49

    
50
  /**
51
   * Saves a term.
52
   *
53
   * We de-array parent fields with only one item.
54
   * This stops leftandright module from freaking out.
55
   */
56
  protected function entitySave($term) {
57
    if (isset($term->parent)) {
58
      if (is_array($term->parent) && count($term->parent) == 1) {
59
        $term->parent = reset($term->parent);
60
      }
61
      if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
62
        throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
63
      }
64
    }
65
    taxonomy_term_save($term);
66
  }
67

    
68
  /**
69
   * Deletes a series of terms.
70
   */
71
  protected function entityDeleteMultiple($tids) {
72
    foreach ($tids as $tid) {
73
      taxonomy_term_delete($tid);
74
    }
75
  }
76

    
77
  /**
78
   * Override parent::configDefaults().
79
   */
80
  public function configDefaults() {
81
    return array(
82
      'vocabulary' => 0,
83
    ) + parent::configDefaults();
84
  }
85

    
86
  /**
87
   * Override setTargetElement to operate on a target item that is a taxonomy term.
88
   */
89
  public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value) {
90
    switch ($target_element) {
91
      case 'parent':
92
        if (!empty($value)) {
93
          $terms = taxonomy_get_term_by_name($value);
94
          $parent_tid = '';
95
          foreach ($terms as $term) {
96
            if ($term->vid == $target_term->vid) {
97
              $parent_tid = $term->tid;
98
            }
99
          }
100
          if (!empty($parent_tid)) {
101
            $target_term->parent[] = $parent_tid;
102
          }
103
          else {
104
            $target_term->parent[] = 0;
105
          }
106
        }
107
        else {
108
          $target_term->parent[] = 0;
109
        }
110
        break;
111
      case 'parentguid':
112
        // value is parent_guid field value
113
        $query = db_select('feeds_item')
114
          ->fields('feeds_item', array('entity_id'))
115
          ->condition('entity_type', $this->entityType());
116
        $parent_tid = $query->condition('guid', $value)->execute()->fetchField();
117
        $target_term->parent[] = ($parent_tid) ? $parent_tid : 0;
118

    
119
        break;
120
      case 'weight':
121
        if (!empty($value)) {
122
          $weight = intval($value);
123
        }
124
        else {
125
          $weight = 0;
126
        }
127
        $target_term->weight = $weight;
128
        break;
129
      default:
130
        parent::setTargetElement($source, $target_term, $target_element, $value);
131
        break;
132
    }
133
  }
134

    
135
  /**
136
   * Return available mapping targets.
137
   */
138
  public function getMappingTargets() {
139
    $targets = parent::getMappingTargets();
140
    $targets += array(
141
      'name' => array(
142
        'name' => t('Term name'),
143
        'description' => t('Name of the taxonomy term.'),
144
        'optional_unique' => TRUE,
145
      ),
146
      'parent' => array(
147
        'name' => t('Parent: Term name'),
148
        'description' => t('The name of the parent taxonomy term.'),
149
        'optional_unique' => TRUE,
150
      ),
151
      'parentguid' => array(
152
        'name' => t('Parent: GUID'),
153
        'description' => t('The GUID of the parent taxonomy term.'),
154
        'optional_unique' => TRUE,
155
      ),
156
      'weight' => array(
157
        'name' => t('Term weight'),
158
        'description' => t('Weight of the taxonomy term.'),
159
        'optional_unique' => TRUE,
160
      ),
161
      'description' => array(
162
        'name' => t('Term description'),
163
        'description' => t('Description of the taxonomy term.'),
164
      ),
165
    );
166

    
167
    // Let implementers of hook_feeds_term_processor_targets() add their targets.
168
    try {
169
      self::loadMappers();
170
      $entity_type = $this->entityType();
171
      $bundle = $this->bundle();
172
      drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
173
    }
174
    catch (Exception $e) {
175
      // Do nothing.
176
    }
177
    return $targets;
178
  }
179

    
180
  /**
181
   * Get id of an existing feed item term if available.
182
   */
183
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
184
    if ($tid = parent::existingEntityId($source, $result)) {
185
      return $tid;
186
    }
187

    
188
    // The only possible unique target is name.
189
    foreach ($this->uniqueTargets($source, $result) as $target => $value) {
190
      if ($target == 'name') {
191
        $vocabulary = $this->vocabulary();
192
        if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
193
          return $tid;
194
        }
195
      }
196
    }
197
    return 0;
198
  }
199

    
200
  /**
201
   * Return vocabulary to map to.
202
   */
203
  public function vocabulary() {
204
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->bundle())) {
205
      return $vocabulary;
206
    }
207
    throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
208
  }
209
}