data_taxonomy.module

Hooks and API functions for Data Node module.

Functions

Namesort descending Description
data_taxonomy_ajax_save AHAH callback for saving terms.
data_taxonomy_data_delete_query_alter Implements hook_data_delete_query_alter().
data_taxonomy_data_insert Implements hook_data_insert().
data_taxonomy_data_update Implements hook_data_update().
data_taxonomy_feeds_data_processor_targets_alter Implements hook_feeds_data_processor_targets_alter().
data_taxonomy_get_info Get data_taxonomy information for a given data table.
data_taxonomy_get_term_by_name_vid Look up a term by name and vocabulary id.
data_taxonomy_get_vocabulary Get a vocabulary by vid or module name.
data_taxonomy_lookup_term Look up a term by name and vid.
data_taxonomy_menu Implements hook_menu().
data_taxonomy_permission Implements hook_permission().
data_taxonomy_sanitize Sanitize a term name depending on its vocabulary settings.
data_taxonomy_save_relations Save term_data - data table relationships in data_taxonomy table.
data_taxonomy_save_tags Explode terms from typed input, create new terms.
data_taxonomy_save_term_array Save a term array, create a new one if it does not exist yet.
data_taxonomy_save_term_name Save a term, create a new one if it does not exist yet.
data_taxonomy_tagging_form Form callback for tagging.
data_taxonomy_tagging_form_submit Submit handler.
data_taxonomy_tag_links Generate a links array suitable for use with theme('links') from an array of taxonomy terms.
data_taxonomy_taxonomy Implements hook_taxonomy().
data_taxonomy_theme Implements hook_theme().
data_taxonomy_views_api Implements hook_views_api().
data_taxonomy_vocabulary_id Return the vocabulary identifier, the vocabulary's vid or module.
template_preprocess_data_taxonomy_tagging_form Preprocessor for theme('data_taxonomy_tagging_form').
_data_taxonomy_insert_terms Helper function, inserts a series of taxonomy terms for a record.
_data_taxonomy_replace_tokens Replaces % in $path with arguments.

File

