Triggers and file inclusion

A place for users and developers of the Xataface to discuss and receive support.

Postby cls » Sun Sep 17, 2006 3:28 pm

Hi all,

I created a trigger to resize image files after save/insert/update in a table. Ok, the resizing works good, but if i put the resize functions in an external file (to be available also to other tables/triggers and mode generally to the entire application) the file is not included, but the page after the form submit, simply displays the php code.

If I put the functions directly in the table.php file it works. I don't want to put duplicate functions in every trigger file, so I ask if it is possible to define something like this?

display();
?>


Thanks in advance,
Claudio
cls
 
Posts: 15
Joined: Wed Dec 31, 1969 5:00 pm

Postby cls » Sun Sep 17, 2006 3:29 pm

sorry for the code:

[PHP]
require_once '../../_src/dataface/dataface-public-api.php';
include("functions/functions.inc");
df_init(__FILE__, 'http://localhost/_src/dataface');
$app =& Dataface_Application::getInstance();
$app->display();
[PHP/]
cls
 
Posts: 15
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Sun Sep 17, 2006 10:02 pm

Are you talking about implementing an afterInsert() function in the functions.inc file and having that get picked up afterInsert for all of the tables?

This will not work, but you can play on PHP's object inheritance ability to do this elegantly.

Create a base class that implements the afterInsert() method how you like. Then make your delegate classes extend from this class.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby cls » Mon Sep 18, 2006 3:53 am

> Are you talking about implementing an afterInsert() function in the
> functions.inc file and having that get picked up afterInsert
> for all of the tables?

I would like to implement an afterInsert() only for the tables containing images I need to manipulate.
These functions do an image resize and a thumbnail creation. I want to define these functions somewhere in my app (eg in the root of the app), and call them every time I need them. The image manipulation functions should be available to the various table triggers, eg:

tables/table1/table1.php

[PHP]
include("functions/resize-functions.inc");
function table1_resize(){ // this func pick up the form data and call the resize func
$directory = getcwd() . "/tables/table1/image";
$fileName = $_FILES["image"]["name"];
resizeImage($fileName,$directory,90); // this is defined in functions.inc
}

class tables_table1 {
function afterSave(&$record){
table1_resize();
}
}
[/PHP]

tables/table2/table2.php

[PHP]
include("functions/resize-functions.inc");
function table2_resize(){
$directory = getcwd() . "/tables/table2/image";
$fileName = $_FILES["image"]["name"];
resizeImage($fileName,$directory,90);
}

class tables_table2 {
function afterSave(&$record){
table2_resize();
}
}
[/PHP]

> ... but you can play on PHP's object inheritance
> ability to do this elegantly.

> Create a base class that implements the afterInsert()
> method how you like. Then make your delegate classes
> extend from this class.

Sorry, I'm not very expert with PHP classes and objects.
Is there an example somewhere? I have to look here?
http://framework.weblite.ca/documentation/tutorial/getting_started/delegate_classes

Thanks,
Claudio
cls
 
Posts: 15
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Mon Sep 18, 2006 9:12 am

There are many ways to do this. By the looks of it, all of your table_resize() functions are the same, except that they save the files to a different directory. So you just need to dynamically get the table name.

The following example will work.

Create a base class for your tables that have images. You can place this base class anywhwere, and call it anything, but for this example let's call it ImagesBaseClass and place it in the tables folder:

tables/ImagesBaseClass.php :

Code: Select all
import('functions/resize-functions.inc');
class tables_ImagesBaseClass {
    function afterSave(&$record){
        $imageField =& $record->_table->getField('image');
            // gets the field definition of the image field for the current table.
        $savepath = $imageField['savepath'];
            // path to the directory where images are saved for this field.
        if ( !$record->val('image') ) return;
            // Note that if no image is set in this record, we don't need to resize anything
       
       
        if ( isset($_FILES['image']['name']) ){
            // A new image has just been uploaded, so we resize it.
           
            resizeImage($record->val('image'), $savepath, 90);
                // We use $record->val('image') instead of $_FILES['image']['name']
                // because Dataface may have had to rename it on upload to resolve
                // conflicts with existing files.
        }
       
    }

}


Then your delegate classes could extend this class as follows:

tables/table1/table1.php:

Code: Select all
import('tables/ImagesBaseClass.php');
class tables_table1 extends tables_ImagesBaseClass {}


tables/table2/table2.php:
Code: Select all
import('tables/ImagesBaseClass.php');
class tables_table2 extends tables_ImagesBaseClass {}
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby cls » Mon Sep 18, 2006 4:01 pm

It works very fine :)
Thanks a lot Steve!!
cls
 
Posts: 15
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 28 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved