/[drupal]/contributions/modules/aggregator2/aggregator2_autotaxonomy.module
ViewVC logotype

Diff of /contributions/modules/aggregator2/aggregator2_autotaxonomy.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7.2.1, Fri Feb 3 10:02:15 2006 UTC revision 1.7.2.2, Thu Jul 20 11:29:05 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: aggregator2_autotaxonomy.module,v 1.7 2005-12-13 22:43:40 ahwayakchih Exp $ */  /* $Id: aggregator2_autotaxonomy.module,v 1.7.2.1 2006/02/03 10:02:15 ahwayakchih Exp $ */
3    
4  /*  /*
5   * Aggregator2 Autotaxonomy   * Aggregator2 Autotaxonomy
# Line 7  Line 7 
7   * @file   * @file
8   * Allows Aggregator2 RSS items with <category> tags to auto create Drupal taxonomy terms.   * Allows Aggregator2 RSS items with <category> tags to auto create Drupal taxonomy terms.
9   * @author Budda <mike@buddasworld.co.uk>   * @author Budda <mike@buddasworld.co.uk>
10     * @author Marcin Konicki <ahwayakchih@gmail.com>
11   */   */
12    
13  /**  /**
# Line 24  function aggregator2_autotaxonomy_help($ Line 25  function aggregator2_autotaxonomy_help($
25    }    }
26  }  }
27    
28    function aggregator2_autotaxonomy_perm() {
29      return array('add new categories');
30    }
31    
32  function aggregator2_autotaxonomy_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {  function aggregator2_autotaxonomy_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
33    static $cat_names = array();    switch ($op) {
34        case 'load':
35    if($node->type == 'aggregator2-item' && isset($node->rss_item_data)) {        // remember title, for case when it's changed and we will want to change category name
36      $vocab = variable_get('aggregator2_autotaxonomy_vocabulary', FALSE);        $node->agg2_old_title = $node->title;
37      if(!$vocab) {        break;
38        watchdog('aggregator2', t('No vocabulary has been set for autotaxonomy to use.'), WATCHDOG_WARNING, l('set', 'admin/settings/aggregator2_autotaxonomy'));      case 'form post':
39        return;      {
40      }        $vid = variable_get('agg2_autotaxonomy_easy_vid', 0);
41  //[RSS:CATEGORY][][VALUE] && [RSS:CATEGORY][][DOMAIN] attrib        if ($vid > 0 && user_access('add new categories')) {
42  //[ATOM:CATEGORY][][TERM][][VALUE]          $vocab = taxonomy_get_vocabulary($vid);
43  //[DC:SUBJECT][][VALUE]          if (in_array($node->type, $vocab->nodes)) {
44      if ($node->rss_item_data['CATEGORY'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // RSS 0.92, 2.0            $temp = '';
45      else if ($node->rss_item_data['CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // ATOM 1.0            $temp .= form_textfield(t('New categories'), 'agg2_autotaxonomy_add_cat', $edit['agg2_autotaxonomy_add_cat'], 50, 128, t('List of comma separated categories. Example: "Linux,Windows,Operating systems".'), NULL, FALSE);
46      else if ($node->rss_item_data['ATOM:CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['ATOM:CATEGORY']; // ATOM 1.0            if ($node->type == 'aggregator2-feed') {
47      else if ($node->rss_item_data['SUBJECT']) $categories = $node->rss_item_data['SUBJECT']; // DublinCore              $temp .= form_checkbox(t('Apply also to items'), 'agg2_autotaxonomy_items_too', 1, $edit['agg2_autotaxonomy_items_too'], t('If enabled, list of categories above will also be used for aggregated items.'));
     else if ($node->rss_item_data['DC:SUBJECT']) $categories = $node->rss_item_data['DC:SUBJECT']; // DublinCore  
   
     switch($op) {  
       case 'insert':  
       case 'update':  
         // If the RSS item has any category tags, add new ones to taxonomy if needed  
         if (count($categories) > 0) {  
           if (!isset($node->taxonomy) || !is_array($node->taxonomy)) {  
             $node->taxonomy = array();  
           }  
   
           foreach ($categories as $category_name) {  
             if ($category_name['TERM']) { // ATOM 1.0  
               $category_name = strtolower($category_name['TERM'][0]['VALUE']);  
             }  
             else { // RSS 0.92, RSS 2.0, DublinCore  
               $category_name = strtolower($category_name['VALUE']);  
             }  
   
             if (trim($category_name) != '') {  
               if (!isset($cat_names[$category_name])) {  
                 $cat_names[$category_name] = module_invoke('taxonomy', 'get_term_by_name', $category_name);  
               }  
   
               // Create a new category term  
               if (count($cat_names[$category_name]) == 0) {  
                 $term = array();  
                 $term['name'] = $category_name;  
                 $term['description'] = t('Auto generated by aggregator2 autotaxonomy');  
                 $term['vid'] = $vocab;  
                 $term['weight'] = 0;  
                 $term = taxonomy_save_term($term);  
                 $node->taxonomy[] = $term['tid'];  
                 $cat_names[$category_name][0]->tid = $term['tid'];  
               }  
               else {  
                 // Use the existing category term in the database  
                 if (!in_array($cat_names[$category_name][0]->tid, $node->taxonomy)) {  
                   $node->taxonomy[] = $cat_names[$category_name][0]->tid;  
                 }  
               }  
             }  
48            }            }
49              return form_group(t('Additional categories'), $temp);
50          }          }
51          break;        }
52          break;
53      }      }
54        case 'form pre':
55          if ($node->type == 'aggregator2-feed') {
56            return form_hidden('agg2_old_title', $node->title);
57          }
58          break;
59        case 'insert':
60          if (isset($node->agg2_autotaxonomy_add_cat) && trim($node->agg2_autotaxonomy_add_cat) != '' && user_access('add new categories')) {
61            // remember old taxonomy, so we can compare with updated and apply new terms to items
62            $old = $node->taxonomy;
63    
64            $terms = explode(',', $node->agg2_autotaxonomy_add_cat);
65            _aggregator2_autotaxonomy_aggregate($node, $terms, variable_get('agg2_autotaxonomy_easy_vid', 0));
66    
67            if ($node->agg2_autotaxonomy_items_too) {
68              $terms = array_diff($node->taxonomy, $old); // find new
69              $node->feed_item_taxonomy = array_merge($node->feed_item_taxonomy, $terms); // add them
70              // save
71              db_query('UPDATE {aggregator2_feed} SET item_taxonomy = \'%s\' WHERE nid = %d', serialize($node->feed_item_taxonomy), $node->nid);
72            }
73          }
74          if ($node->type == 'aggregator2-feed') {
75            _aggregator2_autotaxonomy_add($node);
76          }
77          else if ($node->type == 'aggregator2-item') {
78            _aggregator2_autotaxonomy_aggregate($node);
79          }
80          break;
81        case 'update':
82          if (isset($node->agg2_autotaxonomy_add_cat) && trim($node->agg2_autotaxonomy_add_cat) != '' && user_access('add new categories')) {
83            // remember old taxonomy, so we can compare with updated and apply new terms to items
84            $old = $node->taxonomy;
85    
86            $terms = explode(',', $node->agg2_autotaxonomy_add_cat);
87            _aggregator2_autotaxonomy_aggregate($node, $terms, variable_get('agg2_autotaxonomy_easy_vid', 0));
88    
89            if ($node->agg2_autotaxonomy_items_too) {
90              $terms = array_diff($node->taxonomy, $old); // find new
91              $node->feed_item_taxonomy = array_merge($node->feed_item_taxonomy, $terms); // add them
92              // save
93              db_query('UPDATE {aggregator2_feed} SET item_taxonomy = \'%s\' WHERE nid = %d', serialize($node->feed_item_taxonomy), $node->nid);
94            }
95          }
96          if ($node->type == 'aggregator2-feed') {
97            _aggregator2_autotaxonomy_update($node);
98          }
99          else if ($node->type == 'aggregator2-item') {
100            _aggregator2_autotaxonomy_aggregate($node);
101          }
102          break;
103        case 'delete':
104          if ($node->type == 'aggregator2-feed') {
105            _aggregator2_autotaxonomy_delete($node);
106          }
107          break;
108    }    }
109  }  }
110    
111    
112  function aggregator2_autotaxonomy_settings() {  function aggregator2_autotaxonomy_settings() {
113      $output = '';
114    $vocabs = db_query("SELECT v.vid, v.name FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'aggregator2-item');    $vocabs = db_query("SELECT v.vid, v.name FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'aggregator2-item');
115    
116    // Build list of available vocabularies    // Build list of available vocabularies
117    $options = array();    $options = array();
118      $options[0] = '-------';
119    while ($vocabulary = db_fetch_object($vocabs)) {    while ($vocabulary = db_fetch_object($vocabs)) {
120      $options[$vocabulary->vid] = $vocabulary->name;      $options[$vocabulary->vid] = $vocabulary->name;
121    }    }
122    if($options) {    if(count($options) > 1) {
123      $output = form_select(t('Vocabulary'), 'aggregator2_autotaxonomy_vocabulary', variable_get('aggregator2_autotaxonomy_vocabulary', ''), $options, t('Which vocabulary should the auto created category names be associated with?'), 0, FALSE, TRUE);      $temp = form_select(t('Vocabulary'), 'aggregator2_autotaxonomy_vocabulary', variable_get('aggregator2_autotaxonomy_vocabulary', 0), $options, t('Which vocabulary should the auto created category names be associated with? Select dashed line to disable this feature.'), 0, FALSE, FALSE);
124    }    }
125    else {    else {
126      $output = '<p>' . t("You need to associate a %url with the node type %node to use this module.      $temp = '<p>' . t("You need to associate a %url with the node type %node to use this module.
127                            If you don't have any vocabularies set-up you can %addurl now.",                            If you don't have any vocabularies set-up you can %addurl now.",
128                            array('%url' => l('vocabulary', 'admin/taxonomy'),                            array('%url' => l('vocabulary', 'admin/taxonomy'),
129                                  '%node' => theme('placeholder', 'aggregator2 news feed item'),                                  '%node' => theme('placeholder', 'aggregator2 news feed item'),
130                                  '%addurl' => l('add one', 'admin/taxonomy/add/vocabulary')                                  '%addurl' => l('add one', 'admin/taxonomy/add/vocabulary')
131                                  )) . '</p>';                                  )) . '</p>';
132    }    }
133      $output .= form_group(t('Aggregation of categories'), $temp);
134    
135    
136      if(count($options) > 1) {
137        $temp = form_select(t('Vocabulary'), 'agg2_autotaxonomy_feed_vid', variable_get('agg2_autotaxonomy_feed_vid', 0), $options, t('Which vocabulary should the auto created category names be associated with? Select dashed line to disable this feature.'), 0, FALSE, FALSE);
138        $temp .= form_checkbox(t('Apply to feed items also'), 'agg2_autotaxonomy_items_too', 1, variable_get('agg2_autotaxonomy_items_too', 0), t('If enabled, auto created term will be used also for future feed\'s items'));
139    
140        $output .= form_group(t('Autocreation of feed\'s categories'), $temp, t('Select vocabulary above if you want autotaxonomy module to automatically create term for each new feed. Such term will get name of the feed.'));
141      }
142    
143      if(count($options) > 1) {
144        $temp = form_select(t('Vocabulary'), 'agg2_autotaxonomy_easy_vid', variable_get('agg2_autotaxonomy_easy_vid', 0), $options, t('Which vocabulary should the "easy created" category names be associated with? Select dashed line to disable this feature.'), 0, FALSE, FALSE);
145    
146        $output .= form_group(t('Easy categories'), $temp, t('Select vocabulary above if you want autotaxonomy module to allow to specify set of terms by just writing them in input field. Such terms will be added to vocabulary specified above and will be associated with node being edited.'));
147      }
148    
149    return $output;    return $output;
150  }  }
151  ?>  
152    function _aggregator2_autotaxonomy_aggregate(&$node, $categories = array(), $vocab = 0) {
153      static $cat_names;
154    
155      if (!is_array($cat_names)) {
156        $cat_names = array();
157      }
158    
159      if ($vocab == 0) {
160        $vocab = variable_get('aggregator2_autotaxonomy_vocabulary', 0);
161      }
162      if ($vocab == 0) {
163        //watchdog('aggregator2', t('No vocabulary has been set for autotaxonomy to use.'), WATCHDOG_WARNING, l('set', 'admin/settings/aggregator2_autotaxonomy'));
164        return;
165      }
166    
167      if (count($categories) < 1 && isset($node->rss_item_data)) {
168        //[RSS:CATEGORY][][VALUE] && [RSS:CATEGORY][][DOMAIN] attrib
169        //[ATOM:CATEGORY][][TERM][][VALUE]
170        //[DC:SUBJECT][][VALUE]
171        if ($node->rss_item_data['CATEGORY'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // RSS 0.92, 2.0
172        else if ($node->rss_item_data['CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['CATEGORY']; // ATOM 1.0
173        else if ($node->rss_item_data['ATOM:CATEGORY'][0]['TERM'][0]['VALUE']) $categories = $node->rss_item_data['ATOM:CATEGORY']; // ATOM 1.0
174        else if ($node->rss_item_data['SUBJECT']) $categories = $node->rss_item_data['SUBJECT']; // DublinCore
175        else if ($node->rss_item_data['DC:SUBJECT']) $categories = $node->rss_item_data['DC:SUBJECT']; // DublinCore
176      }
177    
178      // If the RSS item has any category tags, add new ones to taxonomy if needed
179      if (count($categories) > 0) {
180        if (!isset($node->taxonomy) || !is_array($node->taxonomy)) {
181          $node->taxonomy = array();
182        }
183    
184        foreach ($categories as $category_name) {
185          if (is_array($category_name)) {
186            if ($category_name['TERM']) { // ATOM 1.0
187              $category_name = strtolower($category_name['TERM'][0]['VALUE']);
188            }
189            else { // RSS 0.92, RSS 2.0, DublinCore
190              $category_name = strtolower($category_name['VALUE']);
191            }
192          }
193          else {
194            $category_name = strtolower($category_name);
195          }
196    
197          if (trim($category_name) != '') {
198            if (!isset($cat_names[$category_name])) {
199              $temp = module_invoke('taxonomy', 'get_term_by_name', $category_name);
200              foreach ($temp as $cat) {
201                if ($cat->vid == $vocab) {
202                  $cat_names[$category_name] = $cat->tid;
203                }
204              }
205            }
206    
207            // Create a new category term
208            if (!isset($cat_names[$category_name])) {
209              $term = array();
210              $term['name'] = $category_name;
211              $term['description'] = t('Auto generated by aggregator2 autotaxonomy');
212              $term['vid'] = $vocab;
213              $term['weight'] = 0;
214              $term = taxonomy_save_term($term);
215              $node->taxonomy[] = $term['tid'];
216              $cat_names[$category_name] = $term['tid'];
217            }
218            else {
219              // Use the existing category term in the database
220              if (!in_array($cat_names[$category_name], $node->taxonomy)) {
221                $node->taxonomy[] = $cat_names[$category_name];
222              }
223            }
224          }
225        }
226      }
227    }
228    
229    function _aggregator2_autotaxonomy_add(&$node) {
230      $category_name = strtolower($node->title);
231      $vocab = variable_get('agg2_autotaxonomy_feed_vid', 0);
232    
233      if (trim($category_name) == '' || $vocab == 0) {
234        return NULL;
235      }
236    
237      $cat = module_invoke('taxonomy', 'get_term_by_name', $category_name);
238      $term = NULL;
239    
240      foreach ($cat as $t) {
241        if ($t->vid == $vocab) {
242          $term = object2array($t);
243          break;
244        }
245      }
246    
247      // Make sure node has only one term from that vocab (so in case of being created from template it doesn't keep template's term)
248      if (is_array($node->taxonomy)) {
249        $old = db_query('SELECT tid FROM {term_data} WHERE tid IN (%s) AND vid = %d', implode(',', $node->taxonomy), $vocab);
250        $rm = array();
251        while ($temp = db_fetch_object($old)) {
252          $rm[] = $temp->tid;
253        }
254        $node->taxonomy = array_diff($node->taxonomy, $rm);
255        if (is_array($node->feed_item_taxonomy)) {
256          $node->feed_item_taxonomy = array_diff($node->feed_item_taxonomy, $rm);
257        }
258      }
259      else {
260        $node->taxonomy = array();
261      }
262    
263      // Create a new category term
264      if (!is_array($term)) {
265        $term['name'] = $category_name;
266        $term['description'] = '';
267        $term['vid'] = $vocab;
268        $term['weight'] = 0;
269        $term = taxonomy_save_term($term);
270      }
271    
272      if ($term['vid'] == $vocab) {
273        $node->taxonomy[] = $term['tid'];
274        if (!is_array($node->feed_item_taxonomy)) {
275          $node->feed_item_taxonomy = array();
276        }
277        if (variable_get('agg2_autotaxonomy_items_too', 0) && !in_array($term['tid'], $node->feed_item_taxonomy)) {
278          $node->feed_item_taxonomy[] = $term['tid'];
279    
280          // Aggregator2 goes before this one, so we have to modify database directly
281          db_query('UPDATE {aggregator2_feed} SET item_taxonomy = \'%s\' WHERE nid = %d', serialize($node->feed_item_taxonomy), $node->nid);
282        }
283    
284        return $term['tid'];
285      }
286    }
287    
288    function _aggregator2_autotaxonomy_update(&$node) {
289      $category_name = strtolower($node->title);
290      $vocab = variable_get('agg2_autotaxonomy_feed_vid', 0);
291    
292      if (trim($category_name) == '' || $vocab == 0) {
293        return NULL;
294      }
295    
296      $cat = module_invoke('taxonomy', 'get_term_by_name', $category_name);
297      $term = NULL;
298    
299      foreach ($cat as $t) {
300        if ($t->vid == $vocab) {
301          $term = object2array($t);
302          break;
303        }
304      }
305    
306    
307      if (!is_array($term) && isset($node->agg2_old_title)) {
308        // try to find category by previous title of node (in case title was changed)
309        $cat = module_invoke('taxonomy', 'get_term_by_name', strtolower($node->agg2_old_title));
310        foreach ($cat as $t) {
311          if ($t->vid == $vocab) {
312            $term = object2array($t);
313            break;
314          }
315        }
316      }
317      if (is_array($term)) {
318        // update category name
319        $term['name'] = $category_name;
320        $term = taxonomy_save_term($term);
321        if (!in_array($term['tid'], $node->taxonomy)) {
322          $node->taxonomy[] = $term['tid'];
323        }
324        return $term['tid'];
325      }
326      else {
327        // just create new
328        return _aggregator2_autotaxonomy_add($node);
329      }
330    }
331    
332    function _aggregator2_autotaxonomy_delete(&$node) {
333      $category_name = strtolower($node->title);
334      $vocab = variable_get('agg2_autotaxonomy_feed_vid', 0);
335    
336      if (trim($category_name) == '' || $vocab == 0) {
337        return;
338      }
339    
340      $cat = module_invoke('taxonomy', 'get_term_by_name', $category_name);
341      $term = NULL;
342    
343      foreach ($cat as $t) {
344        if ($t->vid == $vocab) {
345          $term = $t;
346        }
347      }
348    
349      if ($term == NULL || $term->vid != $vocab) {
350        return;
351      }
352    
353      // Now check if there are still nodes with this category associated.
354      $result = db_query('SELECT * FROM {term_node} WHERE tid = %d LIMIT 1', $term->tid);
355      if ($result && $temp = db_fetch_object($temp)) {
356        return;
357      }
358    
359      // Delete category only when there is no node associated with it
360      taxonomy_del_term($term->tid);
361    }

Legend:
Removed from v.1.7.2.1  
changed lines
  Added in v.1.7.2.2

  ViewVC Help
Powered by ViewVC 1.1.2