2016-05-15 34 views
0

我想改變$ this-> fields_form當一個字段的值點擊提交時,即在值進入表我想要改變形式值對此我要插入如何設置隱藏或文本字段的值當用戶點擊保存在prestashop1.6

// This form is populated when add or edit is clicked 
    public function renderForm() 
    { 
    $years  = Tools::dateYears(); 
    $months  = Tools::dateMonths(); 
    $days   = Tools::dateDays(); 
    $ticketSeries = Winners::getTicketSeries(); 
    $prdtCategory = Winners::getProductCategory(); 
    $nationality = Winners::getNationality(); 
    $firstArray = 
      array(array(
       'type' => 'text', 
       'label' => $this->l('Name'), 
       'name' => 'name', 
       'required' => true, 
       'col' => '4', 
       'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()@#"°{}_$%:' 
      ), 
       array(
       'type'  => 'file', 
       'label' => $this->l('Winner Image'), 
       'name'  => 'winner_image', 
       'display_image' => true, 
       'required' => false 
      ), 
      array(
       'type' => 'text', 
       'label' => $this->l('Ticket No'), 
       'name' => 'ticket_no', 
       'required' => true, 
       'col' => '4', 
       'hint' => $this->l('Invalid characters:').' 0-9!<>,;?=+()@#"°{}_$%:' 
      ), 
      array(
       'type' => 'select', 
       'label' => $this->l('Ticket Series'), 
       'name' => 'series', 
       'required' => true, 
       'options' => array(
        'query' => $ticketSeries, 
        'id' => 'ticket_series_name', 
        'name' => 'ticket_series_name' 
       ), 
       'col' => '4', 
       'hint' => array(
        $this->l('The ticket series of each draw !!.') 
       ) 
      ), 
      array(
       'type' => 'select', 
       'label' => $this->l('Category'), 
       'name' => 'category', 
       'required' => true, 
       'options' => array(
        'query' => $prdtCategory, 
        'id' => 'name', 
        'name' => 'name' 
       ), 
       'col' => '4', 
       'hint' => array(
        $this->l('Product Category.') 
       ) 
      ), 

      array(
       'type' => 'date', 
       'label' => $this->l('Draw Date:'), 
       'name' => 'draw_date', 
       'size' => 10, 
       'required' => true, 
       'desc' => $this->l('The draw date of this series'), 
      ), 
       array(
       'type' => 'select', 
       'label' => $this->l('Nationality'), 
       'name' => 'nationality', 
       'required' => true, 
       'options' => array(
        'query' => $nationality, 
        'id' => 'name', 
        'name' => 'name' 
       ), 
       'col' => '4', 
       'hint' => array(
        $this->l('Nationality the winner Belongs .') 
       ) 
      ), 

      array(
       'type' => 'textarea', 
       'label' => $this->l('Testimonial'), 
       'name' => 'testimonial', 
       'required' => true, 
       'autoload_rte' => true, 
       'rows' => 7, 
       'cols' => 40, 
       'hint' => $this->l('Invalid characters:').' <>;=#{}' 
      ), // add tag 'autoload_rte' => true, 'lang' => true, --> lang i removed 
       // since the editor value did not submit editor     
     ); 

    if (Tools::getIsset('addkits_winners')){ 
      $secondArray = array(
      array(
       'type' => 'hidden', 
       'label' => $this->l('Add Date'), 
       'name' => 'date_add',     
       'col' => '4',     
       'values'=>date("Y-m-d H:i:s"), 
       'hint' => $this->l('Invalid characters:').' 0-9!&lt;&gt;,;?=+()@#"°{}_$%:' 
      ), 
      array(
       'type' => 'hidden', 
       'label' => $this->l('Winner Image Name'), 
       'name' => 'winner_image_name',     
       'col' => '4',     
       'values'=>"defalt_value" 
      ) 
      ); 
      $mainArray = array_merge($firstArray,$secondArray); 
     } 

     if (Tools::getIsset('updatekits_winners')){ 
      $thirdArray = array(
      array(
       'type' => 'hidden', 
       'label' => $this->l('Update Date'), 
       'name' => 'date_upd',     
       'col' => '4',     
       'values'=>date("Y-m-d H:i:s"), 
       'hint' => $this->l('Invalid characters:').' 0-9!&lt;&gt;,;?=+()@#"°{}_$%:' 
      ), 
       array(
       'type' => 'text', 
       'label' => $this->l('Winner Image Name'), 
       'name' => 'winner_image_name',     
       'col' => '4',     
       'values'=>"defalt_value" 
      )); 
      $mainArray = array_merge($firstArray,$thirdArray); 
     } 


    $this->fields_form = array(
     'tinymce' => true, 
     'legend' => array(
      'title' => $this->l('Configure your Winner'), 
      'icon' => 'icon-user' 
     ),   
     'input' => $mainArray 
    ); 
    //Assign value to hidden 
    $this->fields_value['date_add'] = $date = date("Y-m-d H:i:s"); 
    $this->fields_value['winner_image_name'] ="default_image.jpg"; 
    $this->fields_form['submit'] = array(
     'title' => $this->l('Save'), 
    ); 
    $date = date("Y-m-d H:i:s"); 
    $this->addJqueryUI('ui.datepicker'); 

    return parent::renderForm(); 
} 











public function postProcess() 
{ 
    $this->getContent(); 
    $winner_image_name   = (string) Tools::getValue('winner_image_name'); 
    // d($winner_image_name); 
    parent::postProcess(); 

} 

在這裏,我想改變winner_image_name的值(場),即當圖像被uploaed我想改變字段名稱,如果圖像沒有上傳我想保持默認值

public function getContent() 
    { 
    $output = null;  

    if(Tools::isSubmit('submitAddkits_winners')) 
    {   
     // d("rechaed here");  
     $winner_image  = $_FILES['winner_image']; 
     $ticket_no   = (string) Tools::getValue('ticket_no'); 
     //d($ticket_no); 

     if($winner_image['name'] != "") 
     { 
      //Format allowed i 
      $allowed = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png'); 

      //check allowed formats 
      if(in_array($winner_image['type'], $allowed)) 
      { 
       $path = '../modules/addwinners/'; 
       $ext = pathinfo($winner_image['name'], PATHINFO_EXTENSION); 

       $newfilename = round(microtime(true)) . '_'.$ticket_no; 
       $this->fields_value['winner_image_name']=$newfilename.".".$ext; 
       $helper->fields_value['winner_image_name'] = ""; 

       if(! move_uploaded_file($winner_image['tmp_name'], $path.$newfilename.".".$ext)) 
       { 
        $output .= $this->displayError($path.$stilogo_image['name']); 
        return $output.$this->displayForm(); 
       } 
      } 
      else 
      { 
       $output .= $this->displayError($this->l('Image formated Not Supported.')); 
       return $output.$this->displayForm(); 
      } 
     } 



     //Se arrivo qui è perchè tutti i campi obbligatori sono stati riempiti quindi aggiorno i valori 

     //Configuration::updateValue('STILOGOPOPUP_IMAGE', $winner_image['name']); 

     // $output .= $this->displayConfirmation($this->l('Impostazioni salvate'));      
    } 

    return $output.$this->html;; 
} 

回答

1

我想你應該使用p ostProcess代替OR或者其他方式也可以使用Init(),但以下示例適用於後處理

public function postProcess() 
{ 
    parent::postProcess(); 
    $id = (int)Tools::getValue('id_blahblah'); 
    $file = Tools::fileAttachment('img_any'); 
    if (!empty($file['name']) && $id > 0) 
    { 
     if (ImageManager::validateUpload($file, Tools::convertBytes(ini_get('upload_max_filesize')))) 
      die('Image size exceeds limit in your PrestaShop settings'); 
     if (!is_dir(_PS_IMG_DIR_.'blah')) 
      @mkdir(_PS_IMG_DIR_.'blah', 0777, true); 
     if (!is_dir(_PS_IMG_DIR_.'blah/'.$id)) 
      @mkdir(_PS_IMG_DIR_.'blah/'.$id, 0777, true); 

     $path = _PS_IMG_DIR_.'blah/'.$id.'/'; 
     $absolute_path = $path.$file['name']; 
     move_uploaded_file($file['tmp_name'], $absolute_path); 
     $imgPath = 'blah/'.$id.'/'.$file['name']; 

     //Save in DB if needed 
     Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'blah` 
     SET `img` = "'.pSQL($imgPath).'" 
     WHERE `id` = '.(int)$id); 
    } 
} 
相關問題