Index: contrib/i18nfieldcopy.info =================================================================== RCS file: contrib/i18nfieldcopy.info diff -N contrib/i18nfieldcopy.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ contrib/i18nfieldcopy.info 28 Mar 2008 18:23:15 -0000 @@ -0,0 +1,4 @@ +name = i18n - field coppier +description = Coppies fields between translation sets - will keep language independant fields in sync. +dependencies = i18n translation +package = Multilanguage - i18n Index: contrib/i18nfieldcopy.module =================================================================== RCS file: contrib/i18nfieldcopy.module diff -N contrib/i18nfieldcopy.module --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ contrib/i18nfieldcopy.module 28 Mar 2008 18:23:16 -0000 @@ -0,0 +1,394 @@ +no_i18nfieldcopy) { + return false; + } + i18nfieldcopy_copyfields($node); + //check if any of the groups have copy turned on + //If so, recurse into their fields, to see if there are overrides + //copy each field as it is reached. + //dpr($node); + + break; + + + } +} + +function i18nfieldcopy_i18nfieldcopy_test($arg) { + + $node = node_load(524); + _content_widget_invoke('prepare form values', $t_node); + $phone=rand(0,5000); + $fax=rand(0,5000); + $node->field_phone[0]['value'] = $phone; + $node->field_fax[0]['value'] = $fax; + node_save($node); + + + $trxs = translation_node_get_translations(array('nid' => $node->nid)); + unset ($trxs[$node->language]); + + foreach ($trxs as $translation) { + $t_node = node_load(array('nid' => $translation->nid)); + _content_widget_invoke('prepare form values', $t_node); + } +} + +function i18nfieldcopy_utilities() { + return array ( + 'i18nfieldcopy_test' => array ( + 'title' => "Test i18nfieldcopy", + ), + ); +} + +/** + * Bulk update form submission. + * + * @param unknown_type $form_id + * @param unknown_type $form_values + */ +function i18nfieldcopy_bulkupdate_form_submit($form_id,$form_values) { + $q = 'SELECT node.nid FROM {i18n_node} LEFT JOIN {node} on i18n_node.nid = node.nid WHERE node.type LIKE "%s" AND i18n_node.language LIKE "%s"'; + $result = db_queryd($q,$form_values['bulkupdate_types'],$form_values['bulkupdate_primary_language']); + while ($row = db_fetch_object($result)) { + $node = node_load(array('nid' => $row->nid)); + i18nfieldcopy_copyfields($node); + + $i++; + } + +} + +/** + * See admin/settings/i18n/i18nfieldcopy/bulkupdate + * + * @return unknown + */ +function i18nfieldcopy_bulkupdate_form() { + + $content_types = content_types(); + foreach ($content_types as $system_name => $type) { + $types[$system_name] = $type['name']; + } + + $form = array( + + '#description' => 'This form will run through all nodes of the provided type in the provided + languages and will fire the function to copy their field values to the other nodes + in their translation set as per you settings.', + 'bulkupdate_types' => array ( + '#title' => 'Type', + '#type' => 'select', + '#options' => $types, + '#required' => true, + ), + + 'bulkupdate_primary_language' => array ( + '#title' => 'Primary Language', + '#type' => 'select', + '#options' => i18n_languages(), + '#required' => true, + ), + + 'submit' => array ( + '#type' => 'submit', + '#value' => 'Submit', + ), + ); + return $form; +} + +/** + * The main function to copy fields + * + * @param unknown_type $node + */ +function i18nfieldcopy_copyfields(&$node) { + + /** + * + * Really should cache all of this first part for bulk update... + */ + $type_info = content_types($node->type); + $result = db_query("SELECT * FROM {node_group} WHERE type_name LIKE '%s' ORDER BY weight, group_name",$node->type); + + $groups = array(); + while ($_group = db_fetch_array($result)) { + $_group['settings'] = unserialize($_group['settings']); + if ($_group['settings']['i18n_field_copy']['i18n_field_copy_setting']) { + $groups[$_group['group_name']] = $_group['settings']['i18n_field_copy']['i18n_field_copy_setting']; + } + } + + $fields = $type_info['fields']; + + $overwrite_conditional_vars = $overwrite_vars = array(); + foreach ($fields as $fieldname => $field) { + $setting = ''; + $setting = _i18nfieldcopy_get_setting($node->type,$fieldname); + + if (!$setting && $groups) { + if ($field_group = fieldgroup_get_group($node->type,$fieldname) ) { + //dpr($field_group); + $setting = $groups[$field_group]; + } + } + + //dpr($fieldname); + //dpr($setting); + switch ($setting) { + case 'overwrite': + $overwrite_vars[$fieldname] = $node->{$fieldname}; + //dpr($trxs); + break; + case 'nothing': + break; + case 'overwrite_if_blank': + $overwrite_conditional_vars[$fieldname] = $node->{$fieldname}; + break; + } + + } + if ($overwrite_vars || $overwrite_conditional_vars) { + if ($overwrite_vars) { + $save =true; + } else { + $save =false; + } + $trxs = translation_node_get_translations(array('nid' => $node->nid)); + unset ($trxs[$node->language]); + + foreach ($trxs as $translation) { + $t_node = node_load(array('nid' => $translation->nid)); + _content_widget_invoke('prepare form values', $t_node); + foreach($overwrite_vars as $fieldname => $value) { + //dpr('Writing Values to ' . $fieldname . ' on ' . $t_node->language . ' version of '. $node->title); + $t_node->{$fieldname} = $value; + } + foreach($overwrite_conditional_vars as $fieldname => $value) { + //if ($t_node->{$fieldname} == $value || (_no_value_in_field($t_node->{$fieldname}))) { + if (_no_value_in_field($t_node->{$fieldname})) { + //dpr('Writing Values to ' . $fieldname . ' on ' . $t_node->language . ' version of '. $node->title); + $t_node->{$fieldname} = $value; + $save = true; + } + } + + $t_node->no_i18nfieldcopy = true; + if ($save) { + node_save($t_node); + } + } + } +} + +/** + * Returns true if cck field contains no values (multi-entry field safe); + * + * @param unknown_type $field + */ +function _no_value_in_field($field) { + foreach ($field as $value) { + if (!empty($value['value'])) { + return false; + } + } + return true; +} + +/** + * Implementaiton of hook_menu + * + * @param unknown_type $may_cache + */ +function i18nfieldcopy_menu($may_cache) { + if ($may_cache) { + + $items[] = array ( + 'type' => MENU_LOCAL_TASK, + 'title' => 'i18n Field Copy Bulk Update', + 'path' => 'admin/settings/i18n/i18nfieldcopy/bulkupdate', + 'callback' => 'drupal_get_form', + 'callback arguments' => array('i18nfieldcopy_bulkupdate_form'), + 'access' => user_access('copy fields between translations'), + ); + } + + if (!$may_cache) { + if (arg(0) == 'node' && is_numeric(arg(1)) && variable_get('i18n_node_'.translation_get_node_type(arg(1)), 0)) { + $access = user_access('translate nodes') && user_access('copy fields between translations'); + + $type = MENU_LOCAL_TASK; + /** + * TODO: Implement per node one-off copy + */ +// $items[] = array( +// 'path' => 'node/'. arg(1) .'/i18nfieldcopy', +// 'title' => t('Copy Fields'), +// 'callback' => 'drupal_get_form', +// 'callback arguments' => array('i18nfieldcopy_form',arg(1)), +// 'access' => $access, +// 'type' => $type, +// 'weight' => 3); + } + + + } + return $items; +} + +/** + * TODO: Implement per node one-off copy + * + * @param unknown_type $nid + * @return unknown + */ +function i18nfieldcopy_form($nid) { + $node = node_load(array('nid' => $nid)); + $fields = content_types($node->type); + $fields = $fields['fields']; + + + $options = array('' => 'No setting, ot use group setting', 'nothing' => 'Leave as is', 'overwrite' => 'Overwrite','overwrite_if_blank'=>'Overwrite If Blank'); + + $trxs = translation_node_get_translations(array('nid' => $nid)); + $lang_options = array_intersect_key(i18n_languages(),$trxs); + + $form['languages'] = array( + '#type' => 'select', + '#title' => 'Languages to Sync to', + '#multiple' => true, + '#options' => $lang_options + ); + foreach ($fields as $name => $field) { + + $form[$name] = array ( + '#title' => $field['widget']['label'], + '#type' => 'select', + '#suffix' => 'Current Value: ' . $node->{$name}[0]['value'], + '#options' => $options, + ); + } + + $form['submit'] = array ( + '#type' => 'submit', + '#title' => 'Submit' + ); + + return $form; + return array(); + +} + +/** + * Adds i18nfieldcopy settings to field and group settings forms + * + * @param unknown_type $form_id + * @param unknown_type $form + * @return unknown + */ +function i18nfieldcopy_form_alter($form_id, &$form) { + // Add the node-type settings option to activate the 'Archive to AlFresco' link + if (stristr($form_id,'content_admin_field') || $form_id == 'fieldgroup_edit_group_form') { + + $newform['i18n_field_copy'] = array ( + '#type' => 'fieldset', + '#title' => 'i18n Field Copy', + '#weight' => 2, + ); + $options = array('' => 'No setting, ot use group setting', 'nothing' => 'Leave as is', 'overwrite' => 'Overwrite','overwrite_if_blank'=>'Overwrite If Blank'); + + $form['#submit']['i18nfieldcopy_field_settings_submit'] = array(); + + $default = _i18nfieldcopy_get_setting($form['type_name']['#value'],$form['field_name']['#value']); + + $newform['i18n_field_copy']['i18n_field_copy_setting'] = array ( + '#title' => 'i18n Field Copy Setting', + '#type' => 'select', + '#default_value' => $default, + '#multiple' => false, + //'#suffix' => 'Current Value: ' . $node->{$name}[0]['value'], + '#options' => $options, + + ); + + if ($form_id == 'fieldgroup_edit_group_form') { + $form['settings']['i18n_field_copy'] = $newform['i18n_field_copy']; + //unset ($form['i18n_field_copy']); + } + + + $pos = array_search('submit', array_keys($form)); + $form = array_merge(array_slice($form, 0, $pos), array('i18nfieldcopy' => $newform), array_slice($form, $pos)); + + } + return $form; +} + +/** + * Get the current fieldcopy setting for either a group or a field + * + * @param unknown_type $content_type + * @param unknown_type $field + * @return unknown + */ +function _i18nfieldcopy_get_setting($content_type,$field) { + //bug here for groups. + $prior_instance = db_fetch_array(db_query("SELECT * FROM {node_field_instance} WHERE type_name = '%s' AND field_name = '%s'",$content_type, $field)); + if ($prior_instance) { + $widget_settings = unserialize(($prior_instance['widget_settings'])); + return $widget_settings['i18n_field_copy']['i18n_field_copy_setting']; + } +} + +function i18nfieldcopy_field_settings_submit($form_id,$form_values) { + + if ($form_id == 'fieldgroup_edit_group_form') { + return; + //fieldgroup_save_group + } + + $prior_instance = db_fetch_array(db_query("SELECT * FROM {node_field_instance} WHERE type_name = '%s' AND field_name = '%s'", $form_values['type_name'], $form_values['field_name'])); + $widget_settings = unserialize($prior_instance['widget_settings']); + $widget_settings['i18n_field_copy']['i18n_field_copy_setting'] = $form_values['i18n_field_copy_setting']; + + db_query("UPDATE {node_field_instance} SET widget_settings = '%s' WHERE type_name = '%s' AND field_name = '%s'", serialize($widget_settings), $form_values['type_name'], $form_values['field_name']); + +} Index: contrib/README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/i18n/contrib/Attic/README.txt,v retrieving revision 1.3.4.1 diff -u -p -r1.3.4.1 README.txt --- contrib/README.txt 3 Apr 2007 20:54:55 -0000 1.3.4.1 +++ contrib/README.txt 28 Mar 2008 18:23:16 -0000 @@ -18,6 +18,10 @@ Translates taxonomy terms running them t Can be enabled/disabled per vocabulary. Provides some views support. +i18nfieldcopy.module (jacob singh - jacobsingh at gmail dot net - http://pajamadesign.com) +------------------- +Keeps fields in sync between translated nodes. Go to content field settings (cck) to see the settings + i18nfilter module [Not yet updated for 4.7] ---------------------------------- Provides a filter for language switching in text.