Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Chowly Pick. Eat. Save! | ||
* @copyright Copyright 2011, Martin Samson | ||
* @copyright Copyright 2011, Chowly Corporation | ||
*/ | ||
|
||
namespace chowly\controllers; | ||
|
||
use chowly\models\OfferTemplates; | ||
use li3_flash_message\extensions\storage\FlashMessage; | ||
|
||
class OfferTemplatesController extends \chowly\extensions\action\Controller{ | ||
public function admin_index(){ | ||
$limit = 20; | ||
$page = $this->request->page ?: 1; | ||
$order = array('created' => 'DESC'); | ||
|
||
$total = Offers::count(); | ||
$offers = Offers::all(compact('order','limit','page')); | ||
|
||
return compact('offers', 'total', 'page', 'limit'); | ||
} | ||
|
||
public function admin_view(){ | ||
if (!$this->request->id){ | ||
FlashMessage::set("Missing data."); | ||
return $this->redirect(array("OfferTemplates::index")); | ||
} | ||
|
||
$conditions['_id'] = $this->request->id; | ||
|
||
$template = OfferTemplates::first(compact('conditions')); | ||
if (!$template){ | ||
FlashMessage::set("The specified template does not exists."); | ||
return $this->redirect(array("OfferTemplates::index")); | ||
} | ||
return compact('template'); | ||
} | ||
|
||
public function admin_add(){ | ||
$template = OfferTemplates::create(); | ||
|
||
if ($this->request->data){ | ||
$template->set($this->request->data); | ||
if ($template->save()){ | ||
FlashMessage::set("Template created."); | ||
return $this->redirect(array('OfferTemplates::view', 'id' => $template->_id, 'admin'=>true)); | ||
} | ||
} | ||
|
||
$this->_render['template'] = 'admin_edit'; | ||
return compact('template'); | ||
} | ||
|
||
public function admin_edit(){ | ||
$conditions = array( | ||
'_id' => $this->request->id | ||
); | ||
|
||
$template = OfferTemplates::first(compact('conditions')); | ||
if ($this->request->data){ | ||
if ($template->save($this->request->data)){ | ||
FlashMessage::set("Template modified."); | ||
return $this->redirect(array('OfferTemplates::view', 'id' => $venue->_id)); | ||
} | ||
} | ||
return compact('templates'); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Chowly Pick. Eat. Save! | ||
* @copyright Copyright 2011, Martin Samson | ||
* @copyright Copyright 2011, Chowly Corporation | ||
*/ | ||
|
||
namespace chowly\models; | ||
|
||
class OfferTemplates extends \chowly\extensions\data\Model{ | ||
protected static $_states = array('published'=>'published', 'unpublished'=>'unpublished'); | ||
public $validates = array( | ||
'name' => array( | ||
array('notEmpty','message'=>'Please enter a name'), | ||
array('lengthBetween', 'min'=>1,'max'=>255, | ||
'message' => 'Please enter a name that is between 1 and 255') | ||
), | ||
'cost' => array( | ||
array('numeric','message'=>'Must be a monetary amount (ex: 33.00)') | ||
) | ||
); | ||
|
||
protected $_schema = array( | ||
'_id' => array('type' => 'id'), | ||
'name' => array('type' => 'string','null'=>false), // Name of the coupon | ||
'description'=>array('type'=>'string'), // Description (if any) of the coupon | ||
'limitations'=>array('type'=>'string'), // Limitations regarding usage of the coupon | ||
'created'=>array('type'=>'date'), | ||
'updated'=>array('type'=>'date') | ||
); | ||
|
||
public function getErrors(){ | ||
return $this->_errors; | ||
} | ||
|
||
public function save($entity, $data = null, array $options = array()) { | ||
$files = array(); | ||
if (isset($data['image'])){ | ||
$files['image'] = $data['image']; | ||
unset($data['image']); | ||
} | ||
|
||
if (!$entity->_id){ | ||
$entity->_id = new \MongoId(); | ||
} | ||
|
||
$this->_errors = array(); | ||
foreach ($files as $key => $file){ | ||
if (!$file['tmp_name'] || empty($file['tmp_name'])){ | ||
continue; | ||
} | ||
|
||
$image = Images::create(); | ||
$imageData = array('file'=> $file, 'parent_id'=> $entity->_id, 'parent_type'=>'offer'); | ||
if ($image->save($imageData)){ | ||
$data[$key] = $image->_id; | ||
}else{ | ||
$this->_errors[] = "Image {$key} could not be saved."; | ||
} | ||
} | ||
return parent::save($entity, $data, $options); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters