Hi guys;
I have been struggling with this. I really appreciate some help. I don’t know how to learn this one.
I understand that a class should be responsible for doing only 1 thing. That is a simple concept. But practising that is not that simple.
For example I have a class that sends newsletters out and it is a big big big big mess.
I tried to break it down to different components but I dont know what is the right approach. I appreciate if someone puts me in the right direction.
I know model should be separate from view and controller but how to apply it to this one I am not sure.
Maybe place 1 or 2 of the methods in a another class? Or another location? I am not sure. Here is the code anyways.
Thanks for your help.
<?php
class SendCampaign extends BaseController {
public function __construct(){
$this->beforeFilter('login_check');
}
public function send($id){
//Set data
$data['name'] = $name = ucfirst(Input::get('name'));
$data['email'] = $email = Input::get('email');
$data['receiver'] = Input::get('email');
$time_sent = time();
//Validate posted data
$validator = Validator::make( array('email' =>$email ), array('email' => array('required', 'email')));
if ($validator->fails()){
Session::put('campaign_sent', '<p class="text-error">Invalid email address</p>');
return Redirect::to('email');
}
//Load campaign
$results = Campaign::find($id);
$view_file = $results->view_file;
//Attach deals
if(is_array(Input::get('deals'))){
$in = "(";
foreach(Input::get('deals') as $val){
$in .= $val.",";
}
$in = substr($in, 0, -1);
$in .= ")";
echo $in;
$data['deals']="";
$deals = DB::select('SELECT gomSite_deals.id AS DID,
gomSite_deals.title AS DT,
gomSite_deals.status AS DSTAT,
gomSite_deals.original_value AS OV,
gomSite_deals.end_date AS end_date,
gomSite_deals.deal_value AS DV
FROM gomSite_deals INNER JOIN
gomSite_business ON gomSite_business.id = gomSite_deals.business_id
WHERE gomSite_deals.id IN '.$in.' GROUP BY gomSite_deals.id
');
$data['deals'] .= "<table><tr>";
foreach($deals as $val =>$row){
$seconds_left = strtotime($row->end_date) - time();
$days_left = floor($seconds_left/(3600*24));
if($days_left<0){
$days_left = 0;
}
$discount = round(($row->DV*100)/$row->OV);
$discount = 100 - $discount;
$data['deals'] .= "<td>
<table style = \\"float:left; width:350px;\\">
<tr><td colspan = \\"2\\"><a href = \\"http://www.gomSite.com.au/details/show/".$row->DID."\\"><img src = 'http://gomSite.com.au/uploads/".$row->DID.".jpg' width=\\"250px\\" height = \\"250px\\" style = \\"-moz-border-radius: 10px; border-radius: 10px;\\"></a></td></tr>
<tr><td colspan = \\"2\\"><a style = \\"; text-decoration:none;\\" href = \\"http://www.gomSite.com.au/details/show/".$row->DID."\\"><strong style = \\"font-family:Arial; font-size:16px; text-decoration:none;\\">".$row->DT."</strong></a></td></tr>
<tr>
<td colspan = \\"2\\" style=\\"border-bottom:1px solid #cccccc\\"><br /><strong style = \\"color: #ED2930; margin:0; padding:0;
font-family: Arial,Helvetica,sans-serif;
font-size: 16px;
font-weight: bold;\\">$".$row->DV."
</strong>
<strong style = \\"color: #202020; margin:0; padding:0; text-decoration:line-through;
font-family: Arial,Helvetica,sans-serif;
font-size: 16px;
font-weight: bold;\\">$".$row->OV."
</strong><br /><br />
</td>
</tr>
<tr>
<td><br />
<span style = \\"color: #444444; margin:0; padding:0;
font-family: Arial,Helvetica,sans-serif;
font-size: 14px;\\"><img src = \\"http://gomSite.com.au/images/clock-image.png\\"> ".$days_left." days Left
</span>
</td>
<td align = \\"right;\\">
<span style = \\"color: #444444;
font-family: Arial,Helvetica,sans-serif;
font-size: 14px;
\\"><img src = \\"http://gomSite.com.au/images/voucher-image.png\\"> ".$discount."% discount
</span>
</td>
</tr>
</table>
</td>";
}
$data['deals'] .= "</tr></table>";
//exit();
} else {
$data['deals'] = "";
}
//echo $data['deals'];exit();
//Load sende'r signature details signature
$sender_id = Session::get('id');
$sender_details = Admin::find($sender_id);
$data['sender_name'] = ucwords($sender_details->name);
$data['sender_email'] = $sender_details->email;
$data['position'] = $sender_details->position;
$data['mobile'] = $sender_details->mobile;
$data['phone'] = $sender_details->phone;
//Check if view exists
if (!View::exists($view_file)){
Session::put('campaign_sent', '<p class="text-error">The email template you are trying to send does not exists!</p>');
return Redirect::to('email');
}
//Send email
Mail::send($view_file, $data,
function($message){
$message->to(Input::get('email'), Input::get('name'));
$message->from('support@gomSite.com', 'gomSite Support');
$message->cc('gomSiteAdmin@yahoo.com', 'Behnam');
$message->subject(Input::get('email_title'));
}
);
//Save report gomSite_campaign_sent table
$CampaignReport = new CampaignReport;
$CampaignReport->name = $name;
$CampaignReport->email = $email;
$CampaignReport->campaign_id = $id;
$CampaignReport->time_sent = $time_sent;
$CampaignReport->save();
//If member already not in this mailing list
$results = DB::select('select * from gomSite_individual_campaign_signup where email = ?', array($email));
if(count($results)==0){
//Save new member
$CampaignSignup = new CampaignSignup;
$CampaignSignup->name = $name;
$CampaignSignup->email = $email;
$CampaignSignup->campaign_id = $id;
$CampaignSignup->time_sent = $time_sent;
$CampaignSignup->save();
}
//If member already not exists
$results = DB::select('select * from gomSite_mailinglist_member where email = ? AND mailing_list_id = ?', array(Input::get('email'), Input::get('mailinglist_id')));
if(count($results)==0){
//Sign this user to gomSite_IC_MLG table //
$ICSML = new ICSML;
$ICSML->mailing_list_id = Input::get('mailinglist_id');
$ICSML->email = Input::get('email');
$ICSML->save();
}
//Redirect
Session::put('campaign_sent', '<p class="text-error">Campaign sent successfully</p>');
return Redirect::to('email');
}
}
Thanks