Help me with 'My account' -> 'Edit' form..

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
napoly's picture

Hello people..

I'm trying to populate some forms with flex.. one of the most important is user edit form..[I'm really new to this and drupal].. so I called my simple one-line service return user_edit_form() to see the structure:

Result

Array
(
[account] => Array
(
[#type] => fieldset
[#title] => Account information
[name] => Array
(
[#type] => textfield
[#title] => Username
[#default_value] =>
[#maxlength] => 60
[#description] => Your preferred username; punctuation is not allowed except for periods, hyphens, and underscores.
[#required] => 1
)

[mail] => Array
(
[#type] => textfield
[#title] => E-mail address
[#default_value] =>
[#maxlength] => 64
[#description] => A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.
[#required] => 1
)

[pass] => Array
(
[#type] => password_confirm
[#description] => To change the current user password, enter the new password in both fields.
[#size] => 25
)

[status] => Array
(
[#type] => radios
[#title] => Status
[#default_value] => 1
[#options] => Array
(
[0] => Blocked
[1] => Active
)

)

)

[picture] => Array
(
[#type] => fieldset
[#title] => Picture
[#weight] => 1
[picture_delete] => Array
(
[#type] => hidden
)

[picture_upload] => Array
(
[#type] => file
[#title] => Upload picture
[#size] => 48
[#description] => Your virtual face or picture. Maximum dimensions are 85x85 and the maximum size is 30 kB.
)

)

)

^pretty nice.. so I tried to write my service for it:

  $form_id = 'user_edit_form';

        $form_values = array();
   $form_values['name'] = 'hocigdodopice2';
   $form_values['mail'] = 'ntadldy@gmail.com';
    $form_values['pass'] = 'nap';
  $form_values['init'] = 'ntadldy@gmail.com';
    //$form_values['picture_delete'] = array();
  $form_values['picture'] = 'C:\some_path.jpg;
    $form_values['op'] = 'Submit';
        $form_values['submit'] = 'Submit';
        $form_values['form_id'] = 'user_edit_form';
 
   return user_edit_submit($form_id, $form_values);

ok so it saves all the data to mysql nicely.. with decrypted password.. except the picture.. it get's saved same as provided.. ['C:\some_path.jpg'] and I want it to take the picture from provided path.. upload.. resize.. and save as files/pictures/picture-UID.jpg Am I doing it completely wrong?

Comments

ok.. so here's what I did

napoly's picture

wrote my own .php file named upload_avat.php placed in the drupal root directory on my localhost ..

<?php
    $idcko
=$_POST['uid'];

  
//include_once 'includes/bootstrap.inc';
     //drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
     //$idcko=$user->uid;
    //print t($idcko);

    // Set a maximum height and width
 
$width = 276;
 
$height = 259;

    
$filedir = 'files/'; // the directory for the original image
    
$thumbdir = 'files/pictures/'; // the directory for the thumbnail image
    
$prefix = 'picture-'; // the prefix to be added to the original name

    
$maxfile = '2000000';
    
$mode = '0666';

    
$userfile_name = $_FILES['Filedata']['name'];
    
$userfile_tmp = $_FILES['Filedata']['tmp_name'];
    
$userfile_size = $_FILES['Filedata']['size'];
    
$userfile_type = $_FILES['Filedata']['type'];
//print t($idcko);
     //
    
preg_match('/.([A-Za-z]+?)$/', $_FILES['Filedata']['name'], $matches);
    
$matches[1] = strtolower($matches[1]);
 
//
   
    
if (isset($_FILES['Filedata']['name']))
     {
       
//$idcko=$user->uid;
        //print t($idcko); <-- doesn't work here.. somebody with insane skills should explain..
       
$prod_img = $filedir.$userfile_name;

        
$prod_img_thumb = $thumbdir.$prefix.$idcko;
        
        
move_uploaded_file($userfile_tmp, $prod_img);
        
chmod ($prod_img, octdec($mode));
        
       list(
$width_orig, $height_orig) = getimagesize($prod_img);

       
$ratio_orig = $width_orig/$height_orig;

       if (
$width/$height > $ratio_orig) {
           
$width = $height*$ratio_orig;
       } else {
         
$height = $width/$ratio_orig;
       }

        
$destimg = imagecreatetruecolor($width,$height);
        
         if(
$matches[1] == 'png'){
            
$srcimg = imagecreatefrompng($prod_img);
             
imagecopyresized($destimg,$srcimg,0,0,0,0,$width,$height,imagesx($srcimg),imagesy($srcimg));
          
imagepng($destimg,$prod_img_thumb,100);
           
$png = ".png";
          
//deletes previous picture
            
unlink($prod_img_thumb.$png);
         
rename($prod_img_thumb, $prod_img_thumb.$png);
             }
         if(
$matches[1] == 'jpg' || $matches[1] == 'jpeg'){
            
$srcimg = imagecreatefromjpeg($prod_img);
          
imagecopyresized($destimg,$srcimg,0,0,0,0,$width,$height,imagesx($srcimg),imagesy($srcimg));
          
imagejpeg($destimg,$prod_img_thumb,100);
          
$png = ".jpg";
          
//deletes previous picture
            
unlink($prod_img_thumb.$png);
         
rename($prod_img_thumb, $prod_img_thumb.$png);
             }
         if(
$matches[1] == 'gif'){
            
$srcimg = imagecreatefromgif($prod_img);
          
imagecopyresized($destimg,$srcimg,0,0,0,0,$width,$height,imagesx($srcimg),imagesy($srcimg));
          
imagegif($destimg,$prod_img_thumb,100);
           
$png = ".gif";
          
//deletes previous picture
            
unlink($prod_img_thumb.$png);
         
rename($prod_img_thumb, $prod_img_thumb.$png);
             }      
      
      
imagedestroy($destimg);
       
unlink($prod_img);
        
      
      
//write it to mysql
       
$username="root";
       
//$password="";
     
$database="drupal";

        
$first=$prod_img_thumb.$png;

     
mysql_connect(localhost,$username/*,$password*/);
      @
mysql_select_db($database) or die( "Unable to select database");

      
$query = "UPDATE <code>users</code> SET <code>picture</code> = '$first' WHERE <code>users</code>.<code>uid</code> ='$idcko' LIMIT 1";
       
mysql_query($query);

     
mysql_close();
        
     }
?>

then I'm posting to it through flex.. the uid of a user like this cuz i'm really new to drupal and couldn't import the drupal functionality beyond the if (isset($_FILES['Filedata']['name']) statement:

       private function senDreq(event:ResultEvent):void {
            var variabl:URLVariables = new URLVariables();
            variabl.uid= event.result.toString();//getting result about user id from custom user service

            var reque:URLRequest = new URLRequest("path to your server/drupal/upload_avat.php");

            reque.method=URLRequestMethod.POST;
            reque.data = variabl;

            myFileRefer.upload(reque);
       }

you need to look up the fileReference documentation in flex to get better view on this.. I know this is a HUGE workaround but nobody here is willing to help so it's a solution.. :/ maybe some noob like me can find it useful.. also another issue is deploying this on the server with safe mode ON.. there are some restriction on move_uploaded_file, rename and unlink function which I need to look up..