<?php


require ("config.php");
require_once($mainpath."htdocs/classes/newsletter.class.php");

//include the js file for the popup
$includeJsArr = array("common", "lib/jquery/jquery.scrollbar","tinymce/tinymce.min","application","jquery.datetimepicker");
$smarty->assign('includeJsArr', $includeJsArr);
//include the css file for the tab option
$includeCssArr = array("tabs/adminTab","popup","jquery.datetimepicker");
$smarty->assign('includeCssArr', $includeCssArr);

//Check Admin is login or not
if (isset($_SESSION["id_users"]) ) {
  $smarty->assign('userstat', 'out');
  $smarty->assign('loggedin', 1);
}
else {
  $smarty->assign('userstat', 'in');
  $smarty->assign('loggedin', 0);
  header("Location:".$hosturl.'adminlogin');
}
//ends

//creating a object for the Newsletter class
$news = new Newsletter();
//functional flow starts here
$action = $_POST['action'];
switch($action)
    {
        case "addnewslettertemplate":
                /*is used to validate and add a newsletter template*/    
                $msg = $news->validAddNewsletterTemplate($_POST);
                $smarty->assign('errors',$msg);                
                break;
        case "deletesubscrib":
                /*common function for delete*/
                $table='newsletter_subscribers';
                $cond='id';
                $condval=$_POST['deleteid'];
                $news->commonDeleteFunction($table,$cond,$condval);
                header("Location:".$hosturl.'newsletter.php?sourceto=deleted');
                break;
        case "deletetemplate":
                /*common function for delete*/
                $table='newsletter';
                $cond='id_newsletter';
                $condval=$_POST['deleteid'];
                $news->commonDeleteFunction($table,$cond,$condval);
                header("Location:".$hosturl.'newsletter');
                break;
        case "rejectschedule":
                $table='newsletter_details';
                $cond='id';
                $condval=$_POST['deleteid'];
                $news->commonDeleteFunction($table,$cond,$condval);
                header("Location:".$hosturl.'newsletter');
                break;                
        case "exportall":
                /*is used to export the subscriber list*/
                $news->exportSubscribersRecord();
                break;
        case "importall":
                /*is used to import the subscriber list*/
                $msg = $news->InsertImportSubscribers($_POST);
                if(is_numeric($msg))
                    $smarty->assign('sucess_file',$msg.' records added sucessfully');
                else
                    $smarty->assign('errors_file',$msg);
                break;
        case "sendmessage":
                /*is used to send the newsletter details to the subscribers*/
                //$msg = $news->storeSendingAndSchedulingNewsletter($_POST);//its used to send the mail by send now, now we hide this ,
                $msg = $news->storeNewsletterDetails($_POST);
                header("Location:".$hosturl.'newsletter.php?sendmsg='.$msg);
                break;
    }
//sucess message for add a new template and update template
if(isset($_GET['msg']))
    {
        if($_GET['msg']=='success'){
            $smarty->assign('success','Template added successfully');   
        }
        elseif($_GET['msg']=='updatesuccess'){
            $smarty->assign('success','Template update successfully');   
        }        
    }
    
if(isset($_GET['sendmsg']))
    {
        if($_GET['sendmsg'] == 'sendsuccess'){
            $smarty->assign('success','Newsletter Sent Successfully.');   
        }
        elseif($_GET['sendmsg'] =='schedule'){
            $smarty->assign('success','Newsletter Scheduled Successfully.');   
        }
        elseif($_GET['sendmsg'] == 'noone'){
            $smarty->assign('comp_error','Please add subscriber(s) before you can send a newsletter.');   
        }
        elseif($_GET['sendmsg'] == 'Mailcannotsend'){
            $smarty->assign('comp_error','No newsletter was sent because there are no new clippings.');   //sorry mail cannot send
        }
        elseif($_GET['sendmsg'] == 'NoSubscriber'){
            $smarty->assign('comp_error','Newsletter cannot be sent because the subscribers list is empty.');   //all subsscribers receive a mail already on today 
        }
        
    }
if(isset($_GET['sourceto']) && ($_GET['sourceto'] == 'added'))
    {
      $smarty->assign('sucess_file','Subscribers added successfully');   
    }
elseif(isset($_GET['sourceto']) && ($_GET['sourceto'] == 'updated'))
    {
      $smarty->assign('sucess_file','Subscribers updated successfully');   
    }
elseif(isset($_GET['sourceto']) && ($_GET['sourceto'] == 'deleted'))
    {
      $smarty->assign('sucess_file','Subscribers deleted successfully');   
    }    
    
if(isset($_POST['loadcontent'])){
        $details = $news->toListAllMailTemplates($cond=true,$_POST['temp_id']);        
        echo "sucess|@|".html_entity_decode($details['template_design']);
        exit();
    }   
elseif(isset($_GET['id'])) 
    {
        //is used to edit the newsletter templates
        if( isset($_POST['editTemplate']) && !empty($_POST['template_title']) )
        {
            $table='newsletter';
            $getValue='template_title';
            $cond='template_title';
            $cond_value=$_POST['template_title'];
            
            $val = 'id_newsletter';
            $condval = $_GET['id'];
            if($_POST['template_title'] == $news->commonFunctionTogetSingleValue($table,$getValue,$val,$condval)){
                    $news->updateTemplateDetails($_GET['id']);
                    header("Location:".$hosturl.'newsletter.php?msg=updatesuccess');
                }
            elseif(!$news->chkIsAlreadyExists($table,$getValue,$cond,$cond_value)){
                $news->updateTemplateDetails($_GET['id']);
                header("Location:".$hosturl.'newsletter.php?msg=updatesuccess');
                }
            else{
                    $smarty->assign('errors','Template Name Already Exists');
                    $smarty->assign('templatedetails',$news->toListAllMailTemplates($cond=true,$_GET['id']));
                    $smarty->display('newsletteredit.tpl');    
            }
        }
        else{
            
            $smarty->assign('templatedetails',$news->toListAllMailTemplates($cond=true,$_GET['id']));
            $smarty->display('newsletteredit.tpl');    
        }
    }
elseif(isset($_GET['edit_id']))
    {
        $value = $_POST;
        if(isset($_POST['editsub']) && !empty($value['name']) && !empty($value['email'])){
            /*is used to validate the record before update the subscribers*/
                  if(filter_var($value['email'], FILTER_VALIDATE_EMAIL))
                  {
                     if(!empty($_POST['template_title']))
                        {
                            $column ='name';
                            $column1 ='email';
                            $table ='newsletter_subscribers';
                            $cond ='id';
                            $condval = $_GET['edit_id'];
                            
                            $getValue ='email';
                            $condname ='email';
                            $condname_value =$value['email'];
                            
                            $details = $news->getSubscriberDetails($_GET['edit_id']);
                            if($details['email'] == $value['email'])
                              {
                                $news->updateSubscribers($value);
                                header("Location:".$hosturl.'newsletter.php?sourceto=updated'); 
                              }
                            elseif($details['email'] != $value['email'])
                              {
                                if(!$news->chkIsAlreadyExists($table,$getValue,$condname,$condname_value))
                                  {
                                    $news->updateSubscribers($value);
                                    header("Location:".$hosturl.'newsletter.php?sourceto=updated');
                                  }
                                else
                                  {
                                    $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                                    $smarty->assign('errors_edit',"Email Id already exists");
                                    $details = $news->getSubscriberDetails($_GET['edit_id']);
                                    $smarty->assign('details',$details);
                                    $smarty->display('editsubscribers.tpl');
                                  }
                              }
                        }
                      else
                        {
                            $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                            $smarty->assign('errors_tempedit',"Please select a template");
                            $details = $news->getSubscriberDetails($_GET['edit_id']);
                            $smarty->assign('details',$details);
                            $smarty->display('editsubscribers.tpl');    
                        }
                  }
                  else{
                      $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                      $smarty->assign('errors_edit',"Please enter the valid email id");
                      $details = $news->getSubscriberDetails($_GET['edit_id']);
                      $smarty->assign('details',$details);
                      $smarty->display('editsubscribers.tpl');    
                  }
              }
            else
            {
                // is used to get the subscriber details for edit
                $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                $details = $news->getSubscriberDetails($_GET['edit_id']);
                $smarty->assign('details',$details);
                $smarty->display('editsubscribers.tpl');    
            }
    }
elseif($_GET['action'] == 'addsubscribe')
    {
      //ini_set('display_errors', '1');
      //error_reporting(E_ALL);
      
      /*is used to valid and add a new subscribers */      
      $value = $_POST;
     if(isset($_POST['addnewsub']) && !empty($value['name']) && !empty($value['email']))
     {
            if(filter_var($value['email'], FILTER_VALIDATE_EMAIL))
            {
                if(!empty($_POST['template_title']))
                  {
                      $table ='newsletter_subscribers';
                      $getValue ='email';
                      $condname ='email';
                      $condname_value =$value['email'];
                      if(!$news->chkIsAlreadyExists($table,$getValue,$condname,$condname_value)){
                              $news->insertSubscriber();
                              header("Location:".$hosturl.'newsletter.php?sourceto=added');
                      }
                      else{
                          $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                          $smarty->assign('errors_add',"Email Id already exists");    
                          $smarty->display('editsubscribers.tpl');
                      } 
                  }
                else
                  {
                    $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                    $smarty->assign('errors_temp',"Please select a template");    
                    $smarty->display('editsubscribers.tpl');
                  }
                
            }
            else{
                $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
                $smarty->assign('errors_add',"Please enter the valid email id");
                $smarty->display('editsubscribers.tpl');
            }
    }
    else{
        $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));//here i assign a newsletter template for the add subscriberpage
        $smarty->display('editsubscribers.tpl');
    }
    
}    
else
    {
        $smarty->assign('subscriberslist',$news->toListTheSubscribers($cond='false'));
        $smarty->assign('schedulenews',$news->showAllScheduledNewsletter());
        $smarty->assign('subscriberswithactive',$news->toListTheSubscribers($cond='true'));//hide for list the subscriber from user table ie)this from newsletter subscribers table        
        $smarty->assign('template_list',$news->toListAllMailTemplates($cond=false,$id=false));
        $smarty->display('newsletter.tpl');
    }
?>