magento 添加产品上传附件(中3)

承接上篇配置文件和帮助类

下面是发送邮件给作者的model,可有可无

app\code\local\Iverve\Uploadpdf\Model
app\code\local\Iverve\Uploadpdf\Model\Extensioncheck.php
class Iverve_Uploadpdf_Model_Extensioncheck extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('uploadpdf/extensioncheck');
}   
}

下面是上传附件的model,核心

app\code\local\Iverve\Uploadpdf\Model\Observer.php
class Iverve_Uploadpdf_Model_Observer
{
static protected $_singletonFlag = false;
static protected $_setToName = "Extension Geek";
static protected $_setToEmail= "作者的邮箱";

public function saveProductTabData(Varien_Event_Observer $observer)
{
if(!self::$_singletonFlag)
{
self::$_singletonFlag = true;
$product       =  $observer->getEvent()->getProduct();
$productid     =  $product->getId();
$data          =  $this->_getRequest()->getPost();
$_delEditId   =  $this->_getRequest()->getParam('editid');
$pdfMediaDir   =  Mage::getBaseDir(). DS. "media". DS ."iverve". DS ."uploadpdf". DS;
$pdfMediaLink  =  Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)."media/iverve/uploadpdf/"; 

try{
if($data)
{
$mediaFile          = $_FILES['upload_pdf'];
$pdfname            = $data['pdf_name'];
$data['upload_pdf'] = $mediaFile['name'];
$pdftempfile        = $mediaFile['tmp_name'];
$front              = strtotime(date('Y-m-d H:i:s'))."_".$data['upload_pdf'];
$pdfMediaLink       = $pdfMediaLink.$front;

if(!empty($data['chkdelete'])){ 
$this->deletePDF($data['chkdelete']);	
}
if($_delEditId == ''){
$_uploadPdfModel = Mage::getModel('uploadpdf/uploadpdf');
$_uploadPdfData  = $_uploadPdfModel->getCollection()
		   ->addFieldToFilter('pdf_file', $mediaFile['name'])
		   ->addFieldToFilter('pdfname', $pdfname)
		   ->addFieldToFilter('product_id', $productid);
if(isset($mediaFile['name']) && ($mediaFile['name'] != ''))								
if($data['pdf_name'] == '')
{
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('Please Enter PDF file name.'));
}
elseif($_uploadPdfData->count())
{
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('Record already exist with Same Name and File'));
}
elseif($mediaFile['type'] == 'application/pdf' || $mediaFile['type'] == 'application/zip' || $mediaFile['type'] == 'image/jpeg' || $mediaFile['type'] == 'image/png' || $mediaFile['type'] == 'application/msword' || $mediaFile['type'] == 'application/vnd.ms-excel' || $mediaFile['type'] == 'application/octet-stream')
{
$connection = Mage::getSingleton('core/resource')->getConnection('你的数据库名称');//连接数据库
$query = "SELECT count(*) as AllNum FROM iverve_uploadpdf WHERE product_id = '".$productid."'";//获取表的记录条数
$count =$connection->fetchAll($query);
$count_id = $count['0']['AllNum'];

if($count_id<=4){//限制上传的文件数不能大于5
move_uploaded_file($pdftempfile,$pdfMediaDir.$front);	
$imgPath = $pdfMediaDir.$front;
$data    = array( 'product_id'    => $productid, 'pdf_file' => $mediaFile['name'],
			   'pdfname'       => $pdfname,   'pdf_path' => $pdfMediaLink,
			   'pdf_pathfile'  => $front);
$model   = Mage::getModel('uploadpdf/uploadpdf')->setData($data); 
$model->save();
}else{
	Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('Only upload 5 files'));
}
}else{
	Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('Only PDF ZIP RAR JPG PNG DOC XLS Files allowed'));
}
}
else
{
$_uploadPdfModel = Mage::getModel('uploadpdf/uploadpdf');

if($pdfname != '')
{
$_nameChkData  = $_uploadPdfModel->getCollection()
	 ->addFieldToFilter('pdfname', $pdfname)
	 ->addFieldToFilter('product_id', $productid)
	 ->addFieldToFilter('pdf_id', array('neq' => $_delEditId));
	 
if($_nameChkData->count())
{
Mage::getSingleton('adminhtml/session')
->addError(Mage::helper('uploadpdf')->__('Record with same PDF name exist'));
}
else
{
$_uploadPdfData  = $_uploadPdfModel->getCollection()->addFieldToFilter('pdf_id', $_delEditId);
$currentPDFData  = $_uploadPdfData->getData();
$_pdfData  = $_uploadPdfModel->load($_delEditId);

if($_FILES['upload_pdf']['name'] == '')
{
$arrEditData['pdfname'] = $data['pdf_name'];
}
elseif(isset($mediaFile['name']) && ($mediaFile['name'] != ''))
{
 move_uploaded_file($pdftempfile,$pdfMediaDir.$front);	
 $imgPath = $pdfMediaDir.$front;
 $arrEditData    = array('pdf_file' => $mediaFile['name'],
				   'pdfname'        => $pdfname,  
				   'pdf_path'       => $pdfMediaLink,
				   'pdf_pathfile'   => $front);
$path = $_SERVER['DOCUMENT_ROOT'].'media/iverve/uploadpdf/'.$currentPDFData[0]['pdf_pathfile'];
unlink($path); 
}
if($mediaFile['type'] == 'application/pdf' || $mediaFile['type'] == 'application/zip' || $mediaFile['type'] == 'image/jpeg' || $mediaFile['type'] == 'image/png' || $mediaFile['type'] == 'application/msword' || $mediaFile['type'] == 'application/vnd.ms-excel' || $mediaFile['type'] == 'application/octet-stream'){
$model = Mage::getModel('uploadpdf/uploadpdf')->load($_delEditId)->addData($arrEditData);   
$model->setId($_delEditId)->save();  
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('uploadpdf')->__('PDF updated successfully.')); 
}else{
	Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('Only PDF ZIP RAR JPG PNG DOC XLS Files allowed'));
}
}
}
else
{
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('uploadpdf')->__('PDF name should not be blank'));
}
}
}								
}
catch (Exception $e){
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
}
public function deletePDF($pdfDelList)
{
try{		
$modelDelPdf = Mage::getModel('uploadpdf/uploadpdf')->getCollection()
->addFieldToFilter('pdf_id', array('in' => $pdfDelList))
->getData();
foreach($modelDelPdf as $checkdel){
try{
$_deletePdfModel = Mage::getModel('uploadpdf/uploadpdf');	 
$_deletePdfModel->setId($checkdel['pdf_id'])->delete();
$path = $_SERVER['DOCUMENT_ROOT'].'media/iverve/uploadpdf/'.$checkdel['pdf_pathfile'];
unlink($path);
}catch (Exception $e){
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());					
}  
}
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('uploadpdf')->__('PDF(s) deleted successfully.'));
}catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}

