Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 182 lines (170 sloc) 7.065 kb
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of AdsController
*
* @author futura-creative
*/
class AdsController extends AppController {
var $name ="Ads";
var $helpers = array(
'Html',
'Session',
'Paginator'
);
public $paginate = array(
'limit' => 10,
'order' => 'Ad.id DESC'
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(array('index','refresh','search'));
}
public function index(){
$this->set('title_for_layout','ADDins-Homepage');
if($this->request->is("Ajax")){
$this->autoLayout = False;
}
$ads = $this->paginate('Ad');
$this->set('ads',$ads);
}
public function add(){
if($this->request->is("Ajax")){
$this->autoRender = False;
$this->autoLayout = False;
$image = $this->request->data['Ad']['image'];
$this->request->data['Ad']['user_id'] = $this->Auth->user('id');
$this->request->data['Ad']['city_id'] = $this->Auth->user('city');
$this->request->data['Ad']['state_id'] = $this->Auth->user('state');
$this->request->data['Ad']['issue_date'] = date('y-m-d');
$server_current_time = time();
$this->request->data['Ad']['issue_time'] = date("H:i:s",$server_current_time);
$this->request->data['Ad']['image'] = "";
$this->Ad->set($this->request->data);
if($this->Ad->validates()){
if($this->Ad->save()){
if($image['tmp_name']!=""){
$this->request->data['Ad']['image'] = $this->Ad->id.".jpg";
}
else{
$this->request->data['Ad']['image'] = 'default.jpg';
}
$filePath = WWW_ROOT.DS."files".DS.$this->request->data['Ad']['image'];
$this->Ad->updateAll(array('Ad.image'=>"'".$this->request->data['Ad']['image']."'"),array('Ad.id'=>$this->Ad->id));
if($image!=null)move_uploaded_file($image['tmp_name'],$filePath);
$this->set('ads',$this->Ad->findById($this->Ad->id));
$this->set('user',$this->Auth->user());
$this->render('add');
}
}
else{
echo "status";
$message = array("status"=>0,"message"=>$this->Ad->validationErrors);
echo json_encode($message);
}
}
}
public function refresh(){
if($this->request->is("Ajax")){
$this->autoLayout = False;
$server_current_time = time();
$current_time = date("H:i:s",$server_current_time);
$last_time = date("H:i:s",$server_current_time-(1*60));
$condition = array('And'=>array(
'issue_time >=' => $last_time,
'issue_time <=' => $current_time,
'issue_date =' => date('y-m-d'),
'Not' => array(
'User.id' => $this->Auth->user('id')
)
));
$this->set('ads',$this->Ad->find('all',array('conditions'=>$condition)));
}
}
public function search(){
$this->set("title_for_layout","ADDins - Cari Iklan");
if(isset($this->request->query['q']) || isset($this->params["named"]["page"])){
$conditions = array();
if($this->request->query["q"] != "")
$conditions["And"]["Ad.ads_title LIKE"] = "%".$this->request->query['q']."%";
if($this->request->query['k'] != 0)
$conditions["And"]["Ad.category_id ="] = $this->request->query['k'];
if($this->request->query['p'] != 0)
$conditions["And"]["Ad.state_id ="] = $this->request->query['p'];
if($this->request->query['t'] != ""){
$tgl = split("/",$this->request->query['t']);
$conditions["And"]["Ad.issue_date = "] = $tgl[2]."-".$tgl[0]."-".$tgl[1];
}
$this->paginate = array('limit'=>10,'conditions'=>$conditions);
$result = $this->paginate('Ad');
$this->set('ads',$result);
}
else{
$this->redirect(array("controller"=>'ads',"action"=>"index"));
}
}
public function delete(){
if($this->request->is("Ajax")){
$id = $this->request->data['id'];
$this->autoRender = False;
$this->autoLayout = False;
$ads = $this->Ad->findById($id);
if($ads['Ad']['image']!="default.jpg"){
$filePath = WWW_ROOT.DS."files".DS.$id.".jpg";
unlink($filePath);
}
if($this->Ad->delete($id)){
$message = array("status"=>1);
echo json_encode($message);
}
else{
$message = array("status"=>0);
echo json_encode($message);
}
}
}
public function edit(){
if($this->request->isAjax()){
$this->autoLayout = False;
$this->autoRender = False;
if(isset($this->request->data['Ad'])){
$data = array(
"Ad.ads_title"=>"'".$this->request->data['Ad']['ads_title']."'",
"Ad.category_id"=>$this->request->data['Ad']['category_id'],
"Ad.ads_body"=>"'".$this->request->data['Ad']['ads_body']."'"
);
$filePath = WWW_ROOT.DS."files".DS.$this->request->data['Ad']['id'].".jpg";
if($this->request->data['Ad']['image']['tmp_name']!=""){
if(file_exists($filePath)){
unlink($filePath);
}
move_uploaded_file($this->request->data['Ad']['image']['tmp_name'], $filePath);
$data["Ad.image"]="'".$this->request->data['Ad']['id'].".jpg'";
}
if($this->Ad->updateAll(
$data
,
array("Ad.id"=>$this->request->data['Ad']['id'])
)){
$message = array("status"=>1);
$ads = $this->Ad->findById($this->request->data['Ad']['id']);
$message['Ad'] = $ads['Ad'];
echo json_encode($message);
}
else{
$message =array("status"=>0);
echo json_encode($message);
}
}
else{
$ads = $this->Ad->findById($this->request->data['id']);
$this->set('ads',$ads);
$this->render('edit');
}
}
}
}
?>
Jump to Line
Something went wrong with that request. Please try again.