/[drupal]/contributions/modules/opencalais/calais.ui.inc
ViewVC logotype

Contents of /contributions/modules/opencalais/calais.ui.inc

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


Revision 1.5 - (show annotations) (download) (as text)
Thu Apr 24 17:56:49 2008 UTC (19 months ago) by febbraro
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +11 -1 lines
File MIME type: text/x-php
added license
1 <?php
2 /*
3 Copyright (C) 2008 by Phase2 Technology.
4 Author(s): Frank Febbraro, Irakli Nadareishvili
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. See the LICENSE.txt file for more details.
10
11 $Id$
12 */
13
14 define('CALAIS_TERM_MAXLENGTH', 1000);
15
16 /**
17 * Implementation of hook_theme();
18 */
19 function calais_theme() {
20 return array(
21 'calais_suggestions' => array(
22 'arguments' => array('vid', 'terms'),
23 ),
24 );
25 }
26
27 /**
28 * Alter the standard node edit form to remove calais taxonomies so that they are
29 * only editable via the Calais tab.
30 * TODO: Make this configurable so the user can specify how/where they want to edit calais terms.
31 */
32 function calais_form_alter(&$form, $form_state, $form_id) {
33 if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
34
35 $node = $form['#node'];
36 $vocabs = calais_get_vocabularies($node->type);
37
38 foreach ($vocabs as $vocabulary) {
39 _calais_disable_taxonomy_field($form['taxonomy']['tags'][$vocabulary->vid]);
40 }
41 }
42 }
43
44 /**
45 * This function implements what is essentially disabling of a taxonomy field,
46 * but leaving it on the form so that values can be recreated by
47 * <code>taxonomy_node_save()</code> upon save.
48 */
49 function _calais_disable_taxonomy_field(&$field) {
50 $field['#type'] = 'hidden';
51 $field['#maxlength'] = CALAIS_TERM_MAXLENGTH;
52 }
53
54 /**
55 * Render the form for the Calais taxonomy & suggestions
56 */
57 function calais_keywords_form(&$form_state, $node) {
58 drupal_set_title(t('Calais Terms'));
59
60 $path = drupal_get_path('module', 'calais');
61 drupal_add_css("$path/calais.css");
62 drupal_add_js("$path/calais.js", 'module');
63
64 $vocabs = calais_get_vocabularies($node->type);
65
66 $form = array();
67 $form['#node'] = $node;
68 $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
69 $form['vid'] = array('#type' => 'value', '#value' => $node->vid);
70
71 foreach ($vocabs as $vocabulary) {
72 $keywords = calais_get_keywords($node->nid, $vocabulary->vid);
73 $suggestions = theme('calais_suggestions', $vocabulary->vid, $keywords[$vocabulary->vid]);
74 $current_tags = _calais_get_current_tags($node, $vocabulary->vid);
75 $has_keywords = sizeof($keywords[$vocabulary->vid]);
76
77 $form['calais'][$vocabulary->vid] = array(
78 '#type' => 'textfield',
79 '#title' => $vocabulary->name,
80 '#description' => $suggestions,
81 '#required' => $vocabulary->required,
82 '#default_value' => $current_tags,
83 '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
84 '#weight' => $vocabulary->weight,
85 '#size' => 75,
86 '#maxlength' => CALAIS_TERM_MAXLENGTH,
87 );
88
89 if ($has_keywords) {
90 $form['calais'][$vocabulary->vid]['#attributes'] = array(
91 'class' => 'keywords_available',
92 );
93 }
94 }
95
96 $form['calais']['#tree'] = TRUE;
97 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 45);
98
99 return $form;
100 }
101
102 /**
103 * Provide validation for the Calais free tagging taxonomy terms.
104 *
105 * The essence of this was clipped from <code>taxonomy_node_validate()</code>
106 */
107 function calais_keywords_form_validate($form, &$form_state) {
108 $node = (object)$form_state['values'];
109
110 if (!empty($node->calais)) {
111 $terms = $node->calais;
112 foreach ($terms as $vid => $vid_value) {
113 $vocabulary = taxonomy_vocabulary_load($vid);
114 if (empty($vocabulary->tags)) {
115 // see form_get_error $key = implode('][', $element['#parents']);
116 // on why this is the key
117 form_set_error("calais][$vid", t('The %name vocabulary can not be modified in this way. It is not setup for Free Tagging.', array('%name' => $vocabulary->name)));
118 }
119 }
120 }
121 }
122
123 /**
124 * Update the node for any Calais keyword modifications.
125 */
126 function calais_keywords_form_submit($form, &$form_state) {
127 $node = (object)$form_state['values'];
128
129 foreach ($node->calais as $vid => $vid_value) {
130 calais_remove_terms_for_node($node, $vid);
131
132 $terms = drupal_explode_tags($vid_value);
133 foreach ($terms as $term) {
134 $tid = calais_verify_and_get_term($vid, $term, 'taxonomy');
135 if ($tid) {
136 calais_assign_node_taxonomyterm($node->nid, $node->vid, $tid);
137 }
138 else {
139 watchdog('Calais', "Could not assign Taxonomy Term: $term");
140 }
141 }
142 }
143
144 $form_state['redirect'] = "node/{$node->nid}";
145 }
146
147 /**
148 * Remove all terms on a node for a particular vocabulary.
149 *
150 * @param $node The node to remove terms
151 * @param $vid The vocabulary whose terms will be removed from the node
152 */
153 function calais_remove_terms_for_node($node, $vid) {
154 $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
155 while ($term = db_fetch_object($result)) {
156 db_query('DELETE FROM {term_node} WHERE vid = %d AND tid = %d', $node->vid, $term->tid);
157 }
158 }
159
160 /**
161 * Process the node and get all specified terms for the current vocabulary
162 */
163 function _calais_get_current_tags($node, $vid) {
164 $current_tags = '';
165
166 if (isset($node->taxonomy)) {
167 $terms = $node->taxonomy;
168 $typed_terms = array();
169 foreach ($terms as $term) {
170 if ($term->vid == $vid) {
171 // Commas and quotes in terms are special cases, so encode 'em.
172 if (strpos($term->name, ',') !== FALSE || strpos($term->name, '"') !== FALSE) {
173 $term->name = '"'. str_replace('"', '""', $term->name) .'"';
174 }
175 $typed_terms[] = $term->name;
176 }
177 $current_tags = implode(',', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vid] : NULL);
178 }
179 }
180
181 return $current_tags;
182 }
183
184 /**
185 * Theme function to render the suggestions for a particular vocabulary
186 */
187 function theme_calais_suggestions($vid, $terms) {
188 if (sizeof($terms)) {
189 $suggestions .= "<div class='suggestions'>";
190 $suggestions .= t('Calais Suggestions: ');
191 foreach ($terms as $term) {
192 $suggestions .= "<label class='calais_keyword' for='edit-calais-$vid'>$term</label>";
193 }
194 $suggestions .= "</div>";
195 }
196
197 return $suggestions;
198 }
199

  ViewVC Help
Powered by ViewVC 1.1.2