edgar.dueñas
Enviado por edgar.dueñas el Mié, 11/05/2016 - 15:29

Este script es un formulario que se utiliza para subir un archivo al servidor y guardarlo en un campo dentro de las propiedades del usuario:

almacenar un archivo con user-save

 

almacenar un archivo con user-save

 

<?php
 
/***
 * Implementa hook_block_info().
 */
function transaction_money_block_info() {
 
    $blocks['confidential-agreement'] = array(
        'info' => 'Confidential Agreement',
    );        
 
    return $blocks;
}
 
 
/****
 * Implementa hook_block_view().
 */
function transaction_money_block_view($delta = '') {
    switch ($delta) {        
        case 'confidential-agreement':
            $block['content'] = drupal_get_form('transaction_money_confidential_agreement_form');
            break;              
    }
 
    return $block;
}
 
function transaction_money_confidential_agreement_form($form, &$form_state) {
 
  global $user;
 
  $uid = $user->uid;
  $account = user_load($uid);
 
  $form = array();
 
  // Attach confidential agreement upload
  $form['form_user']['agreement'] = array(
    '#type' => 'file',
    '#title' => t('Confidential Agreement')
  );
 
  $form['submit'] = array(
      '#prefix' => '<div class="submit-form">',
      '#suffix' => '</div>',      
      '#type' => 'submit',
      '#value' => 'Submit'
      );
 
  return $form;    
 
}
 
/****
 * Validate upload file
 * @param type $form
 * @param type $form_state
 */
function transaction_money_confidential_agreement_form_validate($form, &$form_state) {
 
  $file = file_save_upload('agreement', array(
      'file_validate_extensions' => array('pdf')
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['agreement'] = $file;
    }
    else {
      form_set_error('profile', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('profile', t('No file was uploaded.'));
  }    
 
}
/****
 * Upload file
 * @global type $user
 * @param type $form
 * @param type $form_state
 */
function transaction_money_confidential_agreement_form_submit($form, &$form_state) {
 
  global $user;
 
  $uid = $user->uid;
  $account = user_load($uid);
 
  $file = $form_state['values']['agreement'];
 
  unset($form_state['values']['agreement']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
 
  //dpm($file);
 
   $edit = array(
      'field_confidential_agreement' => array(
        'und' => array(
          0 => array(
            'fid' => $file->fid,  
            'uid' => $file->uid,    
            'filename' => $file->filename,
            'uri' => $file->uri,
            'filemime' => $file->filemime, 
            'filesize' => $file->filesize, 
            'status' => $file->status,
            'timestamp' => '$file->timestamp',
            'rdf_mapping' => '',
            'display' => '1',
            'description' => ''              
          ),
        ),
      ),
    );
 
  user_save($account, $edit);
}
 
 
?>