photo/application/classes/Controller/Photo.php

286 lines
8.7 KiB
PHP
Raw Normal View History

2014-07-09 03:04:56 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Mark all accounts that have no outstanding invoices and active services as disabled.
*
* @package Photo
* @category Controllers
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_Photo extends Controller_TemplateDefault {
public function action_index() {
}
2015-05-15 04:12:10 +00:00
public function action_delete() {
$output = '';
// Update the current posted photos.
if ($this->request->post())
foreach ($this->request->post('process') as $pid) {
if (! Arr::get($this->request->post('remove'),$pid,FALSE))
continue;
$po = ORM::factory('Photo',$pid);
// If the photo is not marked as remove, or flagged, dont do it.
if (! $po->loaded() OR $po->flag OR ! $po->remove)
continue;
if (! is_writable(dirname($po->file_path())))
$output .= sprintf('Dont have write permissions on %s',dirname($po->file_path()));
$output .= sprintf('Removing %s (%s)',$po->id,$po->file_path());
}
$p = ORM::factory('Photo');
// Review a specific photo, or the next one marked remove
if ($x=$this->request->param('id'))
$p->where('id','=',$x);
else
$p->where('remove','=',TRUE)
->where_open()
->where('flag','!=',TRUE)
->or_where('flag','is',NULL)
->where_close();
$output .= Form::open(sprintf('%s/%s',strtolower($this->request->controller()),$this->request->action()));
foreach ($p->find_all() as $po) {
$dp = $po->list_duplicate()->find_all();
$output .= Form::hidden('process[]',$po->id);
foreach ($dp as $dpo)
$output .= Form::hidden('process[]',$dpo->id);
$output .= '<table class="table table-striped table-condensed table-hover">';
foreach (array(
'ID'=>array('key'=>'id','value'=>HTML::anchor('/photo/details/%VALUE%','%VALUE%')),
'Thumbnail'=>array('key'=>'id','value'=>HTML::anchor('/photo/view/%VALUE%',HTML::image('photo/thumbnail/%VALUE%'))),
'Signature'=>array('key'=>'signature'),
'Date Taken'=>array('key'=>'date_taken()'),
'File Modified'=>array('key'=>'date_file("m",TRUE)'),
'File Created'=>array('key'=>'date_file("c",TRUE)'),
'Filename'=>array('key'=>'file_path(TRUE,FALSE)'),
'Proposed Name'=>array('key'=>'path()'),
'Width'=>array('key'=>'width'),
'Height'=>array('key'=>'height'),
'Orientation'=>array('key'=>'orientation'),
'Orientate'=>array('key'=>'rotation()'),
'Make'=>array('key'=>'make'),
'Model'=>array('key'=>'model'),
) as $k=>$v)
$output .= $this->table_duplicate_details($dp,$po,$v['key'],$k,Arr::get($v,'value','%VALUE%'));
foreach (array(
'Delete'=>array('key'=>'id','value'=>'remove'),
) as $k=>$v)
$output .= $this->table_duplicate_checkbox($dp,$po,$v['key'],$k,Arr::get($v,'value','%VALUE%'));
$output .= '</table>';
break;
}
$output .= '<div class="row">';
$output .= '<div class="col-md-offset-2">';
$output .= '<button type="submit" class="btn btn-primary">Save changes</button>';
$output .= '<button type="button" class="btn">Cancel</button>';
$output .= '</div>';
$output .= '</div>';
$output .= Form::close();
Block::factory()
->title('Delete Photo:'.$po->id)
->title_icon('icon-delete')
->body($output);
}
2014-07-09 03:04:56 +00:00
public function action_details() {
$po = ORM::factory('Photo',$this->request->param('id'));
if (! $po->loaded())
HTTP::redirect('index');
Block::factory()
->title('Details for Photo:'.$po->id)
->body(Debug::vars($po->info()));
}
public function action_duplicate() {
$output = '';
// Update the current posted photos.
if ($this->request->post())
foreach ($this->request->post('process') as $pid) {
$po = ORM::factory('Photo',$pid);
$po->duplicate = Arr::get($this->request->post('duplicate'),$pid);
2015-05-15 04:12:10 +00:00
$po->remove = Arr::get($this->request->post('remove'),$pid);
2014-07-09 03:04:56 +00:00
$po->flag = Arr::get($this->request->post('flag'),$pid);
$po->save();
}
$p = ORM::factory('Photo');
2015-05-15 04:12:10 +00:00
// Review a specific photo, or the next one marked duplicate
2014-07-09 03:04:56 +00:00
if ($x=$this->request->param('id'))
$p->where('id','=',$x);
else
$p->where('duplicate','=',TRUE)
->where_open()
2015-05-15 04:12:10 +00:00
->where('remove','!=',TRUE)
->or_where('remove','is',NULL)
2014-07-09 03:04:56 +00:00
->where_close();
$output .= Form::open(sprintf('%s/%s',strtolower($this->request->controller()),$this->request->action()));
foreach ($p->find_all() as $po) {
2015-05-15 04:12:10 +00:00
$dp = $po->list_duplicate()->find_all();
2014-07-09 03:04:56 +00:00
// Check that there are still duplicates
if ($dp->count() == 0) {
$po->duplicate = NULL;
$po->save();
continue;
}
$output .= Form::hidden('process[]',$po->id);
foreach ($dp as $dpo)
$output .= Form::hidden('process[]',$dpo->id);
$output .= '<table class="table table-striped table-condensed table-hover">';
foreach (array(
'ID'=>array('key'=>'id','value'=>HTML::anchor('/photo/details/%VALUE%','%VALUE%')),
'Thumbnail'=>array('key'=>'id','value'=>HTML::anchor('/photo/view/%VALUE%',HTML::image('photo/thumbnail/%VALUE%'))),
'Signature'=>array('key'=>'signature'),
'Date Taken'=>array('key'=>'date_taken()'),
2015-05-15 04:12:10 +00:00
'File Modified'=>array('key'=>'date_file("m",TRUE)'),
'File Created'=>array('key'=>'date_file("c",TRUE)'),
'Filename'=>array('key'=>'file_path(TRUE,FALSE)'),
'Proposed Name'=>array('key'=>'file_path(TRUE,TRUE)'),
2014-07-09 03:04:56 +00:00
'Width'=>array('key'=>'width'),
'Height'=>array('key'=>'height'),
'Orientation'=>array('key'=>'orientation'),
2015-05-15 04:12:10 +00:00
'Orientate'=>array('key'=>'rotation()'),
2014-07-09 03:04:56 +00:00
'Make'=>array('key'=>'make'),
'Model'=>array('key'=>'model'),
2015-05-15 04:12:10 +00:00
'Exif Diff'=>array('key'=>"propertydiff({$po->id})"),
2014-07-09 03:04:56 +00:00
) as $k=>$v)
$output .= $this->table_duplicate_details($dp,$po,$v['key'],$k,Arr::get($v,'value','%VALUE%'));
foreach (array(
'Flag'=>array('key'=>'id','value'=>'flag'),
'Duplicate'=>array('key'=>'id','value'=>'duplicate'),
2015-05-15 04:12:10 +00:00
'Delete'=>array('key'=>'id','value'=>'remove'),
2014-07-09 03:04:56 +00:00
) as $k=>$v)
$output .= $this->table_duplicate_checkbox($dp,$po,$v['key'],$k,Arr::get($v,'value','%VALUE%'));
$output .= '</table>';
break;
}
$output .= '<div class="row">';
2015-05-15 04:12:10 +00:00
$output .= '<div class="col-md-offset-2">';
2014-07-09 03:04:56 +00:00
$output .= '<button type="submit" class="btn btn-primary">Save changes</button>';
$output .= '<button type="button" class="btn">Cancel</button>';
$output .= '</div>';
$output .= '</div>';
$output .= Form::close();
Block::factory()
->title('Duplicate Photo:'.$po->id)
->title_icon('icon-edit')
->body($output);
}
public function action_thumbnail() {
// Get the file path from the request
$po = ORM::factory('Photo',$this->request->param('id'));
return $this->image($po->thumbnail(),$po->date_taken,$po->type(TRUE));
}
public function action_view() {
$po = ORM::factory('Photo',$this->request->param('id'));
return $this->image($po->image(),$po->date_taken,$po->type(TRUE));
}
private function image($content,$modified,$type) {
// Send the file content as the response
if ($content)
$this->response->body($content);
// Return a 404 status
else
$this->response->status(404);
// Generate and check the ETag for this file
if (Kohana::$environment < Kohana::TESTING OR Kohana::$config->load('debug')->etag)
$this->check_cache(sha1($this->response->body()));
// Set the proper headers to allow caching
$this->response->headers('Content-Type',$type);
$this->response->headers('Content-Length',(string)$this->response->content_length());
$this->response->headers('Last-Modified',date('r',$modified));
$this->auto_render = FALSE;
}
private function table_duplicate_checkbox(Database_MySQL_Result $dp,Model_Photo $po,$param,$title,$condition) {
$output = '<tr>';
$output .= sprintf('<th>%s</th>',$title);
$output .= '<td>'.Form::checkbox($condition.'['.$po->{$param}.']',TRUE,$po->{$condition} ? TRUE : FALSE).'</td>';
foreach ($dp as $dpo)
$output .= '<td>'.Form::checkbox($condition.'['.$dpo->{$param}.']',TRUE,$dpo->{$condition} ? TRUE : FALSE).'</td>';
$output .= '</tr>';
return $output;
}
2015-05-15 04:12:10 +00:00
private function evaluate(Model $o,$param) {
$result = NULL;
2014-07-09 03:04:56 +00:00
if (preg_match('/\(/',$param) OR preg_match('/-\>/',$param))
2015-05-15 04:12:10 +00:00
eval("\$result = \$o->$param;");
2014-07-09 03:04:56 +00:00
else
2015-05-15 04:12:10 +00:00
$result = $o->display($param);
return $result;
}
private function table_duplicate_details(Database_MySQL_Result $dp,Model_Photo $po,$param,$title='',$content='') {
$output = '<tr>';
$v = $this->evaluate($po,$param);
2014-07-09 03:04:56 +00:00
$output .= sprintf('<th>%s</th>',$title);
2015-05-15 04:12:10 +00:00
$output .= sprintf('<td>%s</td>',$content ? str_replace('%VALUE%',$v,$content) : $v);
2014-07-09 03:04:56 +00:00
foreach ($dp as $dpo) {
2015-05-15 04:12:10 +00:00
$d = $this->evaluate($dpo,$param);
2014-07-09 03:04:56 +00:00
2015-05-15 04:12:10 +00:00
$output .= sprintf('<td class="%s">%s</td>',($d==$v ? 'success' : 'warning'),$content ? str_replace('%VALUE%',$d,$content) : $d);
2014-07-09 03:04:56 +00:00
}
$output .= '</tr>';
return $output;
}
}
?>