/**
* Dvds Controller
*
* file: /app/controllers/dvds_controller.php
*/
**i build dvd script for dvds library.. when i want to edit dvds
details the image i have uploded dont display but when i see databse
table i see it is already into this table and i find the image in the
webroot img folder (dvds)..so how to show image to edit it?
Dvds Controller
<?php
class DvdsController extends AppController {
// good practice to include the name variable
var $name = 'Dvds';
// load any helpers used in the views
var $helpers = array('Html', 'Form', 'Javascript', 'Misc');
// global ratings variable
var $ratings = array('0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3',
'4'=>'4', '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9',
'10'=>'10');
/**
* index()
* main index page for dvds
* url: /dvds/index
*/
function index() {
// get all dvds from database where status = 1
//$dvds = $this->Dvd->find("Dvd.status=1", null, "Dvd.name");
$dvds = $this->Dvd->find('all', array('conditions' =>
array('Dvd.status' => '1')));
// save the dvds in a variable for the view
$this->set('dvds', $dvds);
}
/**
* view()
* displays a single dvd and all related info
* url: /dvds/view/dvd_slug
*/
function view($slug) {
// if slug is null
if(!$slug) {
// set a flash message
$this->Session->setFlash('Invalid slug for DVD',
'flash_bad');
// redirect user
$this->redirect(array('action'=>'index'));
}
// find dvd in database
$dvd = $this->Dvd->findBySlug($slug);
// if dvd has been found
if(!empty($dvd)) {
// set the dvd for the view
$this->set('dvd', $dvd);
} else {
// set a flash message
$this->Session->setFlash('Invalid slug for DVD',
'flash_bad');
// redirect user
$this->redirect(array('action'=>'index'));
}
}
/**
* admin_index()
* main index page for admin users
* url: /admin/dvds/index
*/
function admin_index() {
// get all dvds from database where status = 1, order by dvd
name
// $dvds = $this->Dvd->findAll("Dvd.status=1", null,
"Dvd.name");
$dvds = $this->Dvd->find('all', array('conditions' =>
array('Dvd.status' => '1')));
// save the dvds in a variable for the view
$this->set('dvds', $dvds);
}
/**
* admin_add()
* allows an admin to add a dvd
* url: /admin/dvds/add
*/
function admin_add() {
// if the form data is not empty
if (!empty($this->data)) {
// check for image
$image_ok = $this->_upload_image();
// if the image was uploaded successfully
if($image_ok) {
// initialise the Dvd model
$this->Dvd->create();
// create the slug
$this->data['Dvd']['slug'] = $this->slug($this-
>data['Dvd']['name']);
// check for a dvd with the same slug
$dvd = $this->Dvd->find('first', array(
'conditions' => array(
'Dvd.slug'=>$this->data['Dvd']['slug'],
'Dvd.status' => '1'
)
));
// if slug is not taken
if(empty($dvd)) {
// try saving the dvd
if ($this->Dvd->save($this->data)) {
// set a flash message
$this->Session->setFlash('The DVD has been
saved', 'flash_good');
// redirect
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The DVD could not be
saved. Please, try again.', 'flash_bad');
}
} else {
// set a flash message
$this->Session->setFlash('The DVD could not be
saved. The Name has already been taken.', 'flash_bad');
}
}
}
// find dvd options in a list format
// new 1.2 feature, can also have 'count' and 'first'
$formats = $this->Dvd->Format->find('list');
$types = $this->Dvd->Type->find('list');
$locations = $this->Dvd->Location->find('list');
$ratings = $this->ratings;
// set the variables so they can be accessed from the view
$this->set(compact('formats', 'types', 'locations',
'ratings'));
}
/**
* admin_edit()
* allows an admin to edit a dvd
* url: /admin/dvds/edit/id
*/
function admin_edit($id = null) {
// if the id is null and the form data empty
if (!$id && empty($this->data)) {
// set a flash message
$this->Session->setFlash('Invalid Dvd', 'flash_bad');
// redirect the admin
$this->redirect(array('action'=>'index'));
}
// if the form was submitted
if (!empty($this->data)) {
// check for image
$image_ok = $this->_upload_image();
// if the image was uploaded successfully
if($image_ok) {
// create the slug
$this->data['Dvd']['slug'] = $this->slug($this-
>data['Dvd']['name']);
// check for a dvd with the same slug
$dvd = $this->Dvd->find('first', array(
'conditions' => array(
'Dvd.slug'=>$this->data['Dvd']['slug'],
'Dvd.status' => '1'
)
));
// if slug is not taken
if(empty($dvd) || $dvd['Dvd']['id'] == $id) {
// try to save the Dvd
if ($this->Dvd->save($this->data)) {
// set a flash message
$this->Session->setFlash('The Dvd has been
saved', 'flash_good');
// redirect the admin
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The Dvd could not be
saved. Please, try again.', 'flash_bad');
}
} else {
// set a flash message
$this->Session->setFlash('The DVD could not be
saved. The Name has already been taken.', 'flash_bad');
}
}
} else {
// find the DVD from the database and save it in the data
array
$this->data = $this->Dvd->read(null, $id);
}
// find dvd options from database in a list
$formats = $this->Dvd->Format->find('list');
$types = $this->Dvd->Type->find('list');
$locations = $this->Dvd->Location->find('list');
$ratings = $this->ratings;
$this->set(compact('formats','types','locations', 'ratings'));
}
/**
* admin_delete()
* allows an admin to delete a dvd
* url: /admin/dvds/delete/1
*/
function admin_delete($id = null) {
// if the id is null
if (!$id) {
// set flash message
$this->Session->setFlash('Invalid id for Dvd',
'flash_bad');
// redirect
$this->redirect(array('action'=>'index'));
}
// set the id of the dvd
$this->Dvd->id = $id;
// try to change status from 1 to 0
if ($this->Dvd->saveField('status', 0)) {
// set flash message
$this->Session->setFlash('The Dvd was successfully
deleted.', 'flash_good');
} else {
// set flash message
$this->Session->setFlash('The Dvd could not be deleted.
Please try again.', 'flash_bad');
}
// redirect
$this->redirect(array('action'=>'index'));
}
/**
* upload_image()
* private function to upload a file if it exists in the form
*/
function _upload_image() {
// init
$image_ok = TRUE;
// if a file has been added
if($this->data['File']['image']['error'] != 4) {
// try to upload the file
$result = $this->upload_files('img/dvds', $this->data['File']);
// if there are errors
if(array_key_exists('errors', $result)) {
// set image ok to false
$image_ok = FALSE;
// set the error for the view
$this->set('errors', $result['errors']);
} else {
// save the url
$this->data['Dvd']['image'] = $result['urls'][0];
}
}
return $image_ok;
}
}
?>
and the view file is
<div class="dvds form">
<?php
// if there was an error uploading the file then display errors here
if(isset($errors)) {
echo $misc->display_errors($errors);
}
?>
<?php echo $form->create('Dvd', array('type'=>'file'));?>
<fieldset>
<legend>Edit a Dvd</legend>
<?php
// create the form inputs
// include the id of the DVD as a form input
// CakePHP will automatically create this as a hidden element
echo $form->input('id');
echo $form->input('name', array('label'=>'Name: *'));
echo $form->input('format_id', array('label'=>'Format: *',
'type'=>'select', 'options'=>$formats));
echo $form->input('type_id', array('label'=>'Type: *',
'class'=>'type_select'));
echo $form->input('location_id', array('label'=>'Location:
*'));
// display image if it exists
if(!empty($this->data['Dvd']['image'])): ?>
<div class="input">
<label>Current Image:</label>
<img src="/<?php echo $this->data['Dvd']['image']; ?>" width="100" />
</div>
<?php endif;
echo $form->input('File.image', array('label'=>'Image:',
'type'=>'file'));
echo $form->input('rating', array('label'=>'Rating:'));
echo $form->input('website', array('label'=>'Website URL:'));
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php