Skip to content

Instantly share code, notes, and snippets.

@jimmylatreille
Last active August 29, 2015 13:55
Show Gist options
  • Save jimmylatreille/8717634 to your computer and use it in GitHub Desktop.
Save jimmylatreille/8717634 to your computer and use it in GitHub Desktop.
multy uplaod Magento demo
<?php
/**
* Controler PhotoGallery
* @author Jimmy Latreille
* @email jlatreille@os.ca
* @date 3 septembre 2013
*/
class Ckx_PhotoGallery_IndexController extends Mage_Core_Controller_Front_Action{
//methode pour afficher le formulaire de galerie photo
public function indexAction(){
$this->loadLayout(); //Va chercher les elements à afficher
$this->renderLayout(); //Affiche les elements
}
//methode pour sauvegarder les info dans la base de donner et faire la validation
public function saveFormAction(){
$data = $this->getRequest()->getPost();
if(isset($data) && $_POST['boot'] == 1){
$folder = Mage::getBaseDir('media') . DS . 'photoGallery' . DS . 'filename_original' . DS;
if(!file_exists($folder)){
mkdir($folder, 0777);
}
$max_file_size = 2097152;
$validExtensions = array('png','jpg', 'jpeg');
$nbElem = count($_FILES['uploadfile']['type']);
$date = new DateTime();
$helper = Mage::helper('photogallery');
$personalInfo = array('fullName'=>trim($data['fullName']),'email'=>trim($data['emailPhoto']),'ip'=>$helper->getRealIpAddr());
$errorExtensions = [];
$errorSize = [];
$elemValid = [];
$uploadSuccess = [];
$uploadError = [];
try{
//le type de fichier dimage
foreach ($_FILES['uploadfile']['name'] as $key => $fileType) {
//recupere lextension du fichier
$fileExtensions = end(explode('.', $fileType));
//valide les extention de chaque fichier uploader
if(!in_array($fileExtensions, $validExtensions)){
$errorExtensions[] = $fileExtensions;
unset($_FILES['uploadfile']['name'][$key]);
unset($_FILES['uploadfile']['tmp_name'][$key]);
}else{
//valide la taille de chaque fichier uploader
if(filesize($_FILES['uploadfile']['tmp_name'][$key]) < $max_file_size){
$elemValid[] = 1;
//renomme le fichier valider
$_FILES['uploadfile']['name'][$key] = "photoGallery_".$key.''.$date->getTimestamp().'.'.$fileExtensions;
//upload dans le bon dossier tous les bon fichier uploader
if(move_uploaded_file($_FILES['uploadfile']['tmp_name'][$key], $folder . $_FILES['uploadfile']['name'][$key])){
//met le nom du fichier uploder, le titre et la description associer dans le tableau $uploadsuccess
$uploadSuccess[] = array($_FILES['uploadfile']['name'][$key],
trim($data['photoTitle'][$key]),
trim($data['photoDescription'][$key])
);
}else{
//si le upload na pas fonctionner
$uploadError[] = 1;
}
}else{
//si le fichier dimage exced la taille maximum
$errorSize[] = filesize($_FILES['uploadfile']['tmp_name'][$key]);
}
}
}
//condition qui redirige le client si le nombre dextension non valid et egal aux nombre total de fichier
if(count($errorExtensions) == $nbElem){
$msg = $this->__('You must enter a valid image file (png, jpg, jpeg)');
Mage::getSingleton('core/session')->addError($msg);
$this->_redirect('*/*');
//condition qui redirige le client si le nombre derreur est egale au nombre de fichier dimage
}elseif(count($errorSize) == count($_FILES['uploadfile']['name'])){
$msg = $this->__('The size of your files has exceeded the allowable limit of 2Mb');
Mage::getSingleton('core/session')->addError($msg);
$this->_redirect('*/*');
//conditon qui redirige le client si le nombre derreur dupload est egale au nombre de fichier valider
}elseif(count($uploadError) == count($elemValid)){
$msg = $this->__('We had some difficulties to upload your files');
Mage::getSingleton('core/session')->addError($msg);
$this->_redirect('*/*');
//condition si les fichiers ont ete uploader correctement
}elseif(count($uploadSuccess) == count($elemValid)){
if(count($uploadSuccess) == 1){
$msg = count($uploadSuccess)." ".$this->__('image was sent with success');
}else{
$msg = count($uploadSuccess)." ".$this->__('pictures were sent with success');
}
if($errorExtensions){
if(count($errorExtensions) == 1){
$msg .= ' '.$this->__('but').' '.count($errorExtensions).' '.$this->__('file').' '.implode(",", $errorExtensions).' '.$this->__('has not been sent');
}else{
$msg .= ' '.$this->__('but').' '.count($errorExtensions).' '.$this->__('files').' '.implode(",", $errorExtensions).' '.$this->__('were not sent');
}
}
if($errorSize){
if(count($errorSize) == 1){
$msg .= ' '.count($errorSize).' '.$this->__('exceeds the maximum size of 2Mb');
}else{
$msg .= ' '.$this->__('and').' '.count($errorSize).' '.$this->__('exceed the maximum size of 2Mb');
}
}
$msg .= "<br />".$this->__("Your photo has been successfully delivered. Please note that not all photos will be displayed on the website. Thanks!");
//insertion dans la base de donnee
$successQuery = $helper->insertPhoto($uploadSuccess, $personalInfo);
if(count($successQuery) == count($elemValid)){
$sender = $this->__('senderEmail');
$contactName = $this->__('mailContactName');
$subject = $this->__('subject');
$successMsg = self::emailTemplate($personalInfo['fullName'], $this->__('successMsg'), $this->__('terms1'), $this->__('terms2'), $this->__('terms3'));
$emailPhoto = $personalInfo['email'];
$fullName = $personalInfo['fullName'];
self::senderEmailPhoto($sender, $contactName, $subject, $successMsg, $emailPhoto, $fullName, $uploadSuccess);
//affiche le message de success
Mage::getSingleton('core/session')->addSuccess($msg);
$this->_redirect('*/*');
}
}
}catch (Exception $e){
Mage::getSingleton('core/session')->addError($e->getMessage());
$this->_redirect('*/*');
}
}else{
$msg = $this->__('There was a problem during recovery form');
Mage::getSingleton('core/session')->addError($msg);
$this->_redirect('*/*');
}
}//fin de saveFormAction()
public function senderEmailPhoto($sender, $contactName, $subject, $successMsg, $emailPhoto, $fullName, $images){
$email = Mage::getModel('core/email_template');
$email->setSenderEmail($sender);
$email->setSenderName($contactName);
$email->setTemplateSubject($subject);
$email->setTemplateText($successMsg);
$email->send($emailPhoto, $fullName);
// message envoyer a l'equipe CKX
$successMsgCKX = self::emailTemplateCKX($fullName, $images);
$email = Mage::getModel('core/email_template');
$email->setSenderEmail($sender);
$email->setSenderName($contactName);
$email->setTemplateSubject($subject);
$email->setTemplateText($successMsgCKX);
$email->send($sender, $contactName);
}
public function emailTemplate($fullName, $successMsg, $terms1, $terms2, $terms3){
$langEmail = (Mage::app()->getStore()->getStoreId() == 1)?"fr":"en";
$lang = strtolower(Mage::app()->getLocale()->getLocaleCode());
if(preg_match('/en_us/', $lang)){
$lang = preg_replace('/en_us/', 'usa', $lang);
}elseif(preg_match('/\_/', $lang)) {
$lang = preg_replace('/\_/', '-', $lang);
}
$url = 'http';
if ($_SERVER["HTTPS"] == "on") {$url .= "s";}
$url .= "://".$_SERVER["SERVER_NAME"]."/";
$html = "<!DOCTYPE html><html><head>".
"<title>email CKX</title>".
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />".
"<style type='text/css'> *{font-weight: 300; font-family: Helvetica;color: #555;}".
"a{ color: #4061f9;}".
"td { padding: 10px 0px;}".
".terms { font-size: 80%;}".
".separation { padding-bottom: 50px; border-bottom: 1px #aaa solid;}".
".social td { float: left; margin-right: 3px;} </style></head>".
"<body style='background-color: #f1f1f1;'>".
".<table cellspacing='0' cellpadding='0' border='0' width='100%'>".
"<tr><td align='center'>".
"<table cellspacing='0' cellpadding='0' border='0' width='690'>".
"<tr><td>".$this->__('inviteTitle').' '.$fullName."</td></tr>".
"<tr><td>".$successMsg."</td></tr>".
"<tr><td class='separation'>".$this->__('tanksMsg2')."</td></tr>".
"<tr><td>".$this->__('tanksMsg3')." <a href='mailto:".$this->__('policyEmail')."'>".$this->__('policyEmail')."</a>".
"<table style='position: relative;' width='228' style='width : 228px;'>".
"<tr><td colspan='7'><img src='".$url."media/email/logo_".$langEmail.".png' title='logo CKX' alt='logo CKX' /></td></tr>".
"<tr class='social' style='position: absolute; top: 105px; right: 0px;'>".
"<td ></td>".
"<td><a href='https://www.facebook.com/CKXgear' target='_blank'><img src='".$url."media/email/fb.png' title='logo facebook' alt='logo facebook' /></a></td>".
"<td><a href='https://twitter.com/CKXGear' target='_blank'><img src='".$url."media/email/tw.png' title='logo Twitter' alt='logo Twitter' /></a></td>".
"<td><a href='http://pinterest.com/ckxgear/' target='_blank'><img src='".$url."media/email/pt.png' title='logo Pinterest' alt='logo Pinterest' /></a></td>".
"<td><a href='".$url.''.$lang."/rss'><img src='".$url."media/email/rs.png' title='logo RSS' alt='logo RSS' /></a></td>".
"<td><a href='http://www.linkedin.com/company/kimpex-inc./ckx-gear-368067/product' target='_blank'><img src='".$url."media/email/In.png' title='logo Linkedin' alt='logo Linkedin' /></a></td>".
"<td><a href='http://www.youtube.com/user/ckxgear' target='_blank'><img src='".$url."media/email/yt.png' title='logo Youtube' alt='logo Youtube' /></a></td>".
"</tr>".
"</table>".
"</td></tr>".
"<tr><td><span class='terms'>".$terms1." <a href='mailto:".$terms2."'>".$terms2."</a> ".$terms3."</sapn></td></tr>".
"<tr><td>".
"<h4 style='margin-bottom: 10px;'>".$this->__('policyTitle')."</h4>".
"<span class='terms'>".
"".$this->__('policyText')." <a href='mailto:".$this->__('policyEmail')."'>".$this->__('policyEmail')."</a>".
"</sapn></td></tr>".
"</table>".
"</td></tr>".
"</table></body></html>";
return $html;
}//EmailTemplate()
// email envoyer a l'equipe CKX
public function emailTemplateCKX($fullName, $images){
$url = 'http';
if ($_SERVER["HTTPS"] == "on") {$url .= "s";}
$url .= "://".$_SERVER["SERVER_NAME"]."/";
$html = "<!DOCTYPE html><html><head>".
"<title>CKX</title>".
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />".
"<style type='text/css'> *{font-weight: 300; font-family: Helvetica;color: #555;}".
"a{ color: #4061f9;}".
"td { padding: 10px 0px;}".
".terms { font-size: 80%;}".
".separation { padding-bottom: 50px; border-bottom: 1px #aaa solid;}".
".social td { float: left; margin-right: 3px;} </style></head>".
"<body style='background-color: #f1f1f1;'>".
".<table cellspacing='0' cellpadding='0' border='0' width='100%'>".
"<tr><td align='center'>".
"<table cellspacing='0' cellpadding='0' border='0' width='690'>".
"<tr><td>Bonjour</td></tr>".
"<tr><td>".$fullName." vous a envoyé une photo concernant la section galerie photo dans la partie photo gallery filename_original de l'admin du site de CKX</td></tr>";
foreach ($images as $key => $value) {
$html .= "<tr><td>".
"<span>Nom de l'image ".$images[$key][0]."</span>".
"<img src='".Mage::getBaseUrl('media').'photoGallery/filename_original/'.$images[$key][0]."' alt='".$images[$key][0]."' title='".$images[$key][0]."' />".
"</tr></td>";
}
$html .= "<tr><td>".
"<table style='position: relative;' width='228' style='width : 228px;'>".
"<tr><td colspan='7'><img src='".$url."media/email/logo_fr.png' title='logo CKX' alt='logo CKX' /></td></tr>".
"<tr class='social' style='position: absolute; top: 105px; right: 0px;'>".
"<td ></td>".
"<td><a href='https://www.facebook.com/CKXgear' target='_blank'><img src='".$url."media/email/fb.png' title='logo facebook' alt='logo facebook' /></a></td>".
"<td><a href='https://twitter.com/CKXGear' target='_blank'><img src='".$url."media/email/tw.png' title='logo Twitter' alt='logo Twitter' /></a></td>".
"<td><a href='http://pinterest.com/ckxgear/' target='_blank'><img src='".$url."media/email/pt.png' title='logo Pinterest' alt='logo Pinterest' /></a></td>".
"<td><a href='".$url."fr-ca/rss'><img src='".$url."media/email/rs.png' title='logo RSS' alt='logo RSS' /></a></td>".
"<td><a href='http://www.linkedin.com/company/kimpex-inc./ckx-gear-368067/product' target='_blank'><img src='".$url."media/email/In.png' title='logo Linkedin' alt='logo Linkedin' /></a></td>".
"<td><a href='http://www.youtube.com/user/ckxgear' target='_blank'><img src='".$url."media/email/yt.png' title='logo Youtube' alt='logo Youtube' /></a></td>".
"</tr>".
"</table>".
"</td></tr>".
"<tr><td>".
"<h4 style='margin-bottom: 10px;'>Avis de confidentialité</h4>".
"<span class='terms'>".
"Ce courriel est une communication confidentielle et l'information qu'il contient ".
"est réservée à l'usage exclusif du destinataire. Si vous n'êtes pas le destinataire ".
"visé, vous n'avez aucun droit d'utiliser cette information, de la copier, de la distribuer. ".
"Si cette communication vous a été transmise par erreur, veuillez la détruire et nous aviser ".
"immédiatement par courriel: <a href='mailto:".$this->__('policyEmail')."'>".$this->__('policyEmail')."</a>".
"</sapn></td></tr>".
"</table>".
"</td></tr>".
"</table></body></html>";
return $html;
}//emailTemplateCKX()
} //fin de la class Ckx_PhotoGallery_IndexController()
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment