FeedsTermProcessor.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @file
  4. * FeedsTermProcessor class.
  5. */
  6. /**
  7. * Feeds processor plugin. Create taxonomy terms from feed items.
  8. */
  9. class FeedsTermProcessor extends FeedsProcessor {
  10. /**
  11. * Define entity type.
  12. */
  13. public function entityType() {
  14. return 'taxonomy_term';
  15. }
  16. /**
  17. * Implements parent::entityInfo().
  18. */
  19. protected function entityInfo() {
  20. $info = parent::entityInfo();
  21. $info['label plural'] = t('Terms');
  22. $info['bundle name'] = t('Vocabulary');
  23. return $info;
  24. }
  25. /**
  26. * Creates a new term in memory and returns it.
  27. */
  28. protected function newEntity(FeedsSource $source) {
  29. $vocabulary = $this->vocabulary();
  30. $term = parent::newEntity($source);
  31. $term->vid = $vocabulary->vid;
  32. $term->vocabulary_machine_name = $vocabulary->machine_name;
  33. return $term;
  34. }
  35. /**
  36. * Load an existing entity.
  37. */
  38. protected function entityLoad(FeedsSource $source, $entity_id) {
  39. $entity = parent::entityLoad($source, $entity_id);
  40. // Avoid missing bundle errors when term has been loaded directly from db.
  41. if (empty($entity->vocabulary_machine_name) && !empty($entity->vid)) {
  42. $vocabulary = taxonomy_vocabulary_load($entity->vid);
  43. $entity->vocabulary_machine_name = ($vocabulary) ? $vocabulary->machine_name : NULL;
  44. }
  45. return $entity;
  46. }
  47. /**
  48. * Validates a term.
  49. */
  50. protected function entityValidate($term) {
  51. parent::entityValidate($term);
  52. if (drupal_strlen($term->name) == 0) {
  53. throw new FeedsValidationException(t('Term name missing.'));
  54. }
  55. }
  56. /**
  57. * Saves a term.
  58. *
  59. * We de-array parent fields with only one item.
  60. * This stops leftandright module from freaking out.
  61. */
  62. protected function entitySave($term) {
  63. if (isset($term->parent)) {
  64. if (is_array($term->parent) && count($term->parent) == 1) {
  65. $term->parent = reset($term->parent);
  66. }
  67. if (isset($term->tid) && ($term->parent == $term->tid || (is_array($term->parent) && in_array($term->tid, $term->parent)))) {
  68. throw new FeedsValidationException(t("A term can't be its own child. GUID:@guid", array('@guid' => $term->feeds_item->guid)));
  69. }
  70. }
  71. taxonomy_term_save($term);
  72. }
  73. /**
  74. * Deletes a series of terms.
  75. */
  76. protected function entityDeleteMultiple($tids) {
  77. foreach ($tids as $tid) {
  78. taxonomy_term_delete($tid);
  79. }
  80. }
  81. /**
  82. * Override parent::configDefaults().
  83. */
  84. public function configDefaults() {
  85. return array(
  86. 'vocabulary' => 0,
  87. ) + parent::configDefaults();
  88. }
  89. /**
  90. * Overrides parent::setTargetElement().
  91. *
  92. * Operate on a target item that is a taxonomy term.
  93. */
  94. public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value, array $mapping = array()) {
  95. switch ($target_element) {
  96. case 'parent':
  97. if (!empty($value)) {
  98. $terms = taxonomy_get_term_by_name($value);
  99. $parent_tid = '';
  100. foreach ($terms as $term) {
  101. if ($term->vid == $target_term->vid) {
  102. $parent_tid = $term->tid;
  103. }
  104. }
  105. if (!empty($parent_tid)) {
  106. $target_term->parent[] = $parent_tid;
  107. }
  108. else {
  109. $target_term->parent[] = 0;
  110. }
  111. }
  112. else {
  113. $target_term->parent[] = 0;
  114. }
  115. break;
  116. case 'parentguid':
  117. // value is parent_guid field value
  118. $parent_tid = 0;
  119. $query = db_select('feeds_item')
  120. ->fields('feeds_item', array('entity_id'))
  121. ->condition('entity_type', $this->entityType());
  122. $term_ids = array_keys($query->condition('guid', $value)->execute()->fetchAllAssoc('entity_id'));
  123. if (!empty($term_ids)) {
  124. $terms = entity_load($this->entityType(), $term_ids);
  125. foreach ($terms as $term) {
  126. if ($term->vid == $target_term->vid) {
  127. $parent_tid = $term->tid;
  128. break;
  129. }
  130. }
  131. }
  132. $target_term->parent[] = $parent_tid;
  133. break;
  134. case 'weight':
  135. if (!empty($value)) {
  136. $weight = intval($value);
  137. }
  138. else {
  139. $weight = 0;
  140. }
  141. $target_term->weight = $weight;
  142. break;
  143. case 'description':
  144. if (!empty($mapping['format'])) {
  145. $target_term->format = $mapping['format'];
  146. }
  147. elseif (!empty($this->config['input_format'])) {
  148. $target_term->format = $this->config['input_format'];
  149. }
  150. else {
  151. $target_term->format = filter_fallback_format();
  152. }
  153. $target_term->description = $value;
  154. break;
  155. default:
  156. parent::setTargetElement($source, $target_term, $target_element, $value);
  157. break;
  158. }
  159. }
  160. /**
  161. * Return available mapping targets.
  162. */
  163. public function getMappingTargets() {
  164. $targets = parent::getMappingTargets();
  165. $targets += array(
  166. 'name' => array(
  167. 'name' => t('Term name'),
  168. 'description' => t('Name of the taxonomy term.'),
  169. 'optional_unique' => TRUE,
  170. ),
  171. 'parent' => array(
  172. 'name' => t('Parent: Term name'),
  173. 'description' => t('The name of the parent taxonomy term.'),
  174. 'optional_unique' => TRUE,
  175. ),
  176. 'parentguid' => array(
  177. 'name' => t('Parent: GUID'),
  178. 'description' => t('The GUID of the parent taxonomy term.'),
  179. 'optional_unique' => TRUE,
  180. ),
  181. 'weight' => array(
  182. 'name' => t('Term weight'),
  183. 'description' => t('Weight of the taxonomy term.'),
  184. 'optional_unique' => TRUE,
  185. ),
  186. 'description' => array(
  187. 'name' => t('Term description'),
  188. 'description' => t('Description of the taxonomy term.'),
  189. 'summary_callbacks' => array('text_feeds_summary_callback'),
  190. 'form_callbacks' => array('text_feeds_form_callback'),
  191. ),
  192. );
  193. $this->getHookTargets($targets);
  194. return $targets;
  195. }
  196. /**
  197. * Get id of an existing feed item term if available.
  198. */
  199. protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
  200. if ($tid = parent::existingEntityId($source, $result)) {
  201. return $tid;
  202. }
  203. // The only possible unique target is name.
  204. foreach ($this->uniqueTargets($source, $result) as $target => $value) {
  205. if ($target == 'name') {
  206. $vocabulary = $this->vocabulary();
  207. if ($tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", array(':name' => $value, ':vid' => $vocabulary->vid))->fetchField()) {
  208. return $tid;
  209. }
  210. }
  211. }
  212. return 0;
  213. }
  214. /**
  215. * Return vocabulary to map to.
  216. */
  217. public function vocabulary() {
  218. if ($vocabulary = taxonomy_vocabulary_machine_name_load($this->bundle())) {
  219. return $vocabulary;
  220. }
  221. throw new Exception(t('No vocabulary defined for Taxonomy Term processor.'));
  222. }
  223. /**
  224. * Overrides FeedsProcessor::dependencies().
  225. */
  226. public function dependencies() {
  227. $dependencies = parent::dependencies();
  228. $dependencies['taxonomy'] = 'taxonomy';
  229. return $dependencies;
  230. }
  231. }