--- dependent 5/dependent.info +++ dependent 6/dependent.info @@ -1,12 +1,10 @@ -; $Id: dependent.info,v 1.1 2008/03/07 09:42:10 levyofi Exp $ +; $Id: dependent.info name = Dependent description = Make CCK fields dependent of the values of other fields. -dependencies = content +dependencies[] = content package = CCK +core = 6.x - -; Information added by drupal.org packaging script on 2008-05-31 -version = "5.x-1.4-beta" +; Information added by drupal.org +version = "6.x-1.0-beta" project = "dependent" -datestamp = "1212257405" - --- dependent 5/dependent.install +++ dependent 6/dependent.install @@ -1,4 +1,5 @@ 'value', + '#value' => 1 + ); + if ($form_id == 'content_field_main_form' && isset($form_state['values']['step']) && $form_state['values']['step']==1) { //need to add our form element to the form where field types are created/updated - //get the names of the related content type and the field type $content_type = content_types($form['type_name']['#value']); $field_name=$form['field_name']['#value']; @@ -18,15 +23,33 @@ if ($options===FALSE){ return $form; }else{ - $form['#submit']['dependent_submition'] = array($options); //this is how we tell drupal that during submition the 'dependent_submition' function should be called + $form['step'] = array( + '#type' => 'value', + '#value' => 0 + ); + $form['options'] = array( + '#type' => 'value', + '#value' => $options + ); + //$form['#submit']['dependent_submition'] = array($options); + $form['#submit'][] = 'dependent_submition';//this is how we tell drupal that during submition the 'dependent_submition' function should be called } } + else if ($form_id == 'content_field_edit_form' && count($form_state['post']) && $form_state['post']['op'] != 'Change basic information'){ + $is_required = (int)$form_state['post']['required']; + $content_type_tmp = content_types($form['type_name']['#value']); + $content_type = $content_type_tmp['type']; + $field_name = $form['field_name']['#value']; + db_query("UPDATE {field_dependencies} SET is_required = %s WHERE child_node_type_name ='%s' AND child_field_name = '%s';", $is_required, $content_type, $field_name); + } else if (strpos($form_id,'_node_form')){//this is a node form $content_type = $form['type']['#value']; $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s'", $content_type); - if (db_num_rows($dependencies)>0){ + $dependencies_num_row=db_result(db_query("SELECT COUNT(*) FROM {field_dependencies} WHERE parent_node_type_name='%s'", $content_type)); + if ($dependencies_num_row>0){ dependent_unset_required_before_validation($form_id, $form); - $form['#validate']['dependent_validate_required_fields']=array(); + //$form['#validate']['dependent_validate_required_fields'] = array(); + $form['#validate'][] = 'dependent_validate_required_fields'; } while ($field=db_fetch_array($dependencies)){ //the while loop is to support future development where a field can be dependent on many parents if (isset($form[$field['child_field_name']])){ @@ -61,29 +84,36 @@ drupal_add_css(drupal_get_path('module', 'dependent') .'/dependent.css'); drupal_add_js(drupal_get_path('module', 'dependent') .'/dependent.js'); } - else if ($form_id == '_content_admin_field_remove') { //a field removal page - need to update the database - $form['#submit']['dependent_content_admin_field_remove_submit'] = array(); + else if ($form_id == 'content_field_remove_form') { //a field removal page - need to update the database + //$form['#submit']['dependent_content_admin_field_remove_submit']= array(); + $form['#submit'][]= 'dependent_content_admin_field_remove_submit'; + } + // Delete content type + else if ($form_id == 'node_type_delete_confirm') { + $form['#submit'][]= 'dependent_content_type_remove_submit'; } } - /* * this function is being called when the admin saves field settings * $options - an array with all the options that this field can depend on. */ -function dependent_submition($form_id, $form_values, $options) { - if ($form_id == '_content_admin_field') { +function dependent_submition($form, &$form_state) { + + $options=$form_state['values']['options']; + if ($form_state['values']['form_id'] == 'content_field_main_form') { + //this is a submition of adding or editing a field type - if (isset($form_values['dependent_options'])){ + if (isset($form_state['values']['dependent_options'])){ //get the names of the content type and field type - $content_type = $form_values['type_name']; - $field_name=$form_values['field_name']; + $content_type = $form_state['values']['type_name']; + $field_name=$form_state['values']['field_name']; //check if this is an update operation - $is_updatable=db_num_rows(db_query("SELECT * FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type)); + $is_updatable=db_result(db_query("SELECT COUNT(*) FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type)); //get the selected choice - $choice=strval($form_values['dependent_options']); + $choice=strval($form_state['values']['dependent_options']); $option=explode(" option from ",$options[$choice]); ($choice>0)?($selected_value=(count($option)>1)?$option[0]:'no_value_needed'):''; ($choice>0)?$selected_field = ($option[1]?$option[1]:$options[$choice]): ''; @@ -93,23 +123,29 @@ db_query("DELETE FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type); } //enter the choice to the database - ($choice>0)?db_query("INSERT INTO {field_dependencies} VALUES ('%s','%s','%s','%s','%s',%s, %s)",$selected_field, $field_name, $content_type, $content_type, $selected_value, $form_values['put_after_parent'], $form_values['required']):''; + ($choice>0)?db_query("INSERT INTO {field_dependencies} VALUES ('%s','%s','%s','%s','%s',%s, %s)",$selected_field, $field_name, $content_type, $content_type, $selected_value, $form_state['values']['put_after_parent'], 0):''; } } } -/* - * this function is being called when the admin removes a field - * $options - an array with all the options that this field can depend on. - */ -function dependent_content_admin_field_remove_submit($form_id, $form_values) { - $content_type = $form_values['type_name']; - $field_name=$form_values['field_name']; - if ( $content_type && $field_name && $form_values['confirm']) {//remove this field dependencies data frm database +function dependent_content_admin_field_remove_submit($form, &$form_state) { + + $content_type = $form_state['values']['type_name']; + $field_name=$form_state['values']['field_name']; + + if ( $content_type && $field_name && $form_state['values']['confirm']) {//remove this field dependencies data frm database db_query("DELETE FROM {field_dependencies} WHERE (child_field_name='%s' OR parent_field_name='%s') AND child_node_type_name='%s'", $field_name, $field_name, $content_type); } } +function dependent_content_type_remove_submit($form, &$form_state) { + $content_type = $form_state['values']['type']; + if ( $content_type && $form_state['values']['confirm']) {//remove this field dependencies data frm database + db_query("DELETE FROM {field_dependencies} WHERE (parent_node_type_name ='%s' OR parent_node_type_name ='%s')", $content_type , $content_type); + } + +} + /* * Implementation of hook_form_alter() */ @@ -140,47 +176,37 @@ } } -/* - * This function return an array of options in which a field can be depends on. it also adds the nessecery form elements to the form - * @param $content_types - * An array that represents the content types involved, with two keys 'parent' as the parent content type and 'child' as the dependent content type. - * - * @param $form - * The form to add the possible options that the admin should choose from - * - * $param $field_name - * The field name that this setting is for. Use null if this field is from another content tyoe - * - * @param $new_for_name - * The name of the fieldset item to add to $form - * - * @param $weight - * The weight that the form item will get in the form - * - * @param $form_value_name - * The initial for the form items in the fielset - this is useful when adding more than one fieldsets in other modules - */ - -function dependent_create_form_options($content_types, &$form, $field_name=NULL, $new_form_name='dependent', $weight=-5, $form_value_name=""){ +function dependent_create_form_options($content_types, &$form, $field_name=NULL, $new_form_name='dependent', $weight=0, $form_value_name=""){ if (isset($field_name)){ - $onoff_field_instances=db_query('SELECT * FROM {node_field_instance} WHERE widget_type="options_onoff" and type_name="%s" and field_name<>"%s"', $content_types['parent'],$field_name ); - $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name); - $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name); + $onoff_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s" and field_name<>"%s"', $content_types['parent'],$field_name )); + $options_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name)); + $selection_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name)); }else{ - $onoff_field_instances=db_query('SELECT * FROM {node_field_instance} WHERE widget_type="options_onoff" and type_name="%s"', $content_types['parent']); - $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']); - $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']); + $onoff_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s"', $content_types['parent'])); + $options_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent'])); + $selection_field_instances_num_row=db_result(db_query('SELECT COUNT(*) FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent'])); } $row=1; - if ((db_num_rows($onoff_field_instances)==0)&&(db_num_rows($options_field_instances)==0)&& (db_num_rows($selection_field_instances)==0)) //no field type can be selected + if (($onoff_field_instances_num_row==0)&&($options_field_instances_num_row==0)&& ($selection_field_instances_num_row==0)) //no field type can be selected return FALSE; - + + if (isset($field_name)){ + $onoff_field_instances=db_query('SELECT * FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s" and field_name<>"%s"', $content_types['parent'],$field_name ); + $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name); + $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name); + }else{ + $onoff_field_instances=db_query('SELECT * FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s"', $content_types['parent']); + $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']); + $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {content_node_field_instance} nfi,{content_node_field} nf WHERE nfi.widget_type="optionwidgets_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']); + } //the first oprion is "no field" $options[0]=t("no field"); //add the single on/off checkboxes to the options array + while($onoff_field=db_fetch_object($onoff_field_instances)){ $options[$row++]="$onoff_field->field_name";// in $onoff_field->type_name"; } + //add the options on each radio buttons field type. every single choice will be added as a separate choice which includes the choice text and the field type name while($options_field=db_fetch_object($options_field_instances)){ $field_details=content_fields($options_field->field_name,$content_types['parent']); @@ -188,7 +214,6 @@ //iterate through the array and put the options in $options together with the field name foreach($field_options_array as $option){ $options[$row++]=$option.' '.t("option from").' '.$options_field->field_name; - //print_r($options); } } @@ -199,13 +224,14 @@ //iterate through the array and put the options in $options together with the field name foreach($field_options_array as $option){ $options[$row++]=$option.' '.t("option from").' '.$options_field->field_name; - //print_r($options); } } //if this is an update operation we need to load the previously selected valued to the options list $prev_result=db_query('SELECT * FROM {field_dependencies} WHERE child_field_name="%s" AND parent_node_type_name="%s" AND child_node_type_name="%s"', isset($field_name)?$field_name:"related", $content_types['parent'],$content_types['child']); - $is_updatable=db_num_rows($prev_result); + $prev_result_num_row=db_result(db_query('SELECT COUNT(*) FROM {field_dependencies} WHERE child_field_name="%s" AND parent_node_type_name="%s" AND child_node_type_name="%s"', isset($field_name)?$field_name:"related", $content_types['parent'],$content_types['child'])); + + $is_updatable=$prev_result_num_row; $prev_selected=array(); $put_after_parent_flag=FALSE; while ($parent = db_fetch_array($prev_result)){ @@ -238,9 +264,6 @@ return $options; } -/* - * This function look for dependent fields in the form and unset the required flag if exists - */ function dependent_unset_required_before_validation($form_id, &$form){ $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s' AND is_required=1", $form['type']['#value']); while ($field=db_fetch_array($dependencies)){ @@ -263,7 +286,7 @@ if (strval($key)=='#required') { unset($element[$key]); $dependent_required_fields[]=$field_name; - $element['#title'].= '*'; + $element['#title'].= ''; return; } @@ -278,16 +301,17 @@ * it looks for the required fields that the function 'dependent_find_and_destroy_required_flag' have set as not required. * it then checks if this field was supposed to be filled. If yes, the function checks whether the field has a value and if not it calles 'form_set_error' */ -function dependent_validate_required_fields($form_id, $form_values){ - $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s' AND is_required=1", $form_values['type']); +function dependent_validate_required_fields($form, &$form_state){ + $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s' AND is_required=1", $form_state['values']['type']); while ($field=db_fetch_array($dependencies)){ - if (($form_values[$field['parent_field_name']]['key']===$field['option_text'])||(($field['option_text']=='no_value_needed')&&($form_values[$field['paremt_field_name']]===1))){//check if the parent field has the right value for the child field to be filled - if (((isset($form_values[$field['child_field_name']]['key']))&&($form_values[$field['child_field_name']]['key']===""))||((isset($form_values[$field['child_field_name']][0]))&&($form_values[$field['child_field_name']][0]['value']===""))){//check if the child field has a value + if (($form_state['values'][$field['parent_field_name']]['key']===$field['option_text'])||(($field['option_text']=='no_value_needed')&&($form_state['values'][$field['paremt_field_name']]===1))){//check if the parent field has the right value for the child field to be filled + if (((isset($form_state['values'][$field['child_field_name']]['key']))&&($form_state['values'][$field['child_field_name']]['key']===""))||((isset($form_state['values'][$field['child_field_name']][0]))&&($form_state['values'][$field['child_field_name']][0]['value']===""))){//check if the child field has a value //the child field does not have a value - $field_data=content_fields($field['child_field_name'],$form_values['type']); + $field_data=content_fields($field['child_field_name'],$form_state['values']['type']); form_set_error('dependent',t('!name field is required.', array('!name' => $field_data['widget']['label']))); } } } } -?> + +?> \ No newline at end of file