sites/all/modules/local_contrib/data/data_taxonomy/data_taxonomy.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks and API functions for Data Node module.
  5. */
  6. /**
  7. * Implements hook_views_api().
  8. */
  9. function data_taxonomy_views_api() {
  10. return array(
  11. 'api' => '2.0',
  12. 'path' => drupal_get_path('module', 'data_taxonomy') . '/views',
  13. );
  14. }
  15. /**
  16. * Implements hook_menu().
  17. */
  18. function data_taxonomy_menu() {
  19. $items = array();
  20. $items['data-taxonomy/ajax-save'] = array(
  21. 'page callback' => 'data_taxonomy_ajax_save',
  22. 'page arguments' => array(),
  23. 'access arguments' => array('edit data taxonomy relations'),
  24. 'type' => MENU_CALLBACK,
  25. );
  26. $items['admin/structure/data/edit/%data_ui_table/taxonomy'] = array(
  27. 'title' => 'Relate to taxonomy',
  28. 'description' => 'Administer data tables.',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('data_taxonomy_settings_form', 4),
  31. 'file' => 'data_taxonomy.admin.inc',
  32. 'access arguments' => array('administer data tables'),
  33. 'type' => MENU_LOCAL_TASK,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * Implements hook_theme().
  39. */
  40. function data_taxonomy_theme() {
  41. return array(
  42. 'data_taxonomy_tagging_form' => array(
  43. 'render element' => 'form',
  44. 'path' => drupal_get_path('module', 'data_taxonomy') . '/theme',
  45. 'template' => 'data-taxonomy-tagging-form',
  46. ),
  47. );
  48. }
  49. /**
  50. * Implements hook_permission().
  51. */
  52. function data_taxonomy_permission() {
  53. return array(
  54. 'edit data taxonomy relations' => array(
  55. 'title' => t('edit data taxonomy relations'),
  56. 'description' => t('TODO Add a description for \'edit data taxonomy relations\''),
  57. ),
  58. );
  59. }
  60. /**
  61. * Implements hook_feeds_data_processor_targets_alter().
  62. */
  63. function data_taxonomy_feeds_data_processor_targets_alter(&$fields, $table_name) {
  64. if ($info = data_taxonomy_get_info($table_name)) {
  65. foreach ($info['vocabularies'] as $vid) {
  66. $vocabulary = data_taxonomy_get_vocabulary($vid);
  67. $fields['data_taxonomy:' . $vid] = array(
  68. 'name' => t('Taxonomy: @vocabulary', array("@vocabulary" => $vocabulary->name)),
  69. 'description' => t('Map to taxonomy terms of @vocabulary vocabulary.', array("@vocabulary" => $vocabulary->name)),
  70. );
  71. }
  72. }
  73. }
  74. /**
  75. * Implements hook_data_insert().
  76. */
  77. function data_taxonomy_data_insert($record, $table_name) {
  78. if ($info = data_taxonomy_get_info($table_name)) {
  79. $id = $record[$info['id']];
  80. foreach ($info['vocabularies'] as $vid) {
  81. if (isset($record['data_taxonomy:' . $vid])) {
  82. _data_taxonomy_insert_terms($table_name, $id, $record['data_taxonomy:' . $vid], data_taxonomy_get_vocabulary($vid));
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * Implements hook_data_update().
  89. */
  90. function data_taxonomy_data_update($record, $table_name) {
  91. if ($info = data_taxonomy_get_info($table_name)) {
  92. $id = $record[$info['id']];
  93. foreach ($info['vocabularies'] as $vid) {
  94. if (isset($record['data_taxonomy:' . $vid])) {
  95. $vocabulary = data_taxonomy_get_vocabulary($vid);
  96. // TODO Please convert this statement to the D7 database API syntax.
  97. /* db_query("DELETE dt FROM {data_taxonomy} dt JOIN {taxonomy_term_data} td ON dt.tid = td.tid WHERE td.vid = %d", $vocabulary->vid) */
  98. NULL;
  99. _data_taxonomy_insert_terms($table_name, $id, $record['data_taxonomy:' . $vid], $vocabulary);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Implements hook_taxonomy().
  106. */
  107. function data_taxonomy_taxonomy($op = NULL, $type = NULL, $term = NULL) {
  108. if ($type == 'term' && $term['tid'] && $op == 'delete') {
  109. // TODO Please review the conversion of this statement to the D7 database API syntax.
  110. /* db_query("DELETE FROM {data_taxonomy} WHERE tid = %d", $term['tid']) */
  111. db_delete('data_taxonomy')
  112. ->condition('tid', $term['tid'])
  113. ->execute();
  114. }
  115. }
  116. /**
  117. * Helper function, inserts a series of taxonomy terms for a record.
  118. *
  119. * Creates new taxonomy terms on the fly for vocabularies that are tags.
  120. *
  121. * @param $table_name
  122. * Table name of the record.
  123. * @param $id
  124. * Record identifier.
  125. * @param $terms
  126. * An array of terms. Can be an array of tids, term names, term arrays or
  127. * objects that can be casted into a term array. If the target vocabulary is
  128. * a tag vocabulary, non-existing terms will be created on the fly.
  129. * @param $vocabulary
  130. * A vocuabulary object.
  131. */
  132. function _data_taxonomy_insert_terms($table_name, $id, $terms, $vocabulary) {
  133. if (!is_array($terms)) {
  134. $terms = array($terms);
  135. }
  136. $tids = array();
  137. foreach ($terms as $term) {
  138. if (is_string($term)) {
  139. $term = data_taxonomy_sanitize($term, $vocabulary->vid);
  140. $term = data_taxonomy_save_term_name($term, $vocabulary->vid, $vocabulary->tags);
  141. }
  142. else {
  143. if (is_object($term)) {
  144. $term = (array) $term;
  145. }
  146. if (is_array($term)) {
  147. $term['name'] = data_taxonomy_sanitize($term['name'], $vocabulary->vid);
  148. $term = data_taxonomy_save_term_array($term, $vocabulary->vid, $vocabulary->tags);
  149. }
  150. }
  151. if (is_array($term)) {
  152. $term = isset($term['tid']) ? $term['tid'] : FALSE;
  153. }
  154. if (is_numeric($term) && !isset($tids[$term])) {
  155. $tids[$term] = $term;
  156. // TODO Please convert this statement to the D7 database API syntax.
  157. /* db_query("INSERT INTO {data_taxonomy}(id, data_table_name, tid) VALUES(%d, '%s', %d)", $id, $table_name, $term) */
  158. NULL;
  159. }
  160. }
  161. }
  162. /**
  163. * Implements hook_data_delete_query_alter().
  164. *
  165. * @todo: replace this with hook_data_table_delete_rows().
  166. */
  167. function data_taxonomy_data_delete_query_alter($query, $table_name) {
  168. if ($info = data_taxonomy_get_info($table_name)) {
  169. $table_name = db_escape_table($table_name);
  170. $query->addSubject('data_taxonomy');
  171. $query->addJoin('data_taxonomy', "$table_name.{$info['id']} = data_taxonomy.id AND data_taxonomy.data_table_name = '$table_name'", 'LEFT JOIN');
  172. }
  173. }
  174. /**
  175. * Get data_taxonomy information for a given data table.
  176. */
  177. function data_taxonomy_get_info($table_name) {
  178. static $info = array();
  179. if (!isset($info[$table_name])) {
  180. $info[$table_name] = FALSE;
  181. $meta = data_get_table($table_name)->get('meta');
  182. if (!empty($meta['data_taxonomy']) && is_array($meta['data_taxonomy'])) {
  183. $info[$table_name] = $meta['data_taxonomy'];
  184. }
  185. }
  186. return $info[$table_name];
  187. }
  188. /**
  189. * Form callback for tagging.
  190. */
  191. function data_taxonomy_tagging_form(&$form_state, $vid, $id, $table_name, $path, $args) {
  192. $access = user_access('edit data taxonomy relations');
  193. $form = array('#theme' => 'data_taxonomy_tagging_form');
  194. $form['vid'] = array(
  195. '#type' => 'hidden',
  196. '#value' => $vid,
  197. '#access' => $access,
  198. );
  199. $form['id'] = array(
  200. '#type' => 'hidden',
  201. '#value' => $id,
  202. '#access' => $access,
  203. );
  204. $form['table_name'] = array(
  205. '#type' => 'hidden',
  206. '#value' => $table_name,
  207. '#access' => $access,
  208. );
  209. $form['path'] = array(
  210. '#type' => 'hidden',
  211. '#value' => $path,
  212. '#access' => $access,
  213. );
  214. $form['args'] = array(
  215. '#type' => 'hidden',
  216. '#value' => implode('&', $args),
  217. '#access' => $access,
  218. );
  219. $result = db_query('SELECT td.tid, td.name, td.vid FROM {taxonomy_term_data} td JOIN {data_taxonomy} dt ON td.tid = dt.tid WHERE dt.data_table_name = :dt.data_table_name AND dt.id = :dt.id AND td.vid = :td.vid', array(':dt.data_table_name' => $table_name, ':dt.id' => $id, ':td.vid' => $vid));
  220. $tags = $terms = array();
  221. while ($term = db_fetch_object($result)) {
  222. $tags[$term->tid] = $term->name;
  223. $terms[$term->tid] = $term;
  224. }
  225. $form['tags'] = array(
  226. '#type' => 'textfield',
  227. '#default_value' => implode(', ', $tags),
  228. '#autocomplete_path' => 'taxonomy/autocomplete/' . $vid,
  229. '#id' => "edit-tags-data-taxonomy-{$vid}-{$id}",
  230. '#access' => $access,
  231. );
  232. // Ensure our path gets rewritten. We don't use url() here because we're
  233. // not interested in rewrites to parts of the request other than $_GET['q'].
  234. $ajax_path = 'data-taxonomy/ajax-save';
  235. if (function_exists('custom_url_rewrite_outbound')) {
  236. $original_path = $ajax_path;
  237. $options = array();
  238. custom_url_rewrite_outbound($ajax_path, $options, $original_path);
  239. }
  240. $form['submit'] = array(
  241. '#type' => 'submit',
  242. '#value' => t('Save'),
  243. '#access' => $access,
  244. // AHAH stack: We need to assign our submit button its own ID as auto
  245. // assignment will quickly lead to a situation where our AJAX form button
  246. // has a different ID from the original.
  247. '#id' => "edit-submit-data-taxonomy-{$vid}-{$id}",
  248. '#ahah' => array(
  249. 'path' => $ajax_path,
  250. 'wrapper' => "data-taxonomy-tags-{$vid}-{$id}",
  251. 'method' => 'replace',
  252. 'effect' => 'none',
  253. ),
  254. );
  255. // Pass on key elements for theming.
  256. $form['#terms'] = $terms;
  257. $form['#path'] = $path;
  258. $form['#args'] = $args;
  259. if ($access) {
  260. $form['#edit'] = l(t('Edit'), $_GET['q'], array('fragment' => 'data-taxonomy-edit', 'attributes' => array('class' => 'data-taxonomy-edit')));
  261. }
  262. $form['#vocab'] = taxonomy_vocabulary_load($vid);
  263. return $form;
  264. }
  265. /**
  266. * Submit handler.
  267. */
  268. function data_taxonomy_tagging_form_submit($form, &$form_state) {
  269. // Using clicked_button allows us to use more than one of the same form on
  270. // a screen.
  271. $post = $form_state['clicked_button']['#post'];
  272. $tids = data_taxonomy_save_tags($form_state['values']['tags'], $post['vid']);
  273. data_taxonomy_save_relations($post['vid'], $post['id'], $post['table_name'], $tids);
  274. }
  275. /**
  276. * Save term_data - data table relationships in data_taxonomy table.
  277. */
  278. function data_taxonomy_save_relations($vid, $id, $table_name, $tids) {
  279. // TODO Please convert this statement to the D7 database API syntax.
  280. /* db_query("DELETE dt FROM {data_taxonomy} dt JOIN {taxonomy_term_data} td ON dt.tid = td.tid WHERE dt.id = %d AND dt.data_table_name = '%s' AND td.vid = %d", $id, $table_name, $vid) */
  281. NULL;
  282. foreach ($tids as $tid) {
  283. // TODO Please review the conversion of this statement to the D7 database API syntax.
  284. /* db_query('INSERT INTO {data_taxonomy} (id, data_table_name, tid) VALUES (%d, "%s", %d)', $id, $table_name, $tid) */
  285. $id = db_insert('data_taxonomy')
  286. ->fields(array(
  287. 'id' => $id,
  288. 'data_table_name' => $table_name,
  289. 'tid' => $tid,
  290. ))
  291. ->execute();
  292. }
  293. }
  294. /**
  295. * Save a term, create a new one if it does not exist yet.
  296. *
  297. * @param $name
  298. * A taxonomy term name to look up and save.
  299. * @param $vid
  300. * A <em>numeric</em> vocabulary id (vid).
  301. *
  302. * @return
  303. * A taxonomy term array.
  304. */
  305. function data_taxonomy_save_term_name($name, $vid) {
  306. if ($term = data_taxonomy_lookup_term($name, $vid)) {
  307. return $term;
  308. }
  309. $term = array(
  310. 'vid' => $vid,
  311. 'name' => $name,
  312. );
  313. taxonomy_term_save($term /* TODO Term object replaces array $term */);
  314. return $term;
  315. }
  316. /**
  317. * Save a term array, create a new one if it does not exist yet.
  318. *
  319. * @param $term
  320. * A taxonomy term array to look up and save.
  321. * @param $vid
  322. * A <em>numeric</em> vocabulary id (vid).
  323. *
  324. * @return
  325. * A taxonomy term array.
  326. */
  327. function data_taxonomy_save_term_array($term, $vid) {
  328. if (!isset($term[$vid])) {
  329. $term['vid'] = $vid;
  330. }
  331. if (!isset($term['tid']) || $term['vid'] != $vid) {
  332. if ($lookup = data_taxonomy_lookup_term($term['name'], $vid)) {
  333. $term = $term + $lookup;
  334. }
  335. }
  336. taxonomy_term_save($term /* TODO Term object replaces array $term */);
  337. return $term;
  338. }
  339. /**
  340. * Sanitize a term name depending on its vocabulary settings.
  341. */
  342. function data_taxonomy_sanitize($name, $vid) {
  343. $vocabulary = taxonomy_vocabulary_load($vid);
  344. if ($vocabulary->tags) {
  345. // Make sure there aren't any terms with a comma (=tag delimiter) in it.
  346. return preg_replace('/\s*,\s*/', ' ', $name);
  347. }
  348. return $name;
  349. }
  350. /**
  351. * Look up a term by name and vid.
  352. *
  353. * @param $name
  354. * Term name.
  355. * @param $vid
  356. * A <em>numeric</em> vocabulary id (vid).
  357. *
  358. * @return
  359. * A taxonomy term array if there is a term for $name/$vid, NULL otherwise.
  360. */
  361. function data_taxonomy_lookup_term($name, $vid) {
  362. static $terms;
  363. if (!isset($terms[$vid][$name])) {
  364. foreach (data_taxonomy_get_term_by_name_vid($name, $vid) as $term) {
  365. if ($term->vid == $vid) {
  366. $terms[$vid][$name] = (array) $term;
  367. }
  368. }
  369. }
  370. return isset($terms[$vid][$name]) ? $terms[$vid][$name] : NULL;
  371. }
  372. /**
  373. * Look up a term by name and vocabulary id.
  374. *
  375. * @see taxonomy_get_term_by_name().
  376. */
  377. function data_taxonomy_get_term_by_name_vid($name, $vid) {
  378. // TODO Please convert this statement to the D7 database API syntax.
  379. $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {taxonomy_term_data} t WHERE t.vid = %d AND LOWER(t.name) = LOWER('%s')", 't', 'tid'), $vid, trim($name));
  380. $result = array();
  381. while ($term = db_fetch_object($db_result)) {
  382. $result[] = $term;
  383. }
  384. return $result;
  385. }
  386. /**
  387. * Explode terms from typed input, create new terms.
  388. *
  389. * @param $typed_input
  390. * A comma separated list of terms.
  391. * @param $vid
  392. * A <em>numeric</em> vocabulary id (vid).
  393. *
  394. * @todo: This should actually live in taxonomy module.
  395. *
  396. * @return
  397. * Array of tids corresponding to the terms in typed_input.
  398. */
  399. function data_taxonomy_save_tags($typed_input, $vid) {
  400. $tids = array();
  401. foreach (drupal_explode_tags($typed_input) as $typed_term) {
  402. $term = data_taxonomy_save_term_name($typed_term, $vid);
  403. // Cast the edit as an object as though it were retrieved from the DB.
  404. $tids[$term['tid']] = (object) $term;
  405. }
  406. return $tids;
  407. }
  408. /**
  409. * AHAH callback for saving terms.
  410. *
  411. * @todo: Verify form token.
  412. */
  413. function data_taxonomy_ajax_save() {
  414. $cached_form_state = array();
  415. $files = array();
  416. $cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state);
  417. if ($cached_form['form_token']['#default_value'] == $_POST['form_token']) {
  418. // Rebuild $form_state['values'].
  419. $form_state = array('values' => $_POST);
  420. foreach (element_children($cached_form) as $elem) {
  421. if ($cached_form[$elem]['#type'] === 'value' && isset($cached_form[$elem]['#value'])) {
  422. $form_state['values'][$elem] = $cached_form[$elem]['#value'];
  423. }
  424. }
  425. // Process and save terms & relations.
  426. $values = $form_state['values'];
  427. $terms = data_taxonomy_save_tags($values['tags'], $values['vid']);
  428. data_taxonomy_save_relations($values['vid'], $values['id'], $values['table_name'], array_keys($terms));
  429. drupal_json_output(array('status' => 1, 'data' => theme('links', array('links' => data_taxonomy_tag_links($terms, $_POST['path'], explode('&', $_POST['args'])), 'attributes' => array('class' => 'links data-taxonomy-tags')))));
  430. exit;
  431. }
  432. drupal_json_output(array('status' => 1, 'data' => 'Error submitting form'));
  433. exit;
  434. }
  435. /**
  436. * Generate a links array suitable for use with theme('links') from an array of
  437. * taxonomy terms.
  438. *
  439. * @param $terms
  440. * An array of terms.
  441. * @param $path
  442. * The path template to use (e. g. path/%/!tid/%)
  443. * @param $args
  444. * The arguments to use in the path template, used to replace %'s in $path.
  445. */
  446. function data_taxonomy_tag_links($terms, $path, $args) {
  447. $tags = array();
  448. $path = _data_taxonomy_replace_tokens($path, $args);
  449. foreach ($terms as $tid => $term) {
  450. $tags[] = array(
  451. 'title' => $term->name,
  452. 'href' => str_replace('!term', $term->name, str_replace('!tid', $term->tid, $path)),
  453. );
  454. }
  455. return $tags;
  456. }
  457. /**
  458. * Replaces % in $path with arguments.
  459. *
  460. * @todo: Replace missing % not with 'all' but with value depending on argument
  461. * setting.
  462. *
  463. * @param $path
  464. * A path template like path/%/!tid/%
  465. * @param $args
  466. * An array of arguments used to replace % characters in path.
  467. * @return
  468. * A path with replaced tokens like path/arg1/!tid/arg2
  469. */
  470. function _data_taxonomy_replace_tokens($path, $args) {
  471. if (is_array($args)) {
  472. $args = array_filter($args);
  473. $pos = strpos($path, '%');
  474. while ($pos !== FALSE && count($args)) {
  475. $path = substr_replace($path, array_shift($args), $pos, 1);
  476. $pos = strpos($path, '%');
  477. }
  478. }
  479. $path = str_replace('%', 'all', $path);
  480. return $path;
  481. }
  482. /**
  483. * Preprocessor for theme('data_taxonomy_tagging_form').
  484. */
  485. function template_preprocess_data_taxonomy_tagging_form(&$vars) {
  486. drupal_add_js(drupal_get_path('module', 'data_taxonomy') . '/theme/data_taxonomy.js');
  487. drupal_add_css(drupal_get_path('module', 'data_taxonomy') . '/theme/data_taxonomy.css');
  488. $vars['label'] = $vars['form']['#vocab']->name;
  489. if ($vars['form']['#edit']) {
  490. $vars['edit'] = $vars['form']['#edit'];
  491. }
  492. $vars['tags'] = theme('links', array('links' => data_taxonomy_tag_links($vars['form']['#terms'], $vars['form']['#path'], $vars['form']['#args']), 'attributes' => array('class' => 'links data-taxonomy-tags')));
  493. }
  494. /**
  495. * Get a vocabulary by vid or module name.
  496. *
  497. * @param $id
  498. * A module name or a numeric vocabulary id.
  499. *
  500. * @return
  501. * An object of type stdClass that represents a vocabulary.
  502. */
  503. function data_taxonomy_get_vocabulary($id) {
  504. static $vocabularies;
  505. if (!isset($vocabularies[$id])) {
  506. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  507. if ($vocabulary->vid == $id) {
  508. $vocabularies[$id] = $vocabulary;
  509. break;
  510. }
  511. elseif ($vocabulary->module == $id) {
  512. $vocabularies[$id] = $vocabulary;
  513. break;
  514. }
  515. }
  516. }
  517. return $vocabularies[$id];
  518. }
  519. /**
  520. * Return the vocabulary identifier, the vocabulary's vid or module.
  521. *
  522. * @return
  523. * Vocabulary's module name if it is a features vocabulary (= exportable),
  524. * vocabulary's vid otherwise.
  525. */
  526. function data_taxonomy_vocabulary_id($vocabulary) {
  527. if (strpos($vocabulary->module, 'features_') === 0) {
  528. return $vocabulary->module;
  529. }
  530. return $vocabulary->vid;
  531. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.