当前位置: 首页>>代码示例>>PHP>>正文


PHP Attachment::attachProduct方法代码示例

本文整理汇总了PHP中Attachment::attachProduct方法的典型用法代码示例。如果您正苦于以下问题:PHP Attachment::attachProduct方法的具体用法?PHP Attachment::attachProduct怎么用?PHP Attachment::attachProduct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Attachment的用法示例。


在下文中一共展示了Attachment::attachProduct方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: processAddAttachments

 /**
  * Method processAddAttachments() : Change name of file which are uploaded for this product
  * Rules:
  *      - For the first upload the filename has been : name-of-product.extention
  *      - For the second upload : name-of-product-1.extention
  *      - ...
  *
  * @module now_seo_links
  * @return void
  *
  * @see AdminProductsControllerCore::processAddAttachments()
  */
 public function processAddAttachments()
 {
     $languages = Language::getLanguages(false);
     $is_attachment_name_valid = false;
     foreach ($languages as $language) {
         $attachment_name_lang = Tools::getValue('attachment_name_' . (int) $language['id_lang']);
         if (Tools::strlen($attachment_name_lang) > 0) {
             $is_attachment_name_valid = true;
         }
         if (!Validate::isGenericName(Tools::getValue('attachment_name_' . (int) $language['id_lang']))) {
             $this->errors[] = Tools::displayError('Invalid Name');
         } elseif (Tools::strlen(Tools::getValue('attachment_name_' . (int) $language['id_lang'])) > 32) {
             $this->errors[] = sprintf(Tools::displayError('The name is too long (%d chars max).'), 32);
         }
         if (!Validate::isCleanHtml(Tools::getValue('attachment_description_' . (int) $language['id_lang']))) {
             $this->errors[] = Tools::displayError('Invalid description');
         }
     }
     if (!$is_attachment_name_valid) {
         $this->errors[] = Tools::displayError('An attachment name is required.');
     }
     if (empty($this->errors)) {
         if (isset($_FILES['attachment_file']) && is_uploaded_file($_FILES['attachment_file']['tmp_name'])) {
             if ($_FILES['attachment_file']['size'] > Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024) {
                 $this->errors[] = sprintf($this->l('The file is too large. Maximum size allowed is: %1$d kB. The file you\'re trying to upload is: %2$d kB.'), Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024, number_format($_FILES['attachment_file']['size'] / 1024, 2, '.', ''));
             } else {
                 do {
                     $uniqid = sha1(microtime());
                 } while (file_exists(_PS_DOWNLOAD_DIR_ . $uniqid));
                 if (!copy($_FILES['attachment_file']['tmp_name'], _PS_DOWNLOAD_DIR_ . $uniqid)) {
                     $this->errors[] = $this->l('File copy failed');
                 }
                 @unlink($_FILES['attachment_file']['tmp_name']);
             }
         } elseif ((int) $_FILES['attachment_file']['error'] === 1) {
             $max_upload = (int) ini_get('upload_max_filesize');
             $max_post = (int) ini_get('post_max_size');
             $upload_mb = min($max_upload, $max_post);
             $this->errors[] = sprintf($this->l('The file %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'), '<b>' . $_FILES['attachment_file']['name'] . '</b> ', '<b>' . $upload_mb . '</b>');
         } else {
             $this->errors[] = Tools::displayError('The file is missing.');
         }
         if (empty($this->errors) && isset($uniqid)) {
             $attachment = new Attachment();
             foreach ($languages as $language) {
                 if (Tools::getIsset('attachment_name_' . (int) $language['id_lang'])) {
                     $attachment->name[(int) $language['id_lang']] = Tools::getValue('attachment_name_' . (int) $language['id_lang']);
                 }
                 if (Tools::getIsset('attachment_description_' . (int) $language['id_lang'])) {
                     $attachment->description[(int) $language['id_lang']] = Tools::getValue('attachment_description_' . (int) $language['id_lang']);
                 }
             }
             if (Tools::getIsset('name_' . (int) Configuration::get('PS_LANG_DEFAULT'))) {
                 $sFilename = $_FILES['attachment_file']['name'];
                 $sExtention = substr($sFilename, strrpos($sFilename, '.') + 1);
                 $attachment->file_name = Tools::link_rewrite(trim(Tools::getValue('name_' . (int) Configuration::get('PS_LANG_DEFAULT'))));
                 // On regarde si c'est le premier document joint au produit ou pas
                 $aAttachmentOfProduct = $attachment->getAttachments(Context::getContext()->language->id, (int) Tools::getValue('id_product'));
                 $iNb = count($aAttachmentOfProduct);
                 if ($iNb > 0) {
                     $attachment->file_name .= '-' . $iNb;
                 }
                 $attachment->file_name .= '.' . $sExtention;
             }
             $attachment->file = $uniqid;
             $attachment->mime = $_FILES['attachment_file']['type'];
             if (empty($attachment->mime) || Tools::strlen($attachment->mime) > 128) {
                 $this->errors[] = Tools::displayError('Invalid file extension');
             }
             if (!Validate::isGenericName($attachment->file_name)) {
                 $this->errors[] = Tools::displayError('Invalid file name');
             }
             if (Tools::strlen($attachment->file_name) > 128) {
                 $this->errors[] = Tools::displayError('The file name is too long.');
             }
             if (empty($this->errors)) {
                 $res = $attachment->add();
                 if (!$res) {
                     $this->errors[] = Tools::displayError('This attachment was unable to be loaded into the database.');
                 } else {
                     $id_product = (int) Tools::getValue($this->identifier);
                     $res = $attachment->attachProduct($id_product);
                     if (!$res) {
                         $this->errors[] = Tools::displayError('We were unable to associate this attachment to a product.');
                     }
                 }
             } else {
                 $this->errors[] = Tools::displayError('Invalid file');
//.........这里部分代码省略.........
开发者ID:TheTypoMaster,项目名称:neonflexible,代码行数:101,代码来源:AdminProductsController.php

示例2: processAddAttachments

 /**
  * Upload new attachment
  *
  * @return void
  */
 public function processAddAttachments()
 {
     $languages = Language::getLanguages(false);
     $is_attachment_name_valid = false;
     foreach ($languages as $language) {
         $attachment_name_lang = Tools::getValue('attachment_name_' . (int) $language['id_lang']);
         if (Tools::strlen($attachment_name_lang) > 0) {
             $is_attachment_name_valid = true;
         }
         if (!Validate::isGenericName(Tools::getValue('attachment_name_' . (int) $language['id_lang']))) {
             $this->errors[] = Tools::displayError('Invalid Name');
         } elseif (Tools::strlen(Tools::getValue('attachment_name_' . (int) $language['id_lang'])) > 32) {
             $this->errors[] = sprintf(Tools::displayError('Name is too long (%d chars max).'), 32);
         }
         if (!Validate::isCleanHtml(Tools::getValue('attachment_description_' . (int) $language['id_lang']))) {
             $this->errors[] = Tools::displayError('Invalid description');
         }
     }
     if (!$is_attachment_name_valid) {
         $this->errors[] = Tools::displayError('Attachment name required');
     }
     if (empty($this->errors)) {
         if (isset($_FILES['attachment_file']) && is_uploaded_file($_FILES['attachment_file']['tmp_name'])) {
             if ($_FILES['attachment_file']['size'] > Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024) {
                 $this->errors[] = sprintf($this->l('File too large, maximum size allowed: %1$d kB. File size you\'re trying to upload is: %2$d kB.'), Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024, number_format($_FILES['attachment_file']['size'] / 1024, 2, '.', ''));
             } else {
                 do {
                     $uniqid = sha1(microtime());
                 } while (file_exists(_PS_DOWNLOAD_DIR_ . $uniqid));
                 if (!copy($_FILES['attachment_file']['tmp_name'], _PS_DOWNLOAD_DIR_ . $uniqid)) {
                     $this->errors[] = $this->l('File copy failed');
                 }
                 @unlink($_FILES['attachment_file']['tmp_name']);
             }
         } elseif ((int) $_FILES['attachment_file']['error'] === 1) {
             $max_upload = (int) ini_get('upload_max_filesize');
             $max_post = (int) ini_get('post_max_size');
             $upload_mb = min($max_upload, $max_post);
             $this->errors[] = sprintf($this->l('The File %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'), '<b>' . $_FILES['attachment_file']['name'] . '</b> ', '<b>' . $upload_mb . '</b>');
         } else {
             $this->errors[] = Tools::displayError('File is missing');
         }
         if (empty($this->errors) && isset($uniqid)) {
             $attachment = new Attachment();
             foreach ($languages as $language) {
                 if (Tools::getIsset('attachment_name_' . (int) $language['id_lang'])) {
                     $attachment->name[(int) $language['id_lang']] = Tools::getValue('attachment_name_' . (int) $language['id_lang']);
                 }
                 if (Tools::getIsset('attachment_description_' . (int) $language['id_lang'])) {
                     $attachment->description[(int) $language['id_lang']] = Tools::getValue('attachment_description_' . (int) $language['id_lang']);
                 }
             }
             $attachment->file = $uniqid;
             $attachment->mime = $_FILES['attachment_file']['type'];
             $attachment->file_name = $_FILES['attachment_file']['name'];
             if (empty($attachment->mime) || Tools::strlen($attachment->mime) > 128) {
                 $this->errors[] = Tools::displayError('Invalid file extension');
             }
             if (!Validate::isGenericName($attachment->file_name)) {
                 $this->errors[] = Tools::displayError('Invalid file name');
             }
             if (Tools::strlen($attachment->file_name) > 128) {
                 $this->errors[] = Tools::displayError('File name too long');
             }
             if (empty($this->errors)) {
                 $res = $attachment->add();
                 if (!$res) {
                     $this->errors[] = Tools::displayError('Unable to add this attachment in the database');
                 } else {
                     $id_product = (int) Tools::getValue($this->identifier);
                     $res = $attachment->attachProduct($id_product);
                     if (!$res) {
                         $this->errors[] = Tools::displayError('Unable to associate this attachment to product');
                     }
                 }
             } else {
                 $this->errors[] = Tools::displayError('Invalid file');
             }
         }
     }
 }
开发者ID:jicheng17,项目名称:pengwine,代码行数:86,代码来源:AdminProductsController.php


注:本文中的Attachment::attachProduct方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。