2017-04-05 83 views
0

我已經覆蓋ProductController.php控制器,以便客戶可以將PDF集成到他的訂單中。將定製文件關聯到client_id

protected function pictureUpload() 
{ 
    if (!($field_ids = $this->product->getCustomizationFieldIds())) 
     return false; 
    $authorized_file_fields = array(); 
    foreach ($field_ids AS $field_id) { 
     if ($field_id['type'] == Product::CUSTOMIZE_FILE) 
      $authorized_file_fields[(int)$field_id['id_customization_field']] = 'file' . (int)$field_id['id_customization_field']; 
    } 

    $indexes = array_flip($authorized_file_fields); 
    foreach ($_FILES AS $field_name => $file_doc) { 
     if (in_array($field_name, $authorized_file_fields) AND isset($file_doc['tmp_name']) AND !empty($file_doc['tmp_name'])) { 

      /*if ($_POST[$field_name.'_filename'] != '') { 
       $testName = $_POST[$field_name . '_filename']; 
      } else if (isset($_FILES[$field_name]['name'])){ 
       $testName = $_FILES[$field_name]['name']; 
      } else if ($_FILES['file5'['name']] != ''){ 
       $testName = 'totop'; 
      }*/ 

      // If there is an upload error, let the parent handle it 
      if ($file_doc['error'] != UPLOAD_ERR_OK) 
       continue; 

      // If the file is not allowed, let the parent handle it 
      if (!$this->isUploadTypeAllowed($file_doc)) 
       continue; 

      // Unset the PDF to prevent the parent to handle this file 
      unset($_FILES[$field_name]); 

      // Create dir 
      mkdir(_PS_UPLOAD_DIR_ . ProductController::CUSTOMIZATION_FILE_DIR . '/' . $this->context->cart->id, 0777, true); 

      // Mark the file as a custom upload 
      $file_name = ProductController::CUSTOMIZATION_FILE_DIR . '/' . $this->context->cart->id . '/P' . md5(uniqid(rand(), true)) . '.pdf'; 

      $tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS'); 
      if (!move_uploaded_file($file_doc['tmp_name'], $tmp_name)) { 
       $this->errors[] = Tools::displayError('An error occurred during the PDF upload.'); 
       return false; 
      } 
      // Copy file to the upload dir 
      if (!copy($tmp_name, _PS_UPLOAD_DIR_ . $file_name)) { 
       $this->errors[] = Tools::displayError('An error occurred during the PDF upload.'); 
       return false; 
      } 
      // Chmod the new file 
      if (!chmod(_PS_UPLOAD_DIR_ . $file_name, 0777)) { 
       $this->errors[] = Tools::displayError('An error occurred during the PDF upload.'); 
       return false; 
      } 

      // Create a fake thumb to avoid error on delete, this hack avoids lots of core method override 
      file_put_contents(_PS_UPLOAD_DIR_ . $file_name . '_small', ''); 
      chmod(_PS_UPLOAD_DIR_ . $file_name . '_small', 0777); 

      // Register the file 
      $this->context->cart->addPictureToProduct($this->product->id, $indexes[$field_name], Product::CUSTOMIZE_FILE, $file_name); 

      // Parsing file 
      exec('/usr/bin/pdfinfo ' . _PS_UPLOAD_DIR_ . $file_name, $output); 
      $arrayValue = preg_grep('/\b(Pages)\b/i', $output); 
      $arrayKey = count($arrayValue) > 0 ? key($arrayValue) : false; 

      // Size file 
      $fileSize = fileSize(_PS_UPLOAD_DIR_ . $file_name); 

      // Name file 
      $nameFile = explode(".", $file_doc['name']); 
      $this->context->smarty->assign('customizationParsingFile', (int)substr($arrayValue[$arrayKey], -5)); 
      $this->context->smarty->assign('customizationFileSize', $this->fileSizeUpload($fileSize)); 
      $this->context->smarty->assign('customizationNameFile', $nameFile[0]); 

      // Remove tmp file 
      unlink($tmp_name); 
     } 
    } 
    return parent::pictureUpload(); 
} 

我想這PDF與客戶端相關聯。你有什麼想法我可以做到這一點?

謝謝。

回答

0

Admin Order的頁面您可以獲得Cart ID與當前Order相關聯。 CustomizationCart ID(檢查Customization.php)相關聯。 因此,從Cart ID您可以得到您的Customization和該客戶的PDF。