'admin/settings/image-fields', 'title' => t('Content Image fields'), 'callback' => 'drupal_get_form', 'callback arguments' => array('rename_fields_admin_settings'), 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, 'description' => t('Image rename field settings.'), ); } return $items; } function alter_image_name_init(){ if(arg(1)=="content" && arg(2)=="type"){ variable_del("image_row"); } } function rename_fields_admin_settings(){ $form=array(); $sql="SELECT field_name,label FROM {node_field_instance} WHERE widget_type='image' GROUP BY field_name"; $rs=db_query($sql); $image_set=variable_get("image_row",''); $row_count=db_num_rows($rs); if(count($image_set)==$row_count){ $check=false; }else{ $check=true; $image_rest=array_flip($image_set); } $form['image_fieldset']=array("#type"=>"fieldset"); while($row=db_fetch_object($rs)){ if($check && $image_rest[$row->field_name]!=''){ $form['image_fieldset'][$row->field_name]=array( "#type"=>"checkbox", "#title"=>$row->label, "#attributes"=>array( "checked"=>$flag ) ); }else{ $form['image_fieldset'][$row->field_name]=array( "#type"=>"checkbox", "#title"=>$row->label, ); } } $form['Submit']=array("#type"=>"submit","#value"=>"Submit"); return $form; } function rename_fields_admin_settings_submit($form_id,$form_values){ $image_set=array(); foreach($form_values as $key => $values){ if($values=="1"){ $image_set[]=$key; } } variable_set("image_row",$image_set); } function alter_image_name_form_alter($form_id, &$form) { $sql_image_fields="SELECT field_name,type FROM {node_field} WHERE type='image'"; $image_row=variable_get("image_row",''); if($image_row==""){ $image_rs=db_query($sql_image_fields); $image_row=array(); if(db_num_rows($image_rs)>0){ while($image_obj=db_fetch_object($image_rs)){ $image_row[]=$image_obj->field_name; } variable_set("image_row",$image_row); } } if(!empty($image_row) && is_array($image_row)){ foreach($image_row as $key => $value){ if(!empty($form[$value]) && is_array($form[$value])){ foreach($form[$value] as $key1 => $value1){ if(!empty($value1) && is_array($value1)){ foreach($value1 as $key2 => $value2){ if($key2=="#type" && $value2=="file"){ $form["image_renamed_$key1"]=array("#type"=>"textfield", '#title'=>$form[$value]['#title']." Rename", '#attributes' => array('disabled'), "#description"=>"", "#weight"=>$form[$value]['#weight'], '#value' => ($form[$value][0]['filename']!='')?($form[$value][0]['filename']['#value']):(DEFAULT_FILENAME), "#prefix"=>"
", "#suffix"=>"
"); } } } } } } } } function alter_image_name_nodeapi(&$node, $op){ drupal_add_js(drupal_get_path('module', 'alter_image_name') .'/alter_image_name.js', 'module'); $image_row=variable_get("image_row",''); switch($op){ case "submit": case "prepare": case "insert": case "update": if(!empty($_FILES['files']['name'])){ foreach($image_row as $key => $value){ foreach($_FILES['files']['name'] as $key1 => $value1){ if(trim($value1)&& $_POST["image_renamed_$key1"]!='' && trim($_POST["image_renamed_$key1"])!=DEFAULT_FILENAME){ $type_dt="-".date("dmy").substr($_FILES['files']['name'][$key1],strlen($_FILES['files']['name'][$key1])-4,strlen($_FILES['files']['name'][$key1])); if($key1==$value."_upload") $_FILES['files']['name'][$key1]=build_name(substr($_POST["image_renamed_$key1"],0,25)).$type_dt; } } } } } } function build_name($replace_str){ $replace=array('?','@','%','!','$','*',')', '(','^','{','}','[',']',';', ':',',','>','<','"',"'","/", "\\"," ","_"); $frist_char=array("-","_","0","1","2","3","4","5","6","7","8","9"); $replace_str=str_replace($replace,"-",$replace_str); $replace_str=str_replace($frist_char,"A",substr($replace_str,0,1)).substr($replace_str,1); return $replace_str; }