public function getProduct()
{
return Mage::registry('product');
}
protected function _getRequest()
{
return Mage::app()->getRequest();
}	
public function checkInstallation()
{		
$_iverveExtensionModel   = Mage::getModel('uploadpdf/extensioncheck');
$_iverveUploadPdfExtData = $_iverveExtensionModel->getCollection()
->addFieldToFilter('extension_name', 'iverve_uploadpdf')
->addFieldToFilter('value', '1');
if($_iverveUploadPdfExtData->count())
{
$body = "Extension <b>'Uploadpdf'</b> is installed to the following detail: <br/><br/> 
STORE NAME: ".Mage::getStoreConfig('general/store_information/name')."<br/>
STORE PHONE: ".Mage::getStoreConfig('general/store_information/phone')."<br/>
STORE ADDRESS: ".Mage::getStoreConfig('general/store_information/address')."<br/>
SECURE URL: ".Mage::getStoreConfig('web/secure/base_url')."<br/>
UNSECURE URL: ".Mage::getStoreConfig('web/unsecure/base_url')."<br/>
GENERAL EMAIL ADDRESS: ".Mage::getStoreConfig('trans_email/ident_general/email')."";
$mail = Mage::getModel('core/email');
$mail->setToName(self::$_setToName);
$mail->setToEmail(self::$_setToEmail);			
$mail->setBody($body);
$mail->setSubject('Uploadpdf Extension is installed!!!');
$mail->setFromEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
$mail->setFromName(Mage::getStoreConfig('trans_email/ident_general/name'));
$mail->setType('html');
try{
$mail->send();
$_iverveExtensionModelData = $_iverveExtensionModel->load('iverve_uploadpdf', 'extension_name');			
$_iverveExtensionModelData->setValue('0');
$_iverveExtensionModelData->save();
}
catch(Exception $e)
{	
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
} 

}
}
app\code\local\Iverve\Uploadpdf\Model\Uploadpdf.php
class Iverve_Uploadpdf_Model_Uploadpdf extends Mage_Core_Model_Abstract
{
	public function _construct()
	{
		parent::_construct();
		$this->_init('uploadpdf/uploadpdf');
	}   
	 public function getPdfDetails($_productId){
    	return Mage::getModel('uploadpdf/uploadpdf')->getCollection()->addFieldToFilter('product_id', $_productId);
    }
}
app\code\local\Iverve\Uploadpdf\Model\Mysql4
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck.php
class Iverve_Uploadpdf_Model_Mysql4_Extensioncheck extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('uploadpdf/extensioncheck', 'extension_id');
    }
}
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Uploadpdf.php
class Iverve_Uploadpdf_Model_Mysql4_Uploadpdf extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        $this->_init('uploadpdf/uploadpdf', 'pdf_id');
    }
}
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck\Collection.php
class Iverve_Uploadpdf_Model_Mysql4_Extensioncheck_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
	public function _construct()
	{
		parent::_construct();
		$this->_init('uploadpdf/extensioncheck');
	}	
}
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Uploadpdf\Collection.php
class Iverve_Uploadpdf_Model_Mysql4_Uploadpdf_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
	public function _construct()
	{
		parent::_construct();
		$this->_init('uploadpdf/uploadpdf');
	}	
}

下面是sql文件,用于自动添加对应的数据表,如果没有添加,自己按照代码来手动添加对应的表

app\code\local\Iverve\Uploadpdf\sql\uploadpdf_setup
app\code\local\Iverve\Uploadpdf\sql\uploadpdf_setup\mysql4-install-0.1.0.php
<?php 
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('uploadpdf/uploadpdf')};
   CREATE TABLE {$this->getTable('uploadpdf/uploadpdf')}(
	  `pdf_id` int(11) unsigned NOT NULL auto_increment,
	  `product_id` int(11) NULL,
	  `pdf_file` varchar(500) NULL,
	  `pdfname` varchar(500) NULL,
	  `pdf_path` varchar(500) NULL,
	  `pdf_pathfile` varchar(500) NULL,
	  PRIMARY KEY (`pdf_id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
if(!$installer->tableExists('uploadpdf/extensioncheck')){
	$installer->run("
	CREATE TABLE {$this->getTable('uploadpdf/extensioncheck')}(
	  `extension_id` int(11) unsigned NOT NULL auto_increment,
	  `extension_name` varchar(50) NULL,
	  `value` smallint(1) NOT NULL DEFAULT '0',
	  PRIMARY KEY (`extension_id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;
	");
}
$installer->run("INSERT INTO `{$installer->getTable('uploadpdf/extensioncheck')}` 
					    (`extension_name`, `value`)
				 VALUES 
				 		('iverve_uploadpdf','1');");
$installer->endSetup();

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论