diff --git a/sites/all/modules/content_taxonomy/LICENSE.txt b/sites/all/modules/content_taxonomy/LICENSE.txt new file mode 100644 index 0000000..2c095c8 --- /dev/null +++ b/sites/all/modules/content_taxonomy/LICENSE.txt @@ -0,0 +1,274 @@ +GNU GENERAL PUBLIC LICENSE + + Version 2, June 1991 + +Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, +Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute +verbatim copies of this license document, but changing it is not allowed. + + Preamble + +The licenses for most software are designed to take away your freedom to +share and change it. By contrast, the GNU General Public License is +intended to guarantee your freedom to share and change free software--to +make sure the software is free for all its users. This General Public License +applies to most of the Free Software Foundation's software and to any other +program whose authors commit to using it. (Some other Free Software +Foundation software is covered by the GNU Library General Public License +instead.) You can apply it to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the +freedom to distribute copies of free software (and charge for this service if +you wish), that you receive source code or can get it if you want it, that you +can change the software or use pieces of it in new free programs; and that +you know you can do these things. + +To protect your rights, we need to make restrictions that forbid anyone to +deny you these rights or to ask you to surrender the rights. These restrictions +translate to certain responsibilities for you if you distribute copies of the +software, or if you modify it. + +For example, if you distribute copies of such a program, whether gratis or for +a fee, you must give the recipients all the rights that you have. You must make +sure that they, too, receive or can get the source code. And you must show +them these terms so they know their rights. + +We protect your rights with two steps: (1) copyright the software, and (2) +offer you this license which gives you legal permission to copy, distribute +and/or modify the software. + +Also, for each author's protection and ours, we want to make certain that +everyone understands that there is no warranty for this free software. If the +software is modified by someone else and passed on, we want its recipients +to know that what they have is not the original, so that any problems +introduced by others will not reflect on the original authors' reputations. + +Finally, any free program is threatened constantly by software patents. We +wish to avoid the danger that redistributors of a free program will individually +obtain patent licenses, in effect making the program proprietary. To prevent +this, we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + +The precise terms and conditions for copying, distribution and modification +follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND + MODIFICATION + +0. This License applies to any program or other work which contains a notice +placed by the copyright holder saying it may be distributed under the terms +of this General Public License. The "Program", below, refers to any such +program or work, and a "work based on the Program" means either the +Program or any derivative work under copyright law: that is to say, a work +containing the Program or a portion of it, either verbatim or with +modifications and/or translated into another language. (Hereinafter, translation +is included without limitation in the term "modification".) Each licensee is +addressed as "you". + +Activities other than copying, distribution and modification are not covered +by this License; they are outside its scope. The act of running the Program is +not restricted, and the output from the Program is covered only if its contents +constitute a work based on the Program (independent of having been made +by running the Program). Whether that is true depends on what the Program +does. + +1. You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this License +and to the absence of any warranty; and give any other recipients of the +Program a copy of this License along with the Program. + +You may charge a fee for the physical act of transferring a copy, and you +may at your option offer warranty protection in exchange for a fee. + +2. You may modify your copy or copies of the Program or any portion of it, +thus forming a work based on the Program, and copy and distribute such +modifications or work under the terms of Section 1 above, provided that you +also meet all of these conditions: + +a) You must cause the modified files to carry prominent notices stating that +you changed the files and the date of any change. + +b) You must cause any work that you distribute or publish, that in whole or in +part contains or is derived from the Program or any part thereof, to be +licensed as a whole at no charge to all third parties under the terms of this +License. + +c) If the modified program normally reads commands interactively when run, +you must cause it, when started running for such interactive use in the most +ordinary way, to print or display an announcement including an appropriate +copyright notice and a notice that there is no warranty (or else, saying that +you provide a warranty) and that users may redistribute the program under +these conditions, and telling the user how to view a copy of this License. +(Exception: if the Program itself is interactive but does not normally print such +an announcement, your work based on the Program is not required to print +an announcement.) + +These requirements apply to the modified work as a whole. If identifiable +sections of that work are not derived from the Program, and can be +reasonably considered independent and separate works in themselves, then +this License, and its terms, do not apply to those sections when you distribute +them as separate works. But when you distribute the same sections as part +of a whole which is a work based on the Program, the distribution of the +whole must be on the terms of this License, whose permissions for other +licensees extend to the entire whole, and thus to each and every part +regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest your rights to +work written entirely by you; rather, the intent is to exercise the right to +control the distribution of derivative or collective works based on the +Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of a +storage or distribution medium does not bring the other work under the scope +of this License. + +3. You may copy and distribute the Program (or a work based on it, under +Section 2) in object code or executable form under the terms of Sections 1 +and 2 above provided that you also do one of the following: + +a) Accompany it with the complete corresponding machine-readable source +code, which must be distributed under the terms of Sections 1 and 2 above +on a medium customarily used for software interchange; or, + +b) Accompany it with a written offer, valid for at least three years, to give +any third party, for a charge no more than your cost of physically performing +source distribution, a complete machine-readable copy of the corresponding +source code, to be distributed under the terms of Sections 1 and 2 above on +a medium customarily used for software interchange; or, + +c) Accompany it with the information you received as to the offer to distribute +corresponding source code. (This alternative is allowed only for +noncommercial distribution and only if you received the program in object +code or executable form with such an offer, in accord with Subsection b +above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source code +means all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation and +installation of the executable. However, as a special exception, the source +code distributed need not include anything that is normally distributed (in +either source or binary form) with the major components (compiler, kernel, +and so on) of the operating system on which the executable runs, unless that +component itself accompanies the executable. + +If distribution of executable or object code is made by offering access to +copy from a designated place, then offering equivalent access to copy the +source code from the same place counts as distribution of the source code, +even though third parties are not compelled to copy the source along with the +object code. + +4. You may not copy, modify, sublicense, or distribute the Program except as +expressly provided under this License. Any attempt otherwise to copy, +modify, sublicense or distribute the Program is void, and will automatically +terminate your rights under this License. However, parties who have received +copies, or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + +5. You are not required to accept this License, since you have not signed it. +However, nothing else grants you permission to modify or distribute the +Program or its derivative works. These actions are prohibited by law if you +do not accept this License. Therefore, by modifying or distributing the +Program (or any work based on the Program), you indicate your acceptance +of this License to do so, and all its terms and conditions for copying, +distributing or modifying the Program or works based on it. + +6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these terms and +conditions. You may not impose any further restrictions on the recipients' +exercise of the rights granted herein. You are not responsible for enforcing +compliance by third parties to this License. + +7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), conditions +are imposed on you (whether by court order, agreement or otherwise) that +contradict the conditions of this License, they do not excuse you from the +conditions of this License. If you cannot distribute so as to satisfy +simultaneously your obligations under this License and any other pertinent +obligations, then as a consequence you may not distribute the Program at all. +For example, if a patent license would not permit royalty-free redistribution +of the Program by all those who receive copies directly or indirectly through +you, then the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply and +the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any patents or +other property right claims or to contest validity of any such claims; this +section has the sole purpose of protecting the integrity of the free software +distribution system, which is implemented by public license practices. Many +people have made generous contributions to the wide range of software +distributed through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing to +distribute software through any other system and a licensee cannot impose +that choice. + +This section is intended to make thoroughly clear what is believed to be a +consequence of the rest of this License. + +8. If the distribution and/or use of the Program is restricted in certain +countries either by patents or by copyrighted interfaces, the original copyright +holder who places the Program under this License may add an explicit +geographical distribution limitation excluding those countries, so that +distribution is permitted only in or among countries not thus excluded. In such +case, this License incorporates the limitation as if written in the body of this +License. + +9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will be +similar in spirit to the present version, but may differ in detail to address new +problems or concerns. + +Each version is given a distinguishing version number. If the Program specifies +a version number of this License which applies to it and "any later version", +you have the option of following the terms and conditions either of that +version or of any later version published by the Free Software Foundation. If +the Program does not specify a version number of this License, you may +choose any version ever published by the Free Software Foundation. + +10. If you wish to incorporate parts of the Program into other free programs +whose distribution conditions are different, write to the author to ask for +permission. For software which is copyrighted by the Free Software +Foundation, write to the Free Software Foundation; we sometimes make +exceptions for this. Our decision will be guided by the two goals of +preserving the free status of all derivatives of our free software and of +promoting the sharing and reuse of software generally. + + NO WARRANTY + +11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT +PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE +STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT +WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND +PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL +NECESSARY SERVICING, REPAIR OR CORRECTION. + +12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR +AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR +ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE +LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, +SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES +ARISING OUT OF THE USE OR INABILITY TO USE THE +PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA +OR DATA BEING RENDERED INACCURATE OR LOSSES +SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE +PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN +IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF +THE POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/sites/all/modules/content_taxonomy/README.txt b/sites/all/modules/content_taxonomy/README.txt new file mode 100644 index 0000000..5e26196 --- /dev/null +++ b/sites/all/modules/content_taxonomy/README.txt @@ -0,0 +1,48 @@ +Content Taxonomy: +***************** + +This module provides a field type for cck for referencing taxonomy terms. + +The content_taxonomy.module implements all basic field functions. +The other modules offer different widgets (selects,..) + +Installation: +------------- +1. Install this files into the modules directory of your choice (for example, sites/yoursite.com/modules/content_taxonomy). +2. Enable content_taxonomy.module in the admin/modules page +3. Enable wanted widgets in admin/modules (e.g. content_taxonomy_select.module) Note: all require content_taxonomy.module! +4. Optional enable content_taxonomy_views.module for improved taxonomy-views support + +Settings: +--------- +For every field you have to select the vocabulary and optional a parent term in the field settings. +If a parent is selected, only children of this parent will be given to the form, else the whole vocabulary. +Additional you can choose whether the term is saved as a real tag in the term_node table (standard) +or the term is only saved in a cck-table (so not a real term - node connection) + + + +content_taxonomy_options: +------------------------- + provides a options widget type + this includes: + - radios/checkboxes (depends on single/multiple) + - multiple/single select field (opt-groups are possible) + +content_taxonomy_autocomplete: +------------------------------ + provides autocomplete. synonyms are considered by this autocomplete. Optional you can restrict the + saving of new terms. + +content_taxonomy_activeselect: +------------------------------ + requires the activeselect.module + offers dependent selects, at the moment 2-3 bars are possible + + + +Author: +------- +Matthias Hutterer +mh86@drupal.org +m_hutterer@hotmail.com diff --git a/sites/all/modules/content_taxonomy/content_taxonomy.info b/sites/all/modules/content_taxonomy/content_taxonomy.info new file mode 100644 index 0000000..c20c6c1 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy.info @@ -0,0 +1,15 @@ +; $Id: content_taxonomy.info,v 1.1.2.2.2.1 2008/04/30 08:05:19 mh86 Exp $ +name = Content Taxonomy +description = Defines a field type for taxonomy terms +dependencies[] = content +dependencies[] = taxonomy +package = CCK +core = 6.x + + +; Information added by drupal.org packaging script on 2009-08-19 +version = "6.x-1.0-rc2" +core = "6.x" +project = "content_taxonomy" +datestamp = "1250688034" + diff --git a/sites/all/modules/content_taxonomy/content_taxonomy.install b/sites/all/modules/content_taxonomy/content_taxonomy.install new file mode 100644 index 0000000..88f752f --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy.install @@ -0,0 +1,210 @@ + $fields) { + foreach ($fields as $field) { + switch ($field['type']) { + case 'content_taxonomy': + $db_info = content_database_info($field); + $table = $db_info['table']; + + //fix old settings first and map them to the new ones. + $result = db_query("SELECT * FROM {". content_field_tablename() ."} WHERE type = 'content_taxonomy' AND field_name = '%s'", $field['field_name']); + while ($field = db_fetch_array($result)) { + $field_settings = unserialize($field['global_settings']); + $field_settings['save_term_node'] = isset($field_settings['save_term_node']) ? $field_settings['save_term_node'] : 0; + if ($field_settings['save'] == 'tag') { + $field_settings['save_term_node'] = 1; + $field['type_name'] = $type_name; + $field['columns'] = $db_info['columns']; + content_alter_schema(array(), $field); + + $sql_nodes = db_query("SELECT * FROM {node} WHERE type = '%s'", $type_name); + while ($node = db_fetch_object($sql_nodes)) { + $tids = content_taxonomy_install_terms_by_field($node, $field_settings); + foreach ($tids as $tid) { + $record = array(); + foreach ($db_info['columns'] as $column => $attributes) { + $record[$attributes['column']] = $tid; + } + $record['nid'] = $node->nid; + $record['vid'] = $node->vid; + if ($field['multiple']) { + $record['delta'] = $tid; + } + if (db_result(db_query("SELECT COUNT(*) FROM {". $table ."} WHERE vid = %d", $node->vid))) { + content_write_record($table, $record, array('vid')); + } + else { + content_write_record($table, $record); + } + } + } + } + if ($field_settings['save'] == 'both') { + $field_settings['save_term_node'] = 1; + } + unset($field_settings['save']); + $field_settings['hide_taxonomy_fields'] = isset($field_settings['hide_taxonomy_fields']) ? $field_settings['hide_taxonomy_fields'] : TRUE; + $field_settings['parent'] = $field_settings['tid']; + + $field['global_settings'] = array(); + $setting_names = module_invoke($field['module'], 'field_settings', 'save', $field); + if (is_array($setting_names)) { + foreach ($setting_names as $setting) { + $field['global_settings'][$setting] = isset($field_settings[$setting]) ? $field_settings[$setting] : ''; + } + } + // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'. + $field['db_columns'] = $field['columns']; + drupal_write_record(content_field_tablename(), $field, 'field_name'); + } + + foreach ($db_info['columns'] as $column => $attributes) { + $attributes['not null'] = FALSE; + $column = $attributes['column']; + db_change_field($ret, $table, $column, $column, $attributes); + db_field_set_no_default($ret, $table, $column); + $ret[] = update_sql("UPDATE {". $table ."} SET ". $column ." = NULL WHERE ". $column ." = 0"); + + } + break; + } + } + } + content_associate_fields('content_taxonomy'); + content_associate_fields('content_autocomplete'); + content_associate_fields('content_options'); + content_clear_type_cache(TRUE); + return $ret; +} + +/** + * Implemenation of hook_update_N(). + * + * Renaming of the Parent setting + */ +function content_taxonomy_update_6001() { + drupal_load('module', 'content'); + $result = db_query("SELECT * FROM {". content_field_tablename() ."} WHERE type = 'content_taxonomy'"); + while ($field = db_fetch_array($result)) { + $field['global_settings'] = unserialize($field['global_settings']); + if (!is_null($field['global_settings']['tid'])) { + $field['global_settings']['parent'] = $field['global_settings']['tid']; + unset($field['global_settings']['tid']); + } + // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'. + $field['db_columns'] = $field['columns']; + drupal_write_record(content_field_tablename(), $field, 'field_name'); + } + $result = db_query("SELECT * FROM {". content_instance_tablename() ."} WHERE widget_type = 'content_taxonomy_select'"); + while ($field = db_fetch_array($result)) { + $field['widget_settings'] = unserialize($field['widget_settings']); + $field['widget_settings']['group_parent'] = $field['widget_settings']['group_tid']; + unset($field['widget_settings']['group_tid']); + drupal_write_record(content_instance_tablename(), $field, array('field_name', 'type_name')); + } + return array(); +} + +/** + * Implemenation of hook_update_N(). + * + * Fixing parent setting + */ +function content_taxonomy_update_6002() { + drupal_load('module', 'content'); + $result = db_query("SELECT * FROM {". content_field_tablename() ."} WHERE type = 'content_taxonomy'"); + while ($field = db_fetch_array($result)) { + $field['global_settings'] = unserialize($field['global_settings']); + if (!$field['global_settings']['parent']) { + $field['global_settings']['parent'] = 0; + } + // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'. + $field['db_columns'] = $field['columns']; + drupal_write_record(content_field_tablename(), $field, 'field_name'); + } + return array(); +} +/** + * Helper function to get values from term_node table + * needed in update from 5.x to 6.x + */ +function content_taxonomy_install_terms_by_field($node, $field) { + $terms = array(); + if (is_numeric($field['tid']) && $field['tid'] > 0 && $field['depth'] == 1) { + $result = db_query("SELECT n.tid FROM {term_hierarchy} h, {term_node} n, {term_data} td WHERE + n.nid = %d AND n.tid = h.tid AND h.parent = %d AND td.tid=n.tid AND td.vid=%d", + $node->nid, $field['tid'], $field['vid']); + + while ($data = db_fetch_array($result)) { + $terms[$data["tid"]] = $data["tid"]; + } + } + else { + $result = taxonomy_node_get_terms_by_vocabulary($node, $field['vid']); + foreach ($result as $tid => $term) { + $terms[$tid] = $tid; + } + } + + return $terms; +} diff --git a/sites/all/modules/content_taxonomy/content_taxonomy.module b/sites/all/modules/content_taxonomy/content_taxonomy.module new file mode 100644 index 0000000..537f3d0 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy.module @@ -0,0 +1,540 @@ +'. t('Defines a CCK field type for referencing taxonomy terms. The fields are independent from vocabulary settings and can be configured through the CCK admin field pages. The Content Taxonomy Module provides different widget types, at the moment including Option Widgets (Radios / Checkboxes, Selects), Autocompletes, Tree). The widget modules have to be enabled separately.') .'
'; + } +} + +/** + * Implementation of hook_init(). + */ +function content_taxonomy_init() { + if (module_exists('token')) { + module_load_include('inc', 'content_taxonomy', 'includes/content_taxonomy.token'); + } +} + +/** + * Implementation of hook_theme(). + */ +function content_taxonomy_theme() { + return array( + 'content_taxonomy_formatter_default' => array( + 'arguments' => array('element' => NULL), + ), + 'content_taxonomy_formatter_link' => array( + 'arguments' => array('element' => NULL), + ), + ); +} + +/** + * Implementation of hook_field_info(). + */ +function content_taxonomy_field_info() { + return array( + 'content_taxonomy' => array( + 'label' => t('Content Taxonomy Fields'), + 'description' => t('Stores terms for nodes in the database.'), + 'callbacks' => array( + 'tables' => CONTENT_CALLBACK_DEFAULT, + 'arguments' => CONTENT_CALLBACK_DEFAULT, + ), + ), + ); +} + +/** + * Implementation of hook_field_settings(). + */ +function content_taxonomy_field_settings($op, $field) { + switch ($op) { + case 'form': + $form = array(); + + $form['save_term_node'] = array( + '#type' => 'checkbox', + '#title' => t('Save values additionally to the core taxonomy system (into the \'term_node\' table).'), + '#default_value' => is_numeric($field['save_term_node']) ? $field['save_term_node'] : 0, + '#description' => t('If this option is set, saving of terms is additionally handled by the taxonomy module. So saved terms from Content Taxonomy fields will appear as any other terms saved by the core taxonomy module. Set this option if you are using any other taxonomy application, like tagadelic. Otherwise terms are only saved in the cck tables and can only be accessed via the node or a view'), + ); + + $options_voc = array(); + foreach (taxonomy_get_vocabularies() as $voc) { + _content_taxonomy_localize_vocabulary($voc); + $options_voc[$voc->vid] = $voc->name; + } + + $form['vid'] = array( + '#title' => t('Vocabulary'), + '#type' => 'select', + '#default_value' => is_numeric($field['vid']) ? $field['vid'] : 0, + '#options' => $options_voc, + '#description' => t('Terms of the selected vocabulary get exposed to the field'), + ); + + $form['hierarchical_vocabulary'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced settings for hierarchical vocabularies'), + '#collapsible' => TRUE, + ); + + $form['hierarchical_vocabulary']['parent'] = array( + '#title' => t('Parent Term'), + '#type' => 'select', + '#default_value' => is_numeric($field['parent']) ? $field['parent'] : 0, + '#options' => _content_taxonomy_get_all_terms(), + '#description' => t('If any term is selected here, only child terms of the selected are going to be exposed the field. Otherwise the whole vocabulary selected above'), + ); + $form['hierarchical_vocabulary']['parent_php_code_fieldset'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced PHP code'), + '#collapsible' => TRUE, + '#collapsed' => empty($field['parent_php_code']) , + ); + $form['hierarchical_vocabulary']['parent_php_code_fieldset']['parent_php_code'] = array( + '#title' => t('PHP Code for selecting the parent term'), + '#type' => 'textarea', + '#default_value' => !empty($field['parent_php_code']) ? $field['parent_php_code'] : '', + '#rows' => 3, + '#description' => t('Advanced usage only: PHP code that returns the parent term ID. Should not include <?php ?> delimiters. If this field is filled out, the ID returned by this code will override the selected parent above.'), + ); + + $form['hierarchical_vocabulary']['depth'] = array( + '#type' => 'textfield', + '#title' => t('Depth of taxonomy tree'), + '#default_value' => is_numeric($field['depth']) ? $field['depth'] : '', + '#description' => t('By setting a numeric value, the depth of the hierarchy shown can be limited. Leave this field blank to show the whole hierarchy.'), + ); + + + return $form; + + case 'save': + return array('save_term_node', 'vid', 'parent', 'parent_php_code', 'depth'); + + case 'database columns': + return array( + 'value' => array('type' => 'int', 'not null' => FALSE, 'sortable' => FALSE), + ); + + case 'views data': + $data = content_views_field_views_data($field); + $table_alias = content_views_tablename($field); + + // Add a relation to the taxonomy term table. + $data[$table_alias][$field['field_name'] .'_value']['relationship'] = array( + 'handler' => 'views_handler_relationship', + 'base' => 'term_data', + 'field' => 'tid', + 'label' => t('@field-title term', array('@field-title' => check_plain(t($field['widget']['label'])))), + ); + + // Swap the filter handler to the 'in' operator. + $data[$table_alias][$field['field_name'] .'_value']['filter']['handler'] = 'content_taxonomy_handler_filter_many_to_one'; + return $data; + } +} + +/** + * Implementation of hook_field(). + */ +function content_taxonomy_field($op, &$node, $field, &$items, $teaser, $page) { + switch ($op) { + case 'presave': + if ($field['save_term_node']) { + static $_content_taxonomy_array_cleared; + if (!is_array($_content_taxonomy_array_cleared) || !$_content_taxonomy_array_cleared[$node->nid][$field['vid']]) { + _content_taxonomy_taxonomy_unset($node->taxonomy, array($field['vid'])); + $_content_taxonomy_array_cleared[$node->nid][$field['vid']] = TRUE; + } + + foreach ($items as $key => $entry) { + if ($entry['value']) { + if (is_object($node->taxonomy[$entry['value']]) + || (is_array($node->taxonomy) && in_array($entry['value'], $node->taxonomy)) + || (isset($entry['_remove']) && $entry['_remove'] == 1)) { + continue; + } + elseif (is_array($node->taxonomy[$field['vid']])) { + if (!in_array($entry['value'], $node->taxonomy[$field['vid']])) { + $node->taxonomy[$field['vid']][] = $entry['value']; + } + } + // when saving an existing node without presenting a form to the user, + // the terms are objects keyed by tid. there's no need to re-set these + // terms, and to do so causes php warnings because the database rejects + // the row insert because of primary key constraints. + else { + $node->taxonomy[$field['vid']] = array($entry['value']); + } + } + } + // the $node->taxonomy array should never be empty, because in this case the + // taxonomy nodeapi doesn't call taxonomy_node_save which handles removing + // and inserting of terms + if (empty($node->taxonomy)) { + $node->taxonomy[$field['vid']] = NULL; + } + } + break; + } +} + + +/** + * Implementation of hook_field_formatter_info(). + */ +function content_taxonomy_field_formatter_info() { + return array( + 'default' => array( + 'label' => t('As Text'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_CORE, + ), + 'link' => array( + 'label' => t('As Link'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_CORE, + ), + ); +} + + +/** + * Theme function for 'default' text field formatter. + */ +function theme_content_taxonomy_formatter_default($element) { + if (!empty($element['#item']['value'])) { + $term = taxonomy_get_term($element['#item']['value']); + _content_taxonomy_localize_term($term); + return check_plain($term->name); + } +} + + +/** + * Theme function for 'link' text field formatter. + */ +function theme_content_taxonomy_formatter_link($element) { + if (!empty($element['#item']['value'])) { + $term = taxonomy_get_term($element['#item']['value']); + _content_taxonomy_localize_term($term); + return l($term->name, taxonomy_term_path($term), array('attributes' => array('rel' => 'tag', 'title' => $term->description))); + } +} + +/** + * Implementation of hook_content_is_empty(). + */ +function content_taxonomy_content_is_empty($item, $field) { + if (empty($item['value']) || $item['value'] == 0) { + return TRUE; + } + return FALSE; +} + +/** + * Called by content_allowed_values to create the $options array for the content_taxonomy_options + */ +function content_taxonomy_allowed_values($field) { + $options = array(); + + //for opt groups call different function + if (isset($field['widget']['group_parent']) && $field['widget']['group_parent'] > 0) { + return content_taxonomy_allowed_values_groups($field); + } + + $depth = (is_numeric($field['depth'])) ? $field['depth'] : NULL; + + $tree = taxonomy_get_tree($field['vid'], content_taxonomy_field_get_parent($field), -1, $depth); + + if (is_array($tree)) { + foreach ($tree as $term) { + _content_taxonomy_localize_term($term); + if ($field['widget']['show_depth']) { + $value = str_repeat(' - ', $term->depth) . $term->name; + } + else { + $value = $term->name; + } + //do a check_plain except for selects because form api does that + $options[$term->tid] = ($field['widget']['type'] == 'content_taxonomy_select') ? $value : check_plain($value); + } + } + if (($field['widget']['type'] == 'content_taxonomy_select' && !$field['multiple']) || ($field['widget']['type'] == 'content_taxonomy_options' && !$field['multiple'] && !$field['required'])) { + $options = array('' => theme('content_taxonomy_options_widgets_none', $field)) + $options; + } + return $options; +} + +/** + * Creating Opt Groups for content_taxonomy_options + */ +function content_taxonomy_allowed_values_groups($field) { + $options = array(); + $parent = content_taxonomy_field_get_parent($field); + $group_parent = $field['widget']['group_parent']; + + //if children in no group + $default_terms = taxonomy_get_children($parent, $field['vid']); + foreach ($default_terms as $default_term) { + _content_taxonomy_localize_term($default_term); + $options[$default_term->tid] = check_plain($default_term->name); + } + foreach (taxonomy_get_children($group_parent) as $group) { + foreach (taxonomy_get_children($group->tid) as $term) { + _content_taxonomy_localize_term($term); + $options[$group->name][$term->tid] = check_plain($term->name); + unset($options[$term->tid]); + } + unset($options[$group->tid]); + } + return $options; +} + +/** + * Returns the parent term ID for one field + * can be 0 is no parent is selected or if the php code returns 0 + * + * use this function instead of directly accessing $field['parent'] because of possible php code + * + * @param $field The Content Taxonomy Field + */ +function content_taxonomy_field_get_parent($field) { + if (!empty($field['parent_php_code'])) { + return eval($field['parent_php_code']); + } + return $field['parent']; +} + + +/** + * Implementation of hook_form_alter(). + * + * hides the taxonomy form if there exists a content taxonomy field, which is set to 'hide default taxonomy fields' + */ +function content_taxonomy_form_alter(&$form, $form_state, $form_id) { + if (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id && isset($form['taxonomy'])) { + if (!is_array($form['taxonomy'])) { + // empty taxonomy arrays can cause errors on preview + if ($form_state['post']['op'] == t('Preview')) { + $form['taxonomy'] = array('#tree' => TRUE); + $form['taxonomy']['key'] = array('#type' => 'value', '#value' => ''); + return; + } + } + $_content_taxonomy_vids = array(); + $info = _content_type_info(); + $content_type = $info['content types'][$form['type']['#value']]; + foreach ($content_type['fields'] as $field) { + if ($field['type'] == 'content_taxonomy' && (!in_array($field['vid'], $_content_taxonomy_vids))) { + $_content_taxonomy_vids[] = $field['vid']; + } + } + _content_taxonomy_taxonomy_unset($form['taxonomy'], $_content_taxonomy_vids); + + //hide empty 'Vocabularies' fieldsets + $empty_fieldset = TRUE; + if (is_array($form['taxonomy'])) { + foreach ($form['taxonomy'] as $key => $value) { + if (is_array($value) && !empty($value)) { + $empty_fieldset = FALSE; + break; + } + } + } + if ($empty_fieldset) { + // creating an empty taxonomy array is causing errors on preview in the taxonomy module + // so we create an empty value field as placeholder, which is going to prevent the errors + $form['taxonomy'] = array('#tree' => TRUE); + $form['taxonomy']['key'] = array('#type' => 'value', '#value' => ''); + } + } +} + +/** + * Implementation of hook_taxonomy(). + */ +function content_taxonomy_taxonomy($op, $type, $array = NULL) { + //keep the database consistent when either a voc or a term is deleted + //in case of voc: delete all associated fields and its data + //in case of term: delete all its entries + if ($op == 'delete') { + $vid = $array['vid']; + $needs_refresh = FALSE; + foreach (content_taxonomy_fields() as $type_name => $ct_type) { + foreach ($ct_type['fields'] as $field_name => $field) { + if ($field['vid'] == $vid) { + if ($type == 'vocabulary') { + module_load_include('inc', 'content', 'includes/content.crud'); + content_field_instance_delete($field_name, $field['type_name']); + $needs_refresh = TRUE; + watchdog('content', 'Deleted field %field_name and its data.', array('%field_name' => $field_name)); + } + else { + $tid = $array['tid']; + if ($tid) { + $db_info = content_database_info($field); + if ($field['multiple']) { + db_query('DELETE FROM {'. $db_info['table'] .'} WHERE '. $db_info['columns']['value']['column'] .' = %d', $tid); + } + else { + db_query('UPDATE {'. $db_info['table'] .'} SET '. $db_info['columns']['value']['column'] .'= NULL WHERE '. $db_info['columns']['value']['column'] .' = %d', $tid); + } + $needs_refresh = TRUE; + watchdog('content', 'Entries with term id = %tid have been deleted out of %table for field %field_name.', array('%tid' => $tid, '%table' => $db_info['table'], '%field_name' => $field_name)); + } + } + } + } + } + if ($needs_refresh) { + content_clear_type_cache(TRUE); + } + } +} + +/** + * Implementation of hook_views_api(). + */ +function content_taxonomy_views_api() { + return array( + 'api' => 2, + 'path' => drupal_get_path('module', 'content_taxonomy') . '/includes/views', + ); +} + + +/** + * Helper function that returns all content taxonomy fields, nested in their content types + */ +function content_taxonomy_fields() { + static $ct_fields; + + if (!is_array($ct_fields)) { + $ct_fields = array(); + foreach (content_types() as $type_name => $type) { + foreach ($type['fields'] as $field_name => $field) { + if ($field['type'] == 'content_taxonomy') { + $ct_fields[$type_name]['fields'][$field_name] = $field; + } + } + } + } + return $ct_fields; +} + + +/** + * Helper functions that returns all terms group by their vocabulary + * + * needed for some settings forms + */ +function _content_taxonomy_get_all_terms() { + static $options = array(); + if (!count($options)) { + $options[0] = '---'; + foreach (taxonomy_get_vocabularies() as $voc) { + foreach (taxonomy_get_tree($voc->vid) as $term) { + _content_taxonomy_localize_term($term); + $options[$voc->name][$term->tid] = str_repeat('- ', $term->depth) . $term->name; + } + } + } + return $options; +} + +/** + * Localize a term by replacing its name attribute with its localized version + * for the current language. + * + * @param $term + * The term to localize. + * + * This is based on i18ntaxonomy_localize_terms(), but with less overhead. + */ +function _content_taxonomy_localize_term(&$term) { + // If this term's vocabulary supports localization. + if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { + $term->name = tt("taxonomy:term:$term->tid:name", $term->name); + } +} + +/** + * Localize a vocabulary by replacing its name attribute with its localized + * version for the current language. + * + * @param $vocabulary + * The vocabulary to localize. + */ +function _content_taxonomy_localize_vocabulary(&$vocabulary) { + // If this vocabulary supports localization. + if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($vocabulary->vid) == I18N_TAXONOMY_LOCALIZE) { + $vocabulary->name = tt("taxonomy:vocabulary:$vocabulary->vid:name", $vocabulary->name); + } +} + +/** + * carefully unsets node or node form taxonomy items + * + * + * @param $form the node or node form's taxonomy selections. nodes are objects and forms are arrays, so only the actual taxonomy value or property is passed + * @param $vids an array containing a list of vocabulary IDs whose terms should be unset + */ +function _content_taxonomy_taxonomy_unset(&$form, $vids) { + if (empty($vids) || !is_array($form)) { + return; + } + foreach ($form as $key => $value) { + if ($key == 'tags') { + _content_taxonomy_taxonomy_unset($form[$key], $vids); + } + elseif (is_object($value) && in_array($value->vid, $vids)) { + unset($form[$key]); + } + elseif (in_array($key, $vids)) { + unset($form[$key]); + } + } +} + +/** + * Implementation of hook_content_multigroup_allowed_widgets(). + */ +function content_taxonomy_content_multigroup_allowed_widgets() { + return array('content_taxonomy_autocomplete', 'content_taxonomy_options', 'content_taxonomy_select'); +} + +/** + * Implementation of hook_content_multigroup_no_remove_widgets(). + */ +function content_taxonomy_content_multigroup_no_remove_widgets() { + return array('content_taxonomy_autocomplete', 'content_taxonomy_options', 'content_taxonomy_select'); +} + +/** + * Implementation of hook_content_diff_values + */ +function content_taxonomy_content_diff_values($node, $field, $items) { + $return = array(); + foreach ($items as $item) { + if (isset($item['value']) && is_numeric($item['value'])) { + $term = taxonomy_get_term($item['value']); + $return[] = $term->name; + } + } + return $return; +} diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.info b/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.info new file mode 100644 index 0000000..eb54b68 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.info @@ -0,0 +1,15 @@ +; $Id: content_taxonomy_autocomplete.info,v 1.1.2.4.2.1 2008/04/30 08:05:19 mh86 Exp $ +name = Content Taxonomy Autocomplete +description = Defines a autocomplete widget type for content_taxonomy +dependencies[] = content +dependencies[] = content_taxonomy +dependencies[] = taxonomy +package = CCK +core = 6.x + +; Information added by drupal.org packaging script on 2009-08-19 +version = "6.x-1.0-rc2" +core = "6.x" +project = "content_taxonomy" +datestamp = "1250688034" + diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.install b/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.install new file mode 100644 index 0000000..eb129c0 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_autocomplete.install @@ -0,0 +1,38 @@ + array( + 'arguments' => array('element' => NULL), + ), + ); +} + +/** + * Implementation of hook_menu + */ +function content_taxonomy_autocomplete_menu() { + $items['content_taxonomy/autocomplete'] = array( + 'title' => 'Autocomplete', + 'page callback' => 'content_taxonomy_autocomplete_load', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK + ); + return $items; +} + +/** + * Implementation of hook_widget_info(). + */ +function content_taxonomy_autocomplete_widget_info() { + return array( + 'content_taxonomy_autocomplete' => array( + 'label' => t('Autocomplete (Freetagging)'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_MODULE, + 'callbacks' => array( + 'default value' => CONTENT_CALLBACK_DEFAULT, + ), + ), + ); + return $items; +} + +/** + * Implementation of hook_widget_settings + */ +function content_taxonomy_autocomplete_widget_settings($op, $widget) { + switch ($op) { + case 'form': + $form['autocomplete'] = array( + '#type' => 'fieldset', + '#title' => t('Settings for Autocompletes'), + '#collapsible' => TRUE, + '#weight' => 10, + ); + $form['autocomplete']['new_terms'] = array( + '#type' => 'radios', + '#title' => t('Freetagging settings'), + '#default_value' => isset($widget['new_terms']) ? $widget['new_terms'] : 'insert', + '#options' => array('insert' => t('Allow and insert new terms by the user into the vocabulary'), + 'deny' => t('Deny any new terms'), + ), + ); + $form['autocomplete']['extra_parent'] = array( + '#type' => 'select', + '#title' => t('Extra Parent for new terms'), + '#options' => _content_taxonomy_get_all_terms(), + '#default_value' => (isset($widget['extra_parent']) && is_numeric($widget['extra_parent'])) ? $widget['extra_parent'] : 0, + '#description' => t('This setting is only relevant if you have selected "Allow and insert new terms by the user into the vocabulary". If you select any term here, new terms will get children of the selected one, otherwise new terms get children of the parent term (root, if no parent selected) selected in the global settings.'), + ); + + $form['autocomplete']['maxlength'] = array( + '#type' => 'textfield', + '#title' => t('Maximum length of autocomplete'), + '#default_value' => (isset($widget['maxlength']) && is_numeric($widget['maxlength'])) ? $widget['maxlength'] : 255, + '#element_validate' => array('_content_taxonomy_autocomplete_widget_settings_maxlength_validate'), + '#required' => TRUE, + '#description' => t('Defines how many characters can be typed into the autocomplete field. For values higher than 255, remember that one term name can not be longer than 255 (would be cutted), nevertheless it\'s not a problem for multiple values, separated by commas.'), + ); + if (module_exists('active_tags')) { + $form['autocomplete']['active_tags'] = array( + '#type' => 'checkbox', + '#title' => t('Use Active Tags style widget'), + '#default_value' => isset($widget['active_tags']) ? $widget['active_tags'] : 0, + '#description' => t('Use the Active Tags module to improve the usability of this autocomplete widget.'), + ); + } + return $form; + + case 'save': + return array('new_terms', 'extra_parent', 'maxlength', 'active_tags'); + } +} + +function _content_taxonomy_autocomplete_widget_settings_maxlength_validate($element, &$form_state) { + $value = $form_state['values']['maxlength']; + if (!is_numeric($value) || intval($value) != $value || $value <= 0) { + form_error($element, t('"Maximum length" must be a positive integer.')); + } +} + +/** + * Implementation of FAPI hook_elements(). + * + * Any FAPI callbacks needed for individual widgets can be declared here, + * and the element will be passed to those callbacks for processing. + * + * Drupal will automatically theme the element using a theme with + * the same name as the hook_elements key. + * + * Autocomplete_path is not used by text_widget but other widgets can use it + * (see nodereference and userreference). + */ +function content_taxonomy_autocomplete_elements() { + return array( + 'content_taxonomy_autocomplete' => array( + '#input' => TRUE, + '#columns' => array('value'), + '#delta' => 0, + '#process' => array('content_taxonomy_autocomplete_process'), + '#autocomplete_path' => FALSE, + ), + ); +} + +/** + * Implementation of hook_widget(). + */ +function content_taxonomy_autocomplete_widget(&$form, &$form_state, $field, $items, $delta = NULL) { + $element = array( + '#type' => 'content_taxonomy_autocomplete', + '#default_value' => isset($items) ? $items : NULL, + '#value_callback' => 'content_taxonomy_autocomplete_value', + '#vid' => $field['vid'], + ); + return $element; +} + +/** + * Value for a content taxonomy autocomplete field + * + * returns the taxonomy term name for term ids + */ +function content_taxonomy_autocomplete_value($element, $edit = FALSE) { + $field_key = $element['#columns'][0]; + $terms = array(); + if (count($element['#default_value'])) { + foreach ($element['#default_value'] as $delta => $entry) { + $terms[] = taxonomy_get_term($entry[$field_key]); + } + } + $value = content_taxonomy_autocomplete_merge_tags($terms, $element['#vid']); + $value = !empty($value) ? $value : NULL; + return array($field_key => $value); +} + + +/** + * Process an individual element. + * + * Build the form element. When creating a form using FAPI #process, + * note that $element['#value'] is already set. + * + */ +function content_taxonomy_autocomplete_process($element, $edit, $form_state, $form) { + $field_name = $element['#field_name']; + $field = $form['#field_info'][$field_name]; + $field_key = $element['#columns'][0]; + + $element[$field_key] = array( + '#type' => 'textfield', + '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : '', + '#autocomplete_path' => 'content_taxonomy/autocomplete/'. $element['#field_name'], + '#title' => $element['#title'], + '#required' => $element['#required'], + '#description' => $element['#description'], + '#field_name' => $element['#field_name'], + '#type_name' => $element['#type_name'], + '#delta' => $element['#delta'], + '#columns' => $element['#columns'], + '#maxlength' => !empty($field['widget']['maxlength']) ? $field['widget']['maxlength'] : 255, + ); + + if (empty($element[$field_key]['#element_validate'])) { + $element[$field_key]['#element_validate'] = array(); + } + array_unshift($element[$field_key]['#element_validate'], 'content_taxonomy_autocomplete_validate'); + + if (module_exists('active_tags') && $field['widget']['active_tags']) { + active_tags_enable_widget('#' . $element['#id'] . '-value-wrapper'); + } + + return $element; +} + +/** + * Validation function for the content_taxonomy_autocomplete element + * + * parses input, handles new terms (depending on settings) and sets the values as needed for storing the data + */ +function content_taxonomy_autocomplete_validate($element, &$form_state) { + $field_name = $element['#field_name']; + $field = content_fields($field_name, $element['#type_name']); + $field_key = $element['#columns'][0]; + + //if the element parents array contains the field key, we have to remove it + //because otherwise form_set_value won't work. (still the question why is it in) + if ($element['#parents'][count($element['#parents'])-1] == $field_key) { + array_pop($element['#parents']); + array_pop($element['#array_parents']); + } + + $value = $element['#value']; + + $extracted_ids = content_taxonomy_autocomplete_tags_get_tids($value, $field['vid'], content_taxonomy_field_get_parent($field), $field['widget']['extra_parent']); + + if (!$field['multiple'] && count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > 1) { + form_set_error($field['field_name'] .'][value', t('You can provide only one value')); + return; + } + else if (($field['multiple'] >= 2) && (count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > $field['multiple'])) { + form_set_error($field['field_name'] .'][value', t('%name: this field cannot hold more than @count values.', array('%name' => t($field['widget']['label']), '@count' => $field['multiple']))); + } + if ($field['widget']['new_terms'] == 'deny') { + if (is_array($extracted_ids['non_existing_terms'])) { + form_set_error($field['field_name'] .'][value', t('New tags are not allowed')); + return; + } + } + + $values = content_taxonomy_autocomplete_form2data($extracted_ids, $field, $element); + form_set_value($element, $values, $form_state); +} + +/** + * Helper function to transpose the values returned by submitting the content_taxonomy_autcomplete + * to the format to be stored in the field + */ +function content_taxonomy_autocomplete_form2data($extracted_ids, $field, $element) { + $existing_tids = is_array($extracted_ids['existing_tids']) ? $extracted_ids['existing_tids'] : array(); + $new_tids = array(); + if (is_array($extracted_ids['non_existing_terms'])) { + if ($field['widget']['extra_parent']) { + $new_tids = content_taxonomy_autocomplete_insert_tags($extracted_ids['non_existing_terms'], $field['widget']['extra_parent']); + } + else { + $new_tids = content_taxonomy_autocomplete_insert_tags($extracted_ids['non_existing_terms'], content_taxonomy_field_get_parent($field)); + } + } + return content_transpose_array_rows_cols(array($element['#columns'][0] => array_merge($existing_tids, $new_tids))); + +} + + +/** + * Retrieve a pipe delimited string of autocomplete suggestions + * + * @param String Fieldname + * @param Integer TID of a parent (optional) + * @param BOOLEAN whether a multiple field or not + * @param STRING typed input + */ +function content_taxonomy_autocomplete_load($field_name, $string = '') { + // The user enters a comma-separated list of tags. We only autocomplete the last tag. + // This regexp allows the following types of user input: + // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar + $content_type_info = _content_type_info(); + $vid = $content_type_info['fields'][$field_name]['vid']; + $tid = content_taxonomy_field_get_parent($content_type_info['fields'][$field_name]); + + // If the menu system has splitted the search text because of slashes, glue it back. + if (func_num_args() > 2) { + $args = func_get_args(); + $string .= '/'. implode('/', array_slice($args, 2)); + } + + // The user enters a comma-separated list of tags. We only autocomplete the last tag. + $array = drupal_explode_tags($string); + + // Fetch last tag + $last_string = trim(array_pop($array)); + $matches = array(); + if ($last_string != '') { + if ($tid) { + $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t + LEFT JOIN {term_synonym} s ON t.tid = s.tid + INNER JOIN {term_hierarchy} h ON t.tid = h.tid + WHERE h.parent = %d + AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), + $tid,$last_string,$last_string,0,10); + } + else { + $result = db_query_range(db_rewrite_sql("SELECT t.name FROM {term_data} t + LEFT JOIN {term_synonym} s ON t.tid = s.tid + WHERE t.vid = %d + AND (LOWER(t.name) LIKE LOWER('%%%s%%') OR LOWER(s.name) LIKE LOWER('%%%s%%'))", 't', 'tid'), + $vid, $last_string, $last_string, 0, 10); + } + $prefix = count($array) ? '"'. implode('", "', $array) .'", ' : ''; + + while ($tag = db_fetch_object($result)) { + $n = $tag->name; + // Commas and quotes in terms are special cases, so encode 'em. + if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) { + $n = '"'. str_replace('"', '""', $tag->name) .'"'; + } + $matches[$prefix . $n] = check_plain($tag->name); + } + } + + drupal_json($matches); +} + +/** + * Get TIDs for freetagging tags + * Free tagging vocabularies do not send their tids in the form, + * so we'll detect them here and process them independently. + * @param $typed_input A string containing all comma separated tags. As the user typed it. + */ +function content_taxonomy_autocomplete_tags_get_tids($typed_input, $vid, $parent = 0, $extra_parent = 0) { + // This regexp allows the following types of user input: + // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar + $typed_terms = content_taxonomy_autocomplete_split_tags($typed_input); + + foreach ($typed_terms as $typed_term) { + // If a user has escaped a term (to demonstrate that it is a group, + // or includes a comma or quote character), we remove the escape + // formatting so to save the term into the DB as the user intends. + $typed_term = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term))); + if ($typed_term == "") { continue; } + + // See if the term exists in the chosen vocabulary + // and return the tid, otherwise, add a new record. + $possibilities = taxonomy_get_term_by_name($typed_term); + + $typed_term_tid = NULL; // tid match if any. + foreach ($possibilities as $possibility) { + if ($possibility->vid == $vid) { + if ($parent) { + $parents = array(); + $parents = taxonomy_get_parents($possibility->tid); + if (in_array($parent, array_keys($parents)) || in_array($extra_parent, array_keys($parents))) { + $result['existing_tids'][$possibility->tid] = $possibility->tid; + $typed_term_tid = $possibility->tid; + } + } + else { + $result['existing_tids'][$possibility->tid] = $possibility->tid; + $typed_term_tid = $possibility->tid; + } + } + } + + if (!$typed_term_tid) { + $result['non_existing_terms'][] = array( + 'name' => $typed_term, + 'vid' => $vid, + ); + } + } + + return $result; +} + +/** + * Insert new tags + * + * @param $nid the node id + * @param $terms an array of all nonexisting terms. + * @return an array of newly inserted term ids + */ +function content_taxonomy_autocomplete_insert_tags($terms, $parent = NULL) { + foreach ($terms as $term) { + $edit = array('vid' => $term['vid'], 'name' => $term['name']); + if ($parent) $edit['parent'] = $parent; + $status = taxonomy_save_term($edit); + $saved_terms[$edit['tid']] = $edit['tid']; + } + return $saved_terms; +} + +/** + * Helper function to split the tags + */ +function content_taxonomy_autocomplete_split_tags($typed_input) { + $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x'; + preg_match_all($regexp, $typed_input, $matches); + return $matches[1]; +} + +/** + * Helper function to merge the tags, to prefill the fields when editing a node. + */ +function content_taxonomy_autocomplete_merge_tags($terms, $vid) { + $typed_terms = array(); + if (!empty($terms)) { + foreach ($terms as $term) { + // Extract terms belonging to the vocabulary in question. + if ($term->vid == $vid) { + //if ($tid && in_array($term->tid,drupal_map_assoc(array_keys((taxonomy_get_children($tid,$vid)))))) { + // Commas and quotes in terms are special cases, so encode 'em. + $name = $term->name; + if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) { + $name = '"'. preg_replace('/"/', '""', $name) .'"'; + } + $typed_terms[] = $name; + // } + } + } + } + return implode(', ', $typed_terms); +} + +function theme_content_taxonomy_autocomplete($element) { + return $element['#children']; +} diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_options.info b/sites/all/modules/content_taxonomy/content_taxonomy_options.info new file mode 100644 index 0000000..ddfc3d1 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_options.info @@ -0,0 +1,17 @@ +; $Id: content_taxonomy_options.info,v 1.1.2.5.2.1 2008/04/30 08:05:19 mh86 Exp $ +name = Content Taxonomy Options +description = Defines a option widget type for content_taxonomy for selects, radios/checkboxes +dependencies[] = content +dependencies[] = content_taxonomy +dependencies[] = taxonomy +dependencies[] = optionwidgets +package = CCK +core = 6.x + + +; Information added by drupal.org packaging script on 2009-08-19 +version = "6.x-1.0-rc2" +core = "6.x" +project = "content_taxonomy" +datestamp = "1250688034" + diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_options.install b/sites/all/modules/content_taxonomy/content_taxonomy_options.install new file mode 100644 index 0000000..05dd6df --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_options.install @@ -0,0 +1,38 @@ + array( + 'arguments' => array('field' => NULL), + ), + ); +} + +/** + * Implementation of hook_widget_info(). + */ +function content_taxonomy_options_widget_info() { + return array( + 'content_taxonomy_options' => array( + 'label' => t('Checkboxes/Radios'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_MODULE, + 'callbacks' => array( + 'default value' => CONTENT_CALLBACK_DEFAULT, + ), + ), + 'content_taxonomy_select' => array( + 'label' => t('Select List'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_MODULE, + 'callbacks' => array( + 'default value' => CONTENT_CALLBACK_DEFAULT, + ), + ), + ); + return $items; +} + +/** + * Implementation of hook_widget_settings + */ +function content_taxonomy_options_widget_settings($op, $widget) { + switch ($op) { + case 'form': + $form['settings'] = array( + '#type' => 'fieldset', + '#title' => t('Settings for Options'), + '#collapsible' => TRUE, + '#weight' => 10, + ); + $form['settings']['show_depth'] = array( + '#type' => 'checkbox', + '#title' => t('Indent child terms with \' - \' signs'), + '#default_value' => is_numeric($widget['show_depth']) ? $widget['show_depth'] : 1, + '#description' => t('If this option is checked, a hierarchy gets visualized by indenting child terms, otherwise it\'s a flat list'), + ); + $form['settings']['group_parent'] = array( + '#title' => t('Parent term for OptGroups in select fields'), + '#type' => 'select', + '#default_value' => isset($widget['group_parent']) ? $widget['group_parent'] : 0, + '#options' => _content_taxonomy_get_all_terms(), + '#description' => t('This settings applies only for select fields. Select a parent term containg the grouping terms. Grouping terms should be parents of the selected terms (from the Global Settings).'), + ); + return $form; + + case 'save': + return array('group_parent', 'show_depth'); + } +} + +/** + * Implementation of hook_widget(). + */ +function content_taxonomy_options_widget(&$form, &$form_state, $field, $items, $delta = NULL) { + $element = array( + '#type' => ($field['widget']['type'] == 'content_taxonomy_select') ? 'optionwidgets_select' : 'optionwidgets_buttons', + '#default_value' => !empty($items) ? $items : array(), + ); + return $element; +} + +/** + * Theme the label for the empty value for options (for optional radios and single selects). + */ +function theme_content_taxonomy_options_widgets_none($field) { + switch ($field['widget']['type']) { + case 'content_taxonomy_options': + return t('N/A'); + case 'content_taxonomy_select': + return t('- None -'); + default : + return ''; + } +} diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_tree.info b/sites/all/modules/content_taxonomy/content_taxonomy_tree.info new file mode 100644 index 0000000..a56c669 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_tree.info @@ -0,0 +1,16 @@ +; $Id: content_taxonomy_tree.info,v 1.1.2.1 2008/04/30 08:05:19 mh86 Exp $ +name = Content Taxonomy Tree +description = Defines a dynamic tree widget for Content Taxonomy +dependencies[] = content +dependencies[] = content_taxonomy +dependencies[] = taxonomy +dependencies[] = taxonomy_manager +package = CCK +core = 6.x + +; Information added by drupal.org packaging script on 2009-08-19 +version = "6.x-1.0-rc2" +core = "6.x" +project = "content_taxonomy" +datestamp = "1250688034" + diff --git a/sites/all/modules/content_taxonomy/content_taxonomy_tree.install b/sites/all/modules/content_taxonomy/content_taxonomy_tree.install new file mode 100644 index 0000000..dc750f1 --- /dev/null +++ b/sites/all/modules/content_taxonomy/content_taxonomy_tree.install @@ -0,0 +1,39 @@ + array( + 'arguments' => array('element' => NULL), + ), + ); +} + +/** + * Implementation of hook_widget_info(). + */ +function content_taxonomy_tree_widget_info() { + return array( + 'content_taxonomy_tree' => array( + 'label' => t('Tree'), + 'field types' => array('content_taxonomy'), + 'multiple values' => CONTENT_HANDLE_MODULE, + 'callbacks' => array( + 'default value' => CONTENT_CALLBACK_DEFAULT, + ), + ), + ); + return $items; +} + +/** + * Implementation of hook_widget_settings + */ +function content_taxonomy_tree_widget_settings($op, $widget) { + switch ($op) { + case 'form': + $form['settings'] = array( + '#type' => 'fieldset', + '#title' => t('Settings for Trees'), + '#collapsible' => TRUE, + '#weight' => 10, + ); + $form['settings']['expand_all'] = array( + '#type' => 'checkbox', + '#title' => t('Expand whole tree by default'), + '#default_value' => isset($widget['expand_all']) ? $widget['expand_all'] : 0, + '#description' => t('Otherwise only branches, where a term is selected get expanded by default'), + ); + return $form; + + case 'save': + return array('expand_all'); + } +} + +/** + * Implementation of hook_elements(). + */ +function content_taxonomy_tree_elements() { + return array( + 'content_taxonomy_tree' => array( + '#input' => TRUE, + '#columns' => array('value'), + '#delta' => 0, + '#process' => array('content_taxonomy_tree_process'), + '#element_validate' => array('content_taxonomy_tree_validate'), + '#autocomplete_path' => FALSE, + ), + ); +} + +/** + * Implementation of hook_widget(). + */ +function content_taxonomy_tree_widget(&$form, &$form_state, $field, $items, $delta = NULL) { + $element = array( + '#type' => 'content_taxonomy_tree', + '#default_value' => !empty($items) ? $items : array(), + ); + return $element; +} + +/** + * Processes the content_taxonomy_tree form element + * + * uses the 'taxonomy_manager_tree' element type + */ +function content_taxonomy_tree_process($element, $edit, &$form_state, $form) { + $field_name = $element['#field_name']; + $field = $form['#field_info'][$field_name]; + $field_key = $element['#columns'][0]; + + if (is_array($element['#value'])) { + if (array_key_exists($field_key, $element['#value'])) { + //validation function where values are set haven't been called yet for this form element + //because some other element might have caused an validation error + //so we have to do the processing of the values here + if ($field['multiple']) { + $selected_terms = _taxonomy_manager_tree_get_selected_terms($element['#value']['value']); + $element['#value'] = content_transpose_array_rows_cols(array($field_key => array_values($selected_terms))); + } + } + $element['#value'] = content_taxonomy_tree_data2form($element, $element['#value'], $field); + } + + $element[$field_key] = array( + '#type' => 'taxonomy_manager_tree', + '#default_value' => isset($element['#value']) ? $element['#value'] : '', + '#vid' => $field['vid'], + '#parent' => content_taxonomy_field_get_parent($field), + '#default_value' => is_array($element['#value']) ? $element['#value'] : array(), + '#render_whole_tree' => TRUE, + '#pager' => FALSE, + '#add_term_info' => FALSE, + '#expand_all' => $field['widget']['expand_all'], + '#tree_is_required' => $field['required'], //using tree_is_required instead of required, prevents that error messages are shown twice + '#required' => FALSE, + '#multiple' => isset($field['multiple']) ? $field['multiple'] : FALSE, + '#field_name' => $element['#field_name'], + '#delta' => $element['#delta'], + '#columns' => $element['#columns'], + '#title' => $element['#title'], + '#description' => $element['#description'], + ); + + + // Make sure field info will be available to the validator which + // does not get the values in $form. + $form_state['#field_info'][$field['field_name']] = $field; + + return $element; +} + +/** + * Validation function for the content_taxonomy_tree element + * + * assigns the selected terms to the element + * additionally checks if terms are selected for required fields (if this check isn't already done by form.inc) + */ +function content_taxonomy_tree_validate($element, &$form_state) { + $field = $form_state['#field_info'][$element['#field_name']]; + $field_key = $element['#columns'][0]; + + $values = content_taxonomy_tree_form2data($element, $form_state, $field); + + //check for required fields + //if $element['#value'] is empty, it's validated by form.inc + //otherwise we have to check this by our own + if ($field['required'] && is_null($values[0]['value']) && !empty($element['#value'])) { + form_error($element[$field_key], t('!name field is required.', array('!name' => $element['#title']))); + } + if (($field['multiple'] >= 2) && (count($element['#value']) > $field['multiple'])) { + form_error($element[$field_key], t('%name: this field cannot hold more than @count values.', array('%name' => t($field['widget']['label']), '@count' => $field['multiple']))); + } + + form_set_value($element, $values, $form_state); + +} + +/** + * Helper function to transpose the values as stored in the database + * to the format the content_taxonomy_tree needs to select the values + */ +function content_taxonomy_tree_data2form($element, $values, $field) { + $terms = array(); + foreach ($values as $delta => $entry) { + if ($entry['value']) { + $terms[] = $entry['value']; + } + } + return $terms; +} + +/** + * Helper function to transpose the values returned by submitting the content_taxonomy_tree + * to the format to be stored in the field + */ +function content_taxonomy_tree_form2data($element, &$form_state, $field) { + $field_key = $element['#columns'][0]; + $selected_terms = $form_state['values'][$element['#field_name']][$field_key]['selected_terms']; + + if (!is_array($selected_terms) || !count($selected_terms)) { + $selected_terms[] = NULL; + } + + $form_state['#field_info'][$field['field_name']] = $field; + return content_transpose_array_rows_cols(array($element['#columns'][0] => array_values($selected_terms))); +} + +/** + * FAPI theme for content_taxonomy_tree + * + * element already rendered by taxonomy_manager_tree + */ +function theme_content_taxonomy_tree($element) { + return $element['#children']; +} diff --git a/sites/all/modules/content_taxonomy/includes/content_taxonomy.token.inc b/sites/all/modules/content_taxonomy/includes/content_taxonomy.token.inc new file mode 100644 index 0000000..9b8d59b --- /dev/null +++ b/sites/all/modules/content_taxonomy/includes/content_taxonomy.token.inc @@ -0,0 +1,56 @@ +name; + $vid = $term->vid; + } + } + if ($vid) { + $vocabulary = taxonomy_vocabulary_load($vid); + } + + $tokens['terms-raw'] = implode(', ', $terms); + $tokens['terms'] = check_plain($tokens['terms-raw']); + $tokens['tids'] = implode(', ', $tids); + $tokens['term-raw'] = $terms[0]; + $tokens['term'] = check_plain($tokens['term-raw']); + $tokens['tid'] = $tids[0]; + $tokens['vocab'] = isset($vocabulary) ? $vocabulary->name : ''; + $tokens['vid'] = $vid; + + return $tokens; + } +} diff --git a/sites/all/modules/content_taxonomy/includes/views/content_taxonomy.views.inc b/sites/all/modules/content_taxonomy/includes/views/content_taxonomy.views.inc new file mode 100644 index 0000000..81b12b0 --- /dev/null +++ b/sites/all/modules/content_taxonomy/includes/views/content_taxonomy.views.inc @@ -0,0 +1,17 @@ + array( + 'path' => drupal_get_path('module', 'content_taxonomy') . '/includes/views', + ), + 'handlers' => array( + 'content_taxonomy_handler_filter_many_to_one' => array( + 'parent' => 'content_handler_filter_many_to_one', + ), + ), + ); +} diff --git a/sites/all/modules/content_taxonomy/includes/views/content_taxonomy_handler_filter_many_to_one.inc b/sites/all/modules/content_taxonomy/includes/views/content_taxonomy_handler_filter_many_to_one.inc new file mode 100644 index 0000000..8bb3530 --- /dev/null +++ b/sites/all/modules/content_taxonomy/includes/views/content_taxonomy_handler_filter_many_to_one.inc @@ -0,0 +1,21 @@ +content_field); + unset($options['']); + $this->value_options = $options; + } +} diff --git a/sites/all/modules/content_taxonomy/tests/content_taxonomy.test b/sites/all/modules/content_taxonomy/tests/content_taxonomy.test new file mode 100644 index 0000000..c1a99a7 --- /dev/null +++ b/sites/all/modules/content_taxonomy/tests/content_taxonomy.test @@ -0,0 +1,642 @@ +loginWithPermissions(); + $this->acquireContentTypes(2); + } + + /** + * helper function to create a vocabulary and terms + */ + function createTerms($count = 1) { + $edit['name'] = $this->randomName(200); + $edit['hierarchy'] = 2; // Hierarchy 0,1,2 + $edit['multiple'] = 1; // multiple 0,1 + $edit['required'] = 0; // required 0,1 + $edit['relations'] = 0; + $edit['tags'] = 1; + // exec save function + taxonomy_save_vocabulary($edit); + $vid = $edit['vid']; + + for ($i = 0; $i < $count; $i++) { + // create term + $termname = $this->randomName(20); + $data = array('name' => $termname, 'vid' => $vid); + taxonomy_save_term($data); + $terms[] = taxonomy_get_term($data['tid']); + } + return $terms; + + } + + /** + * helper assertion function, which checks if the node field array is built correctly + */ + function assertNodeMultiValues($node, $field_name, $terms_in = array(), $terms_out = array()) { + $tids = array(); + if (is_array($node->{$field_name})) { + foreach ($node->{$field_name} as $key => $value) { + $tids[$value['value']] = $value['value']; + } + } + foreach ($terms_in as $term) { + $this->assertTrue(in_array($term->tid, $tids), 'Term correctly in node field'); + } + + foreach ($terms_out as $term) { + $this->assertTrue(!in_array($term->tid, $tids), 'Term correctly in node field'); + } + } +} + + +/** + * Base Class for testing Content Taxonomy, + * extends the ContentCrudTestCase Class from CCK, which provides many useful helper functions + */ +class ContentTaxonomyTest extends ContentTaxonomyTestCase { + + function getInfo() { + return array( + 'name' => t('Content Taxonomy - Saving'), + 'description' => t('Tests basic saving'), + 'group' => t('Content Taxonomy'), + ); + } + + function setUp() { + parent::setUp(); + } + + + function testContentTaxonomySaving() { + $type = $this->content_types[0]; + $type_url = str_replace('_', '-', $type->type); + + $terms = $this->createTerms(4); + + $settings = array( + 'type' => 'content_taxonomy', + 'widget_type' => 'content_taxonomy_options', + 'vid' => $terms[0]->vid, + 'parent' => 0, + 'parent_php_code' => '', + 'show_depth' => 0, + 'save_term_node' => FALSE, + 'depth' => NULL, + 'hide_taxonomy_fields' => TRUE, + ); + + $field = $this->createField($settings, 0); + $field_name = $field['field_name']; + + //Check if field get's exposed to the content type + $this->drupalGET('node/add/'. $type_url); + $this->assertRaw($field_name, 'Field found on content type form'); + $this->assertRaw($terms[0]->name, 'Option value found on content type form'); + $this->assertRaw($terms[1]->name, 'Option value found on content type form'); + $this->assertRaw($terms[2]->name, 'Option value found on content type form'); + $this->assertRaw($terms[3]->name, 'Option value found on content type form'); + + // Create a node with one value selected + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->tid; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $this->assertEqual($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertNoText($terms[1]->name, 'Term not displayed'); + + //Edit the node and select a different value + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $this->assertIdentical($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term not displayed'); + + //Edit the node and unselect the value (selecting '- None -'). + $edit = array(); + $edit[$field_name.'[value]'] = ''; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Terms deleted'); + $this->drupalGet('node/'. $node->nid); + $this->assertNoText($terms[0]->name, 'Terms not displayed'); + $this->assertNoText($terms[1]->name, 'Terms not displayed'); + + + //CREATE NEW FIELD MULTIPLE + $settings['multiple'] = TRUE; + $field = $this->createField($settings, 0); + $field_name = $field['field_name']; + + //Check if field get's exposed to the content type + $this->drupalGET('node/add/'. $type_url); + $this->assertRaw($field_name, 'Field found on content type form'); + $this->assertRaw($terms[0]->name, 'Option value found on content type form'); + $this->assertRaw($terms[1]->name, 'Option value found on content type form'); + $this->assertRaw($terms[2]->name, 'Option value found on content type form'); + $this->assertRaw($terms[3]->name, 'Option value found on content type form'); + + // Edit the node and select multiple values. + $edit[$field_name .'[value]['. $terms[0]->tid .']'] = $terms[0]->tid; + $edit[$field_name .'[value]['. $terms[1]->tid .']'] = $terms[1]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $tids = array(); + foreach ($node->{$field_name} as $key => $value) { + $tids[$value['value']] = $value['value']; + } + if (!in_array($terms[0]->tid, $tids) || !in_array($terms[1]->tid, $tids)) { + $this->fail("Terms saved"); + } + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[2]->name, 'Term not displayed'); + + //Edit the node and select different values + $edit = array(); + $edit[$field_name.'[value]['. $terms[0]->tid .']'] = FALSE; + $edit[$field_name.'[value]['. $terms[1]->tid .']'] = $terms[1]->tid; + $edit[$field_name.'[value]['. $terms[2]->tid .']'] = $terms[2]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $tids = array(); + foreach ($node->{$field_name} as $key => $value) { + $tids[$value['value']] = $value['value']; + } + if (!in_array($terms[2]->tid, $tids) || !in_array($terms[1]->tid, $tids)) { + $this->fail("Terms updated"); + } + if (in_array($terms[0]->tid, $tids)) { + $this->fail("Terms updated"); + } + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertText($terms[2]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term1 not displayed'); + + //Edit the node and unselect values + $edit = array(); + $edit[$field_name.'[value]['. $terms[1]->tid .']'] = FALSE; + $edit[$field_name.'[value]['. $terms[2]->tid .']'] = FALSE; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms not in term_node table"); + $tids = array(); + foreach ($node->{$field_name} as $key => $value) { + $tids[$value['value']] = $value['value']; + } + if (in_array($terms[2]->tid, $tids) || in_array($terms[1]->tid, $tids) || in_array($terms[0]->tid, $tids)) { + $this->fail("Terms deleted"); + } + $this->drupalGet('node/'. $node->nid); + $this->assertNoText($terms[1]->name, 'Terms not displayed'); + $this->assertNoText($terms[2]->name, 'Terms not displayed'); + $this->assertNoText($terms[0]->name, 'Terms not displayed'); + + + /** + * Tests Saving in Term Node + */ + $ct = $this->content_types[1]; + $ct_url = str_replace('_', '-', $ct->type); + + $settings['save_term_node'] = TRUE; + $settings['multiple'] = FALSE; + + //$terms = $this->createTerms(4); + $field = $this->createField($settings, 1); + $field_name = $field['field_name']; + + //Check if field get's exposed to the content type + $this->drupalGET('node/add/'. $ct_url); + $this->assertRaw($field_name, 'Field found on content type form'); + $this->assertRaw($terms[0]->name, 'Option value found on content type form'); + $this->assertRaw($terms[1]->name, 'Option value found on content type form'); + $this->assertRaw($terms[2]->name, 'Option value found on content type form'); + $this->assertRaw($terms[3]->name, 'Option value found on content type form'); + + // Create a node with one value selected + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->tid; + $this->drupalPost('node/add/'. $ct_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertEqual($in_term_node, $terms[0]->tid, "Terms saved in term_node table"); + $this->assertEqual($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertNoText($terms[1]->name, 'Term not displayed'); + + //Edit the node and select a different value + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertEqual($in_term_node, $terms[1]->tid, "Terms updated in term_node table"); + $this->assertEqual($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term not displayed'); + + //Edit the node and unselect the value (selecting '- None -'). + $edit = array(); + $edit[$field_name.'[value]'] = ''; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $node->nid)); + $this->assertFalse($in_term_node, "Terms deleted from term_node table"); + $this->assertIdentical($node->{$field_name}[0]['value'], NULL, 'Terms deleted'); + $this->drupalGet('node/'. $node->nid); + $this->assertNoText($terms[0]->name, 'Terms not displayed'); + $this->assertNoText($terms[1]->name, 'Terms not displayed'); + + //CREATE NEW FIELD MULTIPLE + $settings['multiple'] = TRUE; + $field = $this->createField($settings, 1); + $field_name = $field['field_name']; + + //Check if field get's exposed to the content type + $this->drupalGET('node/add/'. $ct_url); + $this->assertRaw($field_name, 'Field found on content type form'); + $this->assertRaw($terms[0]->name, 'Option value found on content type form'); + $this->assertRaw($terms[1]->name, 'Option value found on content type form'); + $this->assertRaw($terms[2]->name, 'Option value found on content type form'); + $this->assertRaw($terms[3]->name, 'Option value found on content type form'); + + // Edit the node and select multiple values. + $edit[$field_name .'[value]['. $terms[0]->tid .']'] = $terms[0]->tid; + $edit[$field_name .'[value]['. $terms[1]->tid .']'] = $terms[1]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node1 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[0]->tid)); + $this->assertEqual($in_term_node1, $terms[0]->tid, "Terms updated in term_node table"); + $in_term_node2 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[1]->tid)); + $this->assertEqual($in_term_node2, $terms[1]->tid, "Terms updated in term_node table"); + $this->assertNodeMultiValues($node, $field_name, array($terms[0], $terms[1])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[2]->name, 'Term not displayed'); + + //Edit the node and select different values + $edit = array(); + $edit[$field_name.'[value]['. $terms[0]->tid .']'] = FALSE; + $edit[$field_name.'[value]['. $terms[1]->tid .']'] = $terms[1]->tid; + $edit[$field_name.'[value]['. $terms[2]->tid .']'] = $terms[2]->tid; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[0]->tid)); + $this->assertFalse($in_term_node, "Term deleted term_node table"); + $in_term_node3 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[2]->tid)); + $this->assertEqual($in_term_node3, $terms[2]->tid, "Terms updated in term_node table"); + $in_term_node2 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[1]->tid)); + $this->assertEqual($in_term_node2, $terms[1]->tid, "Terms updated in term_node table"); + $this->assertNodeMultiValues($node, $field_name, array($terms[1], $terms[2]), array($terms[0])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertText($terms[2]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term1 not displayed'); + + //Edit the node and unselect values + $edit = array(); + $edit[$field_name.'[value]['. $terms[1]->tid .']'] = FALSE; + $edit[$field_name.'[value]['. $terms[2]->tid .']'] = FALSE; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $in_term_node1 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[0]->tid)); + $this->assertFalse($in_term_node1, "Term deleted term_node table"); + $in_term_node2 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[1]->tid)); + $this->assertFalse($in_term_node2, "Term deleted term_node table"); + $in_term_node3 = db_result(db_query("SELECT tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $terms[2]->tid)); + $this->assertFalse($in_term_node3, "Term deleted term_node table"); + $this->assertNodeMultiValues($node, $field_name, array(), array($terms[0], $terms[1], $terms[2])); + $this->drupalGet('node/'. $node->nid); + $this->assertNoText($terms[1]->name, 'Terms not displayed'); + $this->assertNoText($terms[2]->name, 'Terms not displayed'); + $this->assertNoText($terms[0]->name, 'Terms not displayed'); + } +} + + +/** + * Test Cases for Content Taxonomy Autocomplete + */ +class ContentTaxonomyAutocompleteTest extends ContentTaxonomyTestCase { + + function getInfo() { + return array( + 'name' => t('Content Taxonomy - Autocomplete'), + 'description' => t('Tests freetagging widget'), + 'group' => t('Content Taxonomy'), + ); + } + + function setUp() { + parent::setUp("diff"); + } + + function testAutocomplete() { + $type = $this->content_types[1]; + $type_url = str_replace('_', '-', $type->type); + $terms = $this->createTerms(4); + + //single field + $settings = array( + 'type' => 'content_taxonomy', + 'widget_type' => 'content_taxonomy_autocomplete', + 'vid' => $terms[0]->vid, + 'parent' => 0, + 'parent_php_code' => '', + 'show_depth' => 0, + 'save_term_node' => FALSE, + 'depth' => NULL, + 'hide_taxonomy_fields' => TRUE, + ); + + $field = $this->createField($settings, 1); + $field_name = $field['field_name']; + + // Create a node with one value + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $this->assertEqual($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertNoText($terms[1]->name, 'Term not displayed'); + + //Edit the node and select a different value + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $this->assertIdentical($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term not displayed'); + + //Edit the node and select 2 values for single field + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->name .", ". $terms[0]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $this->assertText('You can provide only one value', 'Validated'); + $edit[$field_name.'[value]'] = $terms[1]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $this->assertIdentical($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term not displayed'); + + //Add a new term + $edit = array(); + $new_term_name = 'test'; + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $new_term_name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $new_term_tid = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d", $new_term_name, $settings['vid'])); + $this->assertTrue(($new_term_tid > 0), "New term added to vocabulary"); + $node = node_load(array('title' => $edit['title'])); + $this->assertEqual($node->{$field_name}[0]['value'], $new_term_tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($new_term_name, 'Terms displayed'); + + //test Multi Field + $type = $this->content_types[0]; + $type_url = str_replace('_', '-', $type->type); + + //multi field + $settings = array( + 'type' => 'content_taxonomy', + 'widget_type' => 'content_taxonomy_autocomplete', + 'vid' => $terms[0]->vid, + 'parent' => 0, + 'parent_php_code' => '', + 'show_depth' => 0, + 'save_term_node' => FALSE, + 'depth' => NULL, + 'hide_taxonomy_fields' => TRUE, + 'multiple' => TRUE, + ); + + + $field = $this->createField($settings, 0); + $field_name = $field['field_name']; + + // Create a node with one value + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $this->assertNodeMultiValues($node, $field_name, array($terms[0])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertNoText($terms[1]->name, 'Term not displayed'); + + //Edit the node and select a different value + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $this->assertNodeMultiValues($node, $field_name, array($terms[1]), array($terms[0])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertNoText($terms[0]->name, 'Term not displayed'); + + //Edit the node and select a second value + $edit = array(); + $edit[$field_name.'[value]'] = $terms[1]->name .", ". $terms[0]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $this->assertNodeMultiValues($node, $field_name, array($terms[0], $terms[1])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + $this->assertText($terms[0]->name, 'Terms displayed'); + + // Create a node with one value and test preview + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Preview'); + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $this->assertNodeMultiValues($node, $field_name, array($terms[0])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + + // Create a node with one value and test preview with a new term + $new_term_name = 'test2'; + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $new_term_name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Preview'); + $new_term_tid = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d", $new_term_name, $settings['vid'])); + $this->assertTrue(($new_term_tid > 0), "Term in added to vocabulary"); + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $this->assertNodeMultiValues($node, $field_name, array(taxonomy_get_term($new_term_tid))); + $this->drupalGet('node/'. $node->nid); + $this->assertText($new_term_name, 'Terms displayed'); + + // Create a node with one value and test preview diff + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + + $edit = array(); + $edit['title'] = $node->title; + $edit['body'] = str_replace('', '', $node->body); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Preview changes'); + $this->assertText('No visible changes', 'No visible changes'); + $this->assertRaw($terms[0]->name, 'Term in field'); + /* $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $node = node_load($node->nid, NULL, TRUE); + $this->assertNodeMultiValues($node, $field_name, array($terms[0])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed');*/ + + //CREATE NEW REQUIRED FIELD + $settings['required'] = TRUE; + $field = $this->createField($settings, 0); + $field_name = $field['field_name']; + + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name.'[value]'] = ''; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $this->assertText($field_name .' field is required', 'Validated required field'); + $edit[$field_name.'[value]'] = $terms[1]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $this->assertNoText($field_name .' field is required', 'Validation for required field successfully passed'); + $node = node_load(array('title' => $edit['title'])); + $this->assertNodeMultiValues($node, $field_name, array($terms[1])); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[1]->name, 'Terms displayed'); + + } +} + +/** + * Test Cases for Content Taxonomy Autocomplete + */ +class ContentTaxonomyAutocompletePermissionsTest extends ContentTaxonomyTestCase { + + function getInfo() { + return array( + 'name' => t('Content Taxonomy - Autocomplete with Permissions'), + 'description' => t('Tests freetagging widget with content permissions'), + 'group' => t('Content Taxonomy'), + ); + } + + function setUp() { + parent::setUp("content_permissions"); + } + + function testAutocomplete() { + $type = $this->content_types[1]; + $type_url = str_replace('_', '-', $type->type); + $terms = $this->createTerms(4); + + //single field + $settings = array( + 'type' => 'content_taxonomy', + 'widget_type' => 'content_taxonomy_autocomplete', + 'vid' => $terms[0]->vid, + 'parent' => 0, + 'parent_php_code' => '', + 'show_depth' => 0, + 'save_term_node' => FALSE, + 'depth' => NULL, + 'hide_taxonomy_fields' => TRUE, + ); + + $field = $this->createField($settings, 1); + $field_name = $field['field_name']; + + $permissions = array('edit '. $field_name, 'view '. $field_name); + $rids = db_query("SELECT rid FROM {role}"); + while($obj = db_fetch_object($rids)) { + db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $obj->rid, implode(', ', $permissions)); + } + + // Create a node with one value with edit permissions + $edit = array(); + $edit['title'] = $this->randomName(20); + $edit['body'] = $this->randomName(20); + $edit[$field_name .'[value]'] = $terms[0]->name; + $this->drupalPost('node/add/'. $type_url, $edit, 'Save'); + $node = node_load(array('title' => $edit['title'])); + $this->assertEqual($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + $this->assertNoText($terms[1]->name, 'Term not displayed'); + + //delete edit field perm + $permissions_old = array('edit '. $field_name, 'view '. $field_name); + $permissions_new = array('view '. $field_name); + $rids = db_query("SELECT rid FROM {role}"); + while($obj = db_fetch_object($rids)) { + db_query("DELETE FROM {permission WHERE rid = %d AND perm = '%s'", $obj->rid, implode(', ', $permissions_old)); + db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $obj->rid, implode(', ', $permissions_new)); + } + + //Edit the node, but without perm + $edit = array(); + $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); + $this->drupalGet('node/'. $node->nid .'/edit'); + $this->assertNoRaw($field_name, "Field hidden"); + $node = node_load($node->nid, NULL, TRUE); + $this->assertIdentical($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved'); + $this->drupalGet('node/'. $node->nid); + $this->assertText($terms[0]->name, 'Terms displayed'); + } +} diff --git a/sites/all/modules/content_taxonomy/translations/fr.po b/sites/all/modules/content_taxonomy/translations/fr.po new file mode 100644 index 0000000..486922f --- /dev/null +++ b/sites/all/modules/content_taxonomy/translations/fr.po @@ -0,0 +1,298 @@ +# $Id: fr.po,v 1.1.2.1 2009/04/07 10:52:21 slybud Exp $ +# +# French translation of Drupal (general) +# Copyright YEAR NAME