将customizationFile与client_id相关联

时间:2017-04-05 08:30:02

标签: php smarty prestashop

我已经覆盖了ProductController.php控制器,以便客户可以将PDF集成到他的订单中。

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与客户端相关联。你知道我怎么能这样做吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

Admin Order页面中,您可以Cart ID与当前Order相关联。 CustomizationCart ID相关联(检查Customization.php)。 因此,从Cart ID,您可以获得Customization和该客户的PDF。

相关问题