Subversion Repositories SmartDukaan

Rev

Rev 14509 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Campaigns Controller
 *
 * @property Campaign $Campaign
 * @property PaginatorComponent $Paginator
 */
class CampaignsController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator','RequestHandler');

/**
 * index method
 *
 * @return void
 */
        public function index() {
                $country = 'IN';
                foreach (getallheaders() as $name => $value) {
                        if($name == "Cf-Ipcountry"){
                                //error_log("[XFWDF] [country]- $value");
                                //if($value != 'XX')
                                //      $country = $value;
                        }
                }
                $this->Campaign->recursive = 0;
                $options = array('conditions'=>array('status'=>'active','OR' => array(array('target_countries LIKE'=>'%'.$country.'%'),array('target_countries' => null))),'order'=>array('clicks'=>'asc'));
                $cachekey = "campaign-$country";
                $response = Cache::read($cachekey,'hour');
                if(empty($response)) {
                        $response = $this->Campaign->find('first',$options);
                        Cache::write($cachekey,$response,'hour');
                }
                if ($this->request->is('requested')) {
                    return $response;
                } else {
                    $this->set('campaign', $response);
                }       
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                $this->layout= 'ajax';
                if (!$this->Campaign->exists($id)) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                $options = array('conditions' => array('Campaign.id'=> $id));
                $campaign  = $this->Campaign->find('first', $options);
                $campaign['Campaign']['clicks'] = $campaign['Campaign']['clicks'] + 1;
                $this->Campaign->save($campaign);
                $url = $campaign['Campaign']['url'];
                $url = str_replace('INSERTPUBLISHERID','hotbhojpuri',$url);
                $url = str_replace('INSERTS2STRACKING',$campaign['Campaign']['id'],$url);
                $this->set('url',$url);
        }

/**
 * add method
 *
 * @return void
 */
        public function add() {
                throw new NotFoundException(__('Invalid campaign'));
                if ($this->request->is('post')) {
                        $this->Campaign->create();
                        if ($this->Campaign->save($this->request->data)) {
                                $this->Session->setFlash(__('The campaign has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
                        }
                }
        }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function edit($id = null) {
                throw new NotFoundException(__('Invalid campaign'));
                if (!$this->Campaign->exists($id)) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Campaign->save($this->request->data)) {
                                $this->Session->setFlash(__('The campaign has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
                        $this->request->data = $this->Campaign->find('first', $options);
                }
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function delete($id = null) {
                throw new NotFoundException(__('Invalid campaign'));
                $this->Campaign->id = $id;
                if (!$this->Campaign->exists()) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Campaign->delete()) {
                        $this->Session->setFlash(__('The campaign has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The campaign could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->Campaign->recursive = 0;
                $this->set('campaigns', $this->Paginator->paginate());
                $this->loadModel('Store');
                $this->set('stores',$this->Store->find('list'));
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->Campaign->exists($id)) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                $options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
                $this->set('campaign', $this->Campaign->find('first', $options));
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1));
                        $this->Campaign->create();
                        if ($this->Campaign->save($this->request->data)) {
                                $this->Session->setFlash(__('The campaign has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
                        }
                }
                $this->loadModel('Store');
                $this->set('stores',$this->Store->find('list'));
        }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                if (!$this->Campaign->exists($id)) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Campaign->save($this->request->data)) {
                                $this->Session->setFlash(__('The campaign has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
                        $this->request->data = $this->Campaign->find('first', $options);
                }
                $this->loadModel('Store');
                $this->set('stores',$this->Store->find('list'));
        }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->Campaign->id = $id;
                if (!$this->Campaign->exists()) {
                        throw new NotFoundException(__('Invalid campaign'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Campaign->delete()) {
                        $this->Session->setFlash(__('The campaign has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The campaign could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }
}

if (!function_exists('getallheaders')) 
{ 
    function getallheaders() 
    { 
           $headers = ''; 
       foreach ($_SERVER as $name => $value) 
       { 
           if (substr($name, 0, 5) == 'HTTP_') 
           { 
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
           } 
       } 
       return $headers; 
    } 
}