Initial Photo Work

This commit is contained in:
Deon George
2014-07-09 13:04:56 +10:00
parent d4b3b0f9bc
commit 2e07b08016
23 changed files with 508 additions and 846 deletions

View File

@@ -0,0 +1,185 @@
<?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() {
}
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);
$po->delete = Arr::get($this->request->post('delete'),$pid);
$po->flag = Arr::get($this->request->post('flag'),$pid);
$po->save();
}
$p = ORM::factory('Photo');
if ($x=$this->request->param('id'))
$p->where('id','=',$x);
else
$p->where('duplicate','=',TRUE)
->where_open()
->where('delete','!=',TRUE)
->or_where('delete','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->duplicate_find()->find_all();
// 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()'),
'Filename'=>array('key'=>'filename'),
'Proposed Name'=>array('key'=>'path()'),
'Width'=>array('key'=>'width'),
'Height'=>array('key'=>'height'),
'Orientation'=>array('key'=>'orientation'),
'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(
'Flag'=>array('key'=>'id','value'=>'flag'),
'Duplicate'=>array('key'=>'id','value'=>'duplicate'),
'Delete'=>array('key'=>'id','value'=>'delete'),
) 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="offset2">';
$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;
}
private function table_duplicate_details(Database_MySQL_Result $dp,Model_Photo $po,$param,$title='',$content='') {
$output = '<tr>';
if (preg_match('/\(/',$param) OR preg_match('/-\>/',$param))
eval("\$d = \$po->$param;");
else
$d = $po->display($param);
$output .= sprintf('<th>%s</th>',$title);
$output .= sprintf('<td>%s</td>',$content ? str_replace('%VALUE%',$d,$content) : $d);
foreach ($dp as $dpo) {
if (preg_match('/\(/',$param) OR preg_match('/-\>/',$param))
eval("\$d = \$dpo->$param;");
else
$d = $dpo->display($param);
$output .= sprintf('<td>%s</td>',$content ? str_replace('%VALUE%',$d,$content) : $d);
}
$output .= '</tr>';
return $output;
}
}
?>

View File

@@ -1,218 +0,0 @@
<?php defined('SYSPATH') or die('No direct script access.');
include_once 'includes/kohana/modules/simplehtmldom/classes/simple_html_dom.php';
class Controller_Translate extends Controller_TemplateDefault {
private $_tags = array(
'span.aer_title',
'span.source_panel_heading',
'td.section_heading',
'td.standard_point ul li',
'span.source_panel_heading',
'td.document_name',
'td.collection',
'td.author',
'td.business_case_heading',
'span.source_panel_heading',
'span.target_panel_heading',
'td.grid_header',
'td.tco_label',
'td.summary_header',
'td.summary_tag',
'td.summary_data',
'span.target_mig_panel_heading',
);
private function aer() {
$file = '/local/WEB/sites/net.leenooks.dev/aer/application/media/aer/Honda Foundry_aer.htm';
// Fix errors in the HTML file
$data = file_get_contents($file);
$data = preg_replace('/<td class="grid_total"<td class="grid_header">/','<td class="grid_header">',$data);
return $data;
}
public function action_index() {
HTTP::redirect(URL::link('','translate/render'));
$output = '';
Block::factory()
->title('Hello')
->title_icon('icon-cog')
->body($output);
}
public function action_import() {
$html = new simple_html_dom();
$html->load($this->aer());
$this->store($html->find('head',0)->find('title',0));
foreach ($this->_tags as $z)
foreach ($html->find($z) as $x)
$this->store($x);
HTTP::redirect(URL::link('','translate/index'));
}
public function action_render() {
$output = '';
if ($this->request->post('language_id')) {
$html = new simple_html_dom();
$html->load($this->aer());
$x = $html->find('head',0);
$x->innertext .= '<meta http-equiv="content-type" content="text/html;charset=utf-8">';
$lo = ORM::factory('Language',$this->request->post('language_id'));
foreach ($this->_tags as $z)
foreach ($html->find($z) as $x)
$x->innertext = $this->translate($x,$lo);
// Convert order the img tags
foreach ($html->find('img') as $z) {
$z->src = sprintf('%s/%s',URL::site('media/aer'),$z->src);
}
$this->response->body($html);
$this->auto_render = FALSE;
// We dont know what sort of payment type yet
} else {
$x = $this->lang();
$output .= Form::open();
$output .= Form::select('language_id',ORM::factory('Language')->list_select(),$x->id);
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
$output .= Form::close();
}
Block::factory()
->title('Render in...')
->title_icon('icon-share')
->body($output);
}
public function action_save() {
foreach ($this->request->post('x') as $id => $value) {
if (! $value)
continue;
$to = ORM::factory('Translate',array('language_id'=>$this->request->post('language_id'),'original_id'=>$id));
$to->translation = $value;
$to->language_id = $this->request->post('language_id');
$to->original_id = $id;
$to->save();
}
HTTP::redirect(sprintf('%s?language_id=%s&page=%s',URL::link('','translate/text'),$this->request->post('language_id'),$this->request->post('page')));
}
private function lang() {
foreach ($this->request->accept_lang() as $k=>$v) {
if (strlen($k) == 2)
$k = sprintf('%s_%s',strtolower($k),strtoupper($k));
else {
list($k,$v) = preg_split('/[-_]/',$k,2);
$k = sprintf('%s_%s',strtolower($k),strtoupper($v));
}
if ($x=ORM::factory('Language',array('iso'=>$k)))
return $x;
}
}
private function store(simple_html_dom_node $x,$l=0) {
if ($x->children()) {
foreach ($x->children() as $y) {
$this->store($y,$l+1);
$y->innertext = '%s';
}
}
// If we have a numeric value, convert it to %d
$x->innertext = preg_replace('/[0-9.]+/','%d',$x->innertext);
$oo = ORM::factory('Original',array('sentence'=>$x->innertext));
if (! trim($x->innertext) or (in_array(trim($x->innertext),array('$','&nbsp;')) or preg_match('/%d%?$/',$x->innertext))) {
return $x->innertext;
} elseif (! $oo->loaded()) {
$oo->sentence = $x->innertext;
$oo->save();
}
return $x->innertext;
}
private function translate(simple_html_dom_node $x,Model_Language $lo,$l=0) {
$nums = NULL;
$matches = array();
$dom_tmp = str_get_html($x->outertext);
$dom_tmp_node = $dom_tmp->firstChild();
$string = $this->store($dom_tmp_node,$l);
$oo = ORM::factory('Original',array('sentence'=>$string));
// If we have numbers, we'll need to save them.
if (preg_match('/%d/',$string))
$nums = preg_match('/[0-9.]+/',$x->innertext,$matches);
if ($oo->loaded()) {
$to = ORM::factory('Translate',array('original_id'=>$oo->id,'language_id'=>$lo->id));
$string = $to->loaded() ? $to->translation : $x->innertext;
}
if ($nums && $nums == 1)
$string = str_replace('%d',$matches[0],$string);
elseif ($nums > 1)
throw HTTP_Exception::factory('501','Argh, didnt allow for more than 1 match');
if ($x->children()) {
foreach ($x->children() as $y) {
$string = preg_replace('/%s/',$this->translate($y,$lo,$l+1),$string);
}
}
return $string;
}
public function action_text() {
$output = '';
if ($this->request->query('language_id')) {
$output .= Form::open(URL::link('','translate/save'));
$output .= Form::hidden('language_id',$this->request->query('language_id'));
$oo = ORM::factory('Original')
->select('translate.translation')
->join('translate','LEFT OUTER')
->on('original.id','=','translate.original_id')
->on('translate.language_id','=',$this->request->query('language_id'));
$output .= View::factory('translate')
->set('o',$oo->find_all());
// We dont know what sort of payment type yet
} else {
$x = $this->lang();
$output .= Form::open(NULL,array('method'=>'GET'));
$output .= Form::select('language_id',ORM::factory('Language')->list_select(),$x->id);
}
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
$output .= Form::close();
Block::factory()
->title('Translate Text')
->title_icon('icon-share')
->body($output);
}
} // End Welcome

View File

@@ -4,7 +4,7 @@ class Controller_Welcome extends Controller {
public function action_index()
{
HTTP::redirect(URL::link('','translate'));
HTTP::redirect('photo');
}
} // End Welcome