Skip to content
This repository has been archived by the owner on Sep 4, 2022. It is now read-only.

Commit

Permalink
Merge branch '4.2' of github.com:atk4/atk4-addons into 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Romans Malinovskis committed Feb 14, 2012
2 parents d091d7a + e80f4ab commit 74a1322
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 60 deletions.
9 changes: 0 additions & 9 deletions filestore/Field/Image.php

This file was deleted.

22 changes: 22 additions & 0 deletions filestore/lib/Field/Image.php
@@ -0,0 +1,22 @@
<?php
namespace filestore;
class Field_Image extends \Field {
function init(){
parent::init();
$this->setModel('filestore/Image');
$this->display(array('form'=>'upload'));
}
function ref($load=true){
if(!$this->model){
$this->model=preg_replace('|^(.*/)?(.*)$|','\1Model_\2',$this->getModel());
$this->model=$this->add($this->model);
}
if(!$load)return $this->model;
if(!$this->get())throw $this->exception('Reference field has no value')
->addMoreInfo('model',$this->owner)
->addMoreInfo('field',$this->short_name)
->addMoreInfo('id',$this->owner->id)
;
return $this->model->load($this->get());
}
}
38 changes: 20 additions & 18 deletions filestore/Model/File.php → filestore/lib/Model/File.php
Expand Up @@ -6,6 +6,7 @@ class Model_File extends \Model_Table {
public $entity_filestore_type='Type';
public $entity_filestore_volume='Volume';

public $impotr_mode=null;
public $import_source=null;

function init(){
Expand Down Expand Up @@ -34,16 +35,17 @@ function init(){
;
$this->newField('filesize')
->datatype('int')
->defaultValue(0)
;
$this->newField('deleted')
->datatype('boolean')
->defaultValue(false)
;

$this->newField('name_size')
->calculated(true)
;
$this->addHook('beforeInsert',$this);
$this->addHook('beforeModify',$this);
$this->addHook('beforeSave',$this);
}
function calculate_name_size(){
return 'concat("[",filestore_file.id,"] ",coalesce(original_filename,"??")," (",coalesce(round(filesize/1024),"?"),"k)")';
Expand All @@ -54,21 +56,19 @@ function toStringSQL($source_field, $dest_fieldname){
public function getListFields(){
return array('id'=>'id','name_size'=>'name');
}
function beforeInsert($m,$q){
$this->set('filestore_volume_id',$x=$this->getAvailableVolumeID());
$this->set('filename',$this->generateFilename());
if($this->import_source){
$this->performImport();
}
}
function beforeModify(){
if($this->import_source){
function beforeSave($m){
if(!$this->loaded()){
// New record, generate the name
$this->set('filestore_volume_id',$x=$this->getAvailableVolumeID());
$this->set('filename',$this->generateFilename());
}
if($this->import_mode){
$this->performImport();
}
}
function getAvailableVolumeID(){
// Determine best suited volume and returns it's ID
$c=$this->add('./Model_'.$this->entity_filestore_volume)
$c=$this->add('filestore/Model_'.$this->entity_filestore_volume)
->addCondition('enabled',true)
->addCondition('stored_files_cnt','<',4096*256*256)
;
Expand All @@ -93,7 +93,7 @@ function getFiletypeID($mime_type = null, $add = false){
if(!$path)throw $this->exception('Load file entry from filestore or import');
$mime_type=mime_content_type($path);
}
$c=$this->add('./Model_'.$this->entity_filestore_type);
$c=$this->add('filestore/Model_'.$this->entity_filestore_type);
$data = $c->getBy('mime_type',$mime_type);
if(!$data['id']){
if ($add){
Expand Down Expand Up @@ -131,7 +131,7 @@ function generateFilename(){

// Verify that file was created
if(!file_exists($d.'/'.$file)){
throw $this->exception('Could not create file')->addMoreInfo('inside',$d);
throw $this->exception('Could not create file')->addMoreInfo('inside',$d)->addMoreInfo('file',$file);
}

return dechex($node).'/'.$file;
Expand All @@ -150,12 +150,12 @@ function import($source,$mode='upload'){
$this->import_source=$source;
$this->import_mode=$mode;

if($this->isInstanceLoaded() && $this->get('id')){// -- if we have this, then we cannot really
// import anything..
if($this->isInstanceLoaded() && $this->get('id')){// -- if we have this, then we
// can import right now

// If file is already in database - put it into store
$this->performImport();
$this->update();
$this->save();
}
return $this;
}
Expand Down Expand Up @@ -195,9 +195,11 @@ function performImport(){
}
chmod($destination, $this->api->getConfig('filestore/chmod',0660));
clearstatcache();
$this->set('filesize',filesize($destination));
$this->set('filesize',$f=filesize($destination));
$this->set('deleted',false);
$this->set('filestore_type_id',$this->getFiletypeID());
$this->import_source=null;
$this->import_mode=null;
return $this;
}
/*
Expand Down
50 changes: 17 additions & 33 deletions filestore/Model/Image.php → filestore/lib/Model/Image.php
Expand Up @@ -11,40 +11,22 @@ class Model_Image extends Model_File {
function init(){
parent::init();

//$this->addRelatedEntity('i','filestore_image','original_file_id','inner','related',true);
$this->i=$this->join('filestore_image.original_file_id');

/*
$this->newField('original_file_id')
->relEntity('i')
->datatype('int')->system(true);
/*
->refModel('Model_'.$this->entity_file)
->caption('Original File')
;
*/
$this->hasOne('filestore/'.$this->entity_file,'original_file_id')
->caption('Original File');
*/

$this->i->hasOne('filestore/'.$this->entity_file,'thumb_file_id')
->caption('Thumbnail');
/*
addField('thumb_file_id')
->datatype('reference')
->refModel('Model_'.$this->entity_file)
->caption('Thumbnail')
;
*/
}
function toStringSQL($source_field, $dest_fieldname){
return $source_field.' '.$dest_fieldname;
}
function performImport(){
echo "i";
parent::performImport();

echo "g";
$original=$this->getPath();
echo "o";

$this->createThumbnail('thumb_file_id',$this->default_thumb_height,$this->default_thumb_width);

// Now that the origninal is imported, lets generate thumbnails
Expand All @@ -53,35 +35,37 @@ function performImport(){
$this->update();
$this->afterImport();
*/
echo "I";
return $this;
}
function createThumbnail($field,$x,$y){
// Create entry for thumbnail.
$thumb=$this->getRef($field);
if(!$thumb->isInstanceLoaded()){
$thumb=$this->ref($field,false);
if(!$thumb->loaded()){
$thumb->set('filestore_volume_id',$this->get('filestore_volume_id'));
$thumb->set('original_filename','thumb_'.$this->get('original_filename'));
$thumb->set('filestore_type_id',$this->get('filestore_type_id'));
$thumb->update();
$this->set($field,$thumb->get('id'));
}

$image=new Imagick($this->getPath());
$image->resizeImage($x,$y,Imagick::FILTER_LANCZOS,1,true);
$image->writeImage($thumb->getPath());
if(class_exists('\Imagick',false)){
$image=new \Imagick($this->getPath());
$image->resizeImage($x,$y,\Imagick::FILTER_LANCZOS,1,true);
$thumb->save(); // generates filename
$image->writeImage($thumb->getPath());
$thumb->import(null,'none');
}else{
// No Imagemagick support. Ignore resize
$thumb->import($this->getPath(),'copy');
}
$thumb->save(); // update size and chmod

$thumb->import(null,'none');
$thumb->update(); // update size and chmod
$this->set($field,$thumb->get('id'));
}
function afterImport(){
// Called after original is imported. You can do your resizes here

$f=$this->getPath();

$gd_info=getimagesize($f);
var_Dump($f);

}
function setMaxResize(){
}
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions formandsave/lib/FormAndSave.php
@@ -0,0 +1,13 @@
<?php
namespace formandsave;
class FormAndSave extends \Form {
function init(){
parent::init();
$this->addSubmit('Save');

$this->onSubmit(function($f){
$f->update();
$f->js()->univ()->successMessage('Saved')->execute();
});
}
}
78 changes: 78 additions & 0 deletions gridorder/lib/Controller/GridOrder.php
@@ -0,0 +1,78 @@
<?php
namespace gridorder;
class Controller_GridOrder extends \AbstractController {
public $model;

function init(){
parent::init();

$l=$this->api->locate('addons','gridorder','location');
$this->api->pathfinder->addLocation($this->api->locate('addons','gridorder'),array(
'template'=>'templates'
))->setParent($l);

$this->owner->addButton('Re-order records')
->js('click')->univ()->frameURL('Re-order records',
$this->api->getDestinationURL(null,
array($this->name=>'activate')),array('width'=>'500px'));

$this->owner->dq->order('ord');

if($_GET[$this->name]=='activate'){
$this->api->stickyGET($this->name);
$this->initFrame();
}
}
function initFrame(){
$v=$this->owner->owner->add('View',null,$this->owner->spot);
$_GET['cut_object']=$v->name;

$lister=$v->add('CompleteLister',null,null,array('view/gridorder'));
$this->model=$m=$this->owner->getModel();

if(!$m->hasElement('ord')){
$m->addField('ord')->system(true);
}

$lister->setModel($m);
$lister->dq->order('ord');

$lister->js(true)->sortable();

$v->add('Button')->set('Save')->js('click')->univ()->ajaxec(
array($this->api->getDestinationURL(),
$this->name.'_order'=>$v->js(null,"\$('#{$lister->name}').children().map(function(){ return $(this).attr('data-id'); }).get().join(',')")
)
);
if(isset($_GET[$this->name.'_order'])){
$this->processReorder($_GET[$this->name.'_order']);
$v->js(null,$this->owner->js()->reload(array($this->name=>false)))->univ()->closeDialog()->successMessage('New order saved')->execute();
}
}
function processReorder($id_order){
// add missing "ord" fields
$q=$this->model->dsql();
$q->set('ord',$q->expr('id'));
$q->where($q->expr('ord is null'));
$q->do_update();

$q=$this->model->dsql()->field('id')->field('ord');
$seq=$q->do_getAllHash();

// extract ORDs
$ord=array();
foreach($seq as $key=>$val){
$ord[]=$val['ord'];
}

sort($ord);

foreach(explode(',',$id_order) as $id){
$q=$this->model->dsql();
$q->set('ord',array_shift($ord));
$q->where('id',$id);
$q->do_update();
}
}
}

8 changes: 8 additions & 0 deletions gridorder/readme.txt
@@ -0,0 +1,8 @@
This addon allows you to sort records.

1. Create field "ord" in your database table
2. $grid->add('gridorder/Controller_GridOrder');

if you need sorting in grud, then

if($crud->grid)$crud->grid->add('gridorder/Controller_GridOrder');
6 changes: 6 additions & 0 deletions gridorder/templates/view/gridorder.html
@@ -0,0 +1,6 @@
<div id="<?$_name?>">
<?rows?><?row?>
<div data-id="<?$id?>"> <i style="float: left" class="ui-icon ui-icon-arrow-2-n-s"></i> <?$name?></div>
<?/?><?/?>
<div class="hr"></div>
</div>

0 comments on commit 74a1322

Please sign in to comment.