You are here

menu_editor_term_creation.module in Menu Editor 6.3

File

me_term_creation/menu_editor_term_creation.module
View source
<?php

function menu_editor_term_creation_menu_editor_placeholders($menu) {
  $placeholders = array(
    '<term>' => NULL,
  );
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
    $placeholders["<term {$vocabulary->name}>"] = new _menu_editor_term_creation_PathFactory($vocabulary);
    if ($vocabulary->name === 'menu:' . $menu['menu_name']) {
      $placeholders['<term>'] = new _menu_editor_term_creation_PathFactory($vocabulary);
    }
  }
  if (empty($placeholders['<term>'])) {
    unset($placeholders['<term>']);
  }
  return $placeholders;
}
class _menu_editor_term_creation_PathFactory {
  protected $_vocabulary;
  function __construct($vocabulary) {
    $this->_vocabulary = $vocabulary;
  }
  function createPath($item) {
    $term = array(
      // 'tid' => NULL,  // term id
      'name' => $item['link_title'],
      'description' => $item['options']['description'],
      'vid' => $this->_vocabulary->vid,
      'weight' => $item['weight'],
    );
    drupal_write_record('term_data', $term);
    $parent_tid = 0;
    if (isset($item['plid'])) {
      if ($parent_item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['plid']))) {
        $m = array();
        if (preg_match('/^taxonomy\\/term\\/(\\d+)$/', $parent_item['link_path'], $m)) {
          $parent_tid = (int) $m[1];
          if ($parent_tid && ($parent_term = taxonomy_get_term($parent_tid))) {

            // check if in same vocabulary
            if ($parent_term->vid !== $term['vid']) {
              $parent_tid = 0;
            }
          }
        }
      }
    }
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $term['tid'], $parent_tid);
    module_invoke_all('taxonomy', 'insert', 'term', $term);
    return 'taxonomy/term/' . $term['tid'];
  }

}

/**
 * Implementation of class_menu_editor_Listener
 * (which is a new type of hook, hehe)
 */
class menu_editor_term_creation_class_menu_editor_Listener {
  protected $_terms_by_mlid = array();
  protected $_vocabulary;
  public function __construct($menu) {
    $this->_vocabulary = db_fetch_object(db_query("SELECT * FROM {vocabulary} WHERE name = '%s'", 'menu:' . $menu['menu_name']));
  }

  /**
   * Listener callback for submitted menu items
   * with router path 'taxonomy/term/%'
   */
  public function notifyItem__taxonomy_term__($item, $trail) {
    $m = array();
    if (!preg_match('/^taxonomy\\/term\\/(\\d+)$/', $item['link_path'], $m)) {
      return;
    }
    $tid = (int) $m[1];
    $term = $this
      ->_loadTerm($tid);
    if ($this->_vocabulary->vid !== $term->vid) {

      // only do this stuff with this menu's native vocabulary.
      return;
    }
    $this->_terms_by_mlid[$item['mlid']] = $term;
    $this->_terms[$term->tid] = $term;
    if (!empty($item['plid']) && isset($this->_terms_by_mlid[$item['plid']])) {
      $parent_term = $this->_terms_by_mlid[$item['plid']];
    }
    if (isset($parent_term) && $parent_term->vid === $term->vid) {
      $parent_tid = $parent_term->tid;
    }
    else {
      $parent_tid = 0;
    }
    $term->name = $item['link_title'];
    $term->description = $item['options']['attributes']['title'];
    $term->weight = $item['weight'];
    $values = array(
      'parent' => $parent_tid,
    ) + (array) $term;
    taxonomy_save_term($values);
  }

  /**
   * Listener callback for submitted menu items
   * with router path 'node/%'
   */
  public function notifyItem__node__($item, $trail) {
    $m = array();
    if (!preg_match('/^node\\/(\\d+)$/', $item['link_path'], $m)) {
      return;
    }
    $vid = $this->_vocabulary->vid;
    $nid = (int) $m[1];
    if (!empty($item['plid']) && isset($this->_terms_by_mlid[$item['plid']])) {
      $parent_term = $this->_terms_by_mlid[$item['plid']];
    }
    if ($vid && isset($parent_term) && $parent_term->vid === $vid) {
      $parent_tid = $parent_term->tid;
    }
    else {
      $parent_tid = 0;
    }
    $node = db_fetch_object(db_query('SELECT nid, vid FROM node WHERE nid = %d', $nid));
    taxonomy_node_delete_revision($node);
    if ($parent_tid) {
      db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $nid, $node->vid, $parent_tid);
    }
  }

  /**
   * This function is called at the end of
   * menu_editor_overview_form_submit(),
   * after the above listeners have been called for the respective items.
   */
  public function flush() {

    // we only allow those terms that are in the menu.
    $q = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $this->_vocabulary->vid);
    while ($term = db_fetch_object($q)) {
      if (!isset($this->_terms[$term->tid])) {
        taxonomy_del_term($term->tid);
      }
    }
  }
  protected function _loadTerm($tid) {
    return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
  }

}

Functions

Classes

Namesort descending Description
menu_editor_term_creation_class_menu_editor_Listener Implementation of class_menu_editor_Listener (which is a new type of hook, hehe)
_menu_editor_term_creation_PathFactory