Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Showing with 83 additions and 1 deletion.
  1. +15 −0 lib/Model/DVD.php
  2. +23 −0 lib/Model/DVD/InStore.php
  3. +6 −0 lib/Model/Movie.php
  4. +13 −0 lib/Model/Movie/Available.php
  5. +26 −1 page/video.php
View
15 lib/Model/DVD.php
@@ -6,9 +6,24 @@ function init(){
$this->addField('movie_id')->refModel('Model_Movie');
$this->addField('code');
+
+ $this->addfield('is_rented')
+ ->type('boolean')
+ ->calculated(true);
}
function toStringSQL($source_field, $dest_fieldname){
return 'concat("DVD#",id,": ",(select name
from movie m,dvd d where m.id=d.movie_id and d.id='.$source_field.')) as '.$dest_fieldname;
}
+ function calculate_is_rented(){
+ $select=$this->add('Model_Rental')
+ ->dsql()
+ ->field('id')
+ ->where('rental.dvd_id=dvd.id')
+ ->where('is_returned!=','Y')
+ ->select()
+ ;
+
+ return "if(($select) is null,'N','Y')";
+ }
}
View
23 lib/Model/DVD/InStore.php
@@ -0,0 +1,23 @@
+<?php
+class Model_DVD_InStore extends Model_DVD {
+ function init(){
+ parent::init();
+ $this->addCondition('is_rented',false);
+ }
+ function dsql_count(){
+ return $this
+ ->dsql(null,false)
+ ->field('count(*)')
+ ->where('('.$this->calculate_is_rented().')!=','Y')
+ ->where('dvd.movie_id=movie.id')
+ ;
+ }
+ function rent($customer_id){
+ $m=$this->add('Model_Rental')
+ ->set('dvd_id',$this->get('id'))
+ ->set('customer_id',$customer_id)
+ ;
+ $m->update();
+ return $m;
+ }
+}
View
6 lib/Model/Movie.php
@@ -7,5 +7,11 @@ function init(){
$this->addField('name');
$this->addField('year')->type('int');
$this->addField('imdb')->caption('IMDB Link');
+
+ $this->addField('available')->calculated(true);
+ }
+ function calculate_available(){
+ return $this->add('Model_DVD_InStore')
+ ->dsql_count()->select();
}
}
View
13 lib/Model/Movie/Available.php
@@ -0,0 +1,13 @@
+<?php
+class Model_Movie_Available extends Model_Movie {
+ function init(){
+ parent::init();
+ $this->addCondition('available>',0);
+ }
+ function rent($customer_id){
+ $m=$this->add('Model_DVD_InStore')
+ ->loadBy('movie_id',$this->get('id'));
+
+ return $m->rent($customer_id);
+ }
+}
View
27 page/video.php
@@ -4,6 +4,31 @@ function init(){
parent::init();
$this->api->auth->check();
- $this->add('MVCGrid')->setModel('Movie');
+ $grid=$this->add('MVCGrid');
+ $grid->setModel('Movie_Available');
+ $grid->addColumn('button','rent');
+ if($_GET['rent']){
+ $rental = $grid->getModel()->loadData($_GET['rent'])->rent($this->api->auth->get('id'));
+
+ $grid->js(null,
+ $this->js()->reload())->univ()->successMessage('Rented successfully #'.$rental->get('id'))
+ ->execute();
+ }
+
+ $grid=$this->add('MVCGrid');
+ $grid->setModel('Rental')
+ ->addCondition('customer_id',$this->api->auth->get('id'))
+ ->addCondition('is_returned',false)
+ ;
+ $grid->addColumn('button','return');
+
+ if($_GET['return']){
+ $rental = $grid->getModel()->loadData($_GET['return'])->returnMovie();
+
+ $grid->js(null,
+ $this->js()->reload())->univ()->successMessage('Returned #'.$_GET['return'])
+ ->execute();
+ }
+
}
}

No commit comments for this range

Something went wrong with that request. Please try again.