CRUD CAKE PHP 3.x
CREATE TABLE `homepage_slides` (
`id` int(11) NOT NULL,
`url` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
src/Model/Table
SlideTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Rule\IsUnique;
use Cake\ORM\TableRegistry;
use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\ORM\RulesChecker;
use Cake\Validation\Validator;
class SlidesTable extends AppTable {
public function initialize(array $config)
{
$this->setTable('homepage_slides');
}
//var $validate = array ('name' => 'notEmpty');
}
?>
src/Model/Entity
Slides.php
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Slides extends Entity {
protected $_accessible = [
'*' => true,
'id' => false,
];
}
?>
src/Controller
SlidesController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Event\Event;
use Cake\Utility\Hash;
use Cake\Datasource\ConnectionManager;
use Cake\Controller\Component\FlashComponent;
use Cake\Utility\Security;
use Cake\Network\Request;
use Cake\View\Helper;
class SlidesController extends AppController {
var $components = array(
'Redirection',
'Paginator',
'BookingComp',
'Email',
'ical',
// 'Mage'
);
// var $helpers = array('Paginator', 'Calendar', 'Javascript','DateHelper');
var $helpers = array('Paginator', 'Calendar', 'Date');
public function initialize()
{
parent::initialize();
$this->loadModel('Slides');
$this->loadComponent('Session');
$this->loadComponent('Flash'); // Include the FlashComponent
$this->Session = $this->request->session();
$connection = ConnectionManager::get('default');
}
public function beforeFilter(Event $event) {
//$this->loadModel('Users');
parent::beforeFilter($event);
if(Configure::read('Auth.disable'))
$this->Auth->allow('admin_index','admin_edit','admin-edit');
$this->adminallow('admin_index','admin_edit','admin-edit');
$this->adminandmanagerallow('index','edit','admin_index');
}
function adminIndex() {
// Configure::write('debug',2);
$slides = $this->Slides->find()->toArray();
$this->set('slides', $slides);
}
function edit($slide_id = null) {
$connection = ConnectionManager::get('default');
//Configure::write('debug',2);
if(!empty($this->request->getData())){
$data = $this->request->getData();
$homepage_slides = array();
if (!empty($data['url']) && is_array($data['url'])) {
// Check if we should do a file upload
if (empty($data['url']['tmp_name'])) {
if (!empty($slide_id)) {
// This is an edit, so we don't need an image
unset($data['url']);
} else {
// Throw a warning though when creating a new slide
$data['url'] = null;
}
} else {
// Perform a file upload
$file_name = implode('_', array(date('Y-m-d_His'), urlencode($data['url']['name'])));
$file_path = $data['url']['tmp_name'];
$upload_to = "/slides/${file_name}";
if (!copy($file_path, WWW_ROOT . $upload_to)) {
$this->Flash->success(__('File upload failed.'));
} else {
$homepage_slides['url'] = $upload_to;
}
}
if(empty($data['id']))
{
$Slides = $this->Slides->newEntity();
if ($this->request->is('post')) {
// Prior to 3.4.0 $this->request->data() was used.
$slide = $this->Slides->patchEntity($Slides, $this->request->getData());
if ($this->Slides->save($Slides)) {
$this->Flash->success(__('Slide saved.'));
}
$this->Flash->error(__('Some errors were encountered while trying to save this slide.'));
}
$homepage_slides = $connection->update('homepage_slides', $homepage_slides, ['id' => $slide->id]);
return $this->redirect(array('action' => 'admin_index'));
}
else
{
$Slides = $this->Slides->get($slide_id);
if ($this->request->is(['post', 'put'])) {
// Prior to 3.4.0 $this->request->data() was used.
$this->Slides->patchEntity($Slides, $this->request->getData());
if ($this->Slides->save($Slides)) {
$this->Flash->success(__('Slide updated.'));
}
$this->Flash->error(__('Unable to update your slide.'));
}
$homepage_slides = $connection->update('homepage_slides', $homepage_slides, ['id' => $slide_id]);
return $this->redirect(array('action' => 'admin_index'));
}
}
}
if (!empty($slide_id)) {
$data = $this->Slides->find('all', array('conditions' => array('Slides.id' => $slide_id)))->first()->toArray();
$this->set('data', $data);
}
$this->set('slide_id', $slide_id);
}
function delete($slide_id)
{
//Configure::write('debug',2);
// $this->request->allowMethod(['post', 'delete']);
$slide = $this->Slides->get($slide_id);
if ($this->Slides->delete($slide)) {
$this->Flash->success(__('The slide with id: {0} has been deleted.', h($slide_id)));
return $this->redirect(['action' => 'admin_index']);
}
}
} //endclass
src/Template/Slides
admin_index.ctp
<?php echo $this->Html->link('create a new slide', array('controller' => 'slides', 'action' => 'edit')); ?></span>
<h2>Manage Home Page Slides</h2>
<?php
if (!empty($slides)) {
echo $this->Html->tag('table',null,array('class' => 'stripey'));
foreach ($slides as $slide) {
echo '<tr><td>' . $slide->title . '</td><td>' .
$this->Html->link('edit', array('controller' => 'slides', 'action' => 'edit', $slide->id)) .
' | ' .
$this->Html->link('delete', array('controller' => 'slides', 'action' => 'delete', $slide->id)) .
'</td></tr>';
}
echo $this->Html->tag('/table');
} else {
echo $this->Html->tag('p', 'You have not created any slides');
}
echo $this->Html->tag('hr');
//echo $this->Html->tag('p', $this->Html->link('Click here', array('controller' => 'slides', 'action' => 'admin_edit')) . ' to create a new slide.');
?>
---------------------------------------------------------------------------------------------------------------
src/Template/Slides/
edit.ctp
<div class="container">
<div class="slides edit">
<?php
echo $this->Html->tag('h2', sprintf('%s slide', ($slide_id ? 'Edit' : 'Add')),array('class'=>'page-header'));
echo $this->Form->create('Slides', array('url' =>'slides/edit/'.$slide_id, 'type' => 'file'));
echo $this->Form->input('id', array('value' => $slide_id, 'type' => 'hidden') );
echo $this->Form->input('title' ,array('placeholder' => 'Enter a caption','value'=>$data['title']) );
echo $this->Form->input('link' , array('label' => 'Link (URL)', 'placeholder' => 'www.example.com' ,'value'=>$data['link']) );
echo $this->Form->input('url', array('type' => 'file', 'label' => 'Image'));
if(!empty($data['url']))
{
echo "<div class='input file'><label> </label>
<img src=".$data['url']." height='100' width='100'></div>";
}
echo $this->Form->submit(__('Save'));
echo $this->Form->end();
?>
</div>
</div>
Comments
Post a Comment