Archive

Posts Tagged ‘symfony’

Flash Percent Uploader with Symfony!

August 22nd, 2008

Flash Percent Uploader with Symfony!

I just had to upload 40+mb of video file sizes to one of my recent project, For this purpose using simple form is obviously not a good choice as user never knows how much data has been transferred and how much left. So percent uploader was inevitable here, for this purpose flash offers fileReference class which can be used to upload mutliple files to the server, with this filereference class you can get file size at onSelect event, so you can apply max-upload file size check and as well as you can monitor file transfer with onProgress event. The only problem while implementing the flash percent uploader is that you may recieve message like “A flash script is causing you computer to get slow! Would you like to abort it?”. The best way to avoid this error message is to make flash script busy in doing something else while uploading, it can be as simple as incrementing tmp variable.
Also, you will definitely love to increase upload limit at server, For PHP you will change the following variables:
upload_max_filesize = 50M
post_max_size = 50M

Now it was as simple as it is for the non-symfony apps, The problem of using this with symfony app is that upload() method of fileReference class cannot initiate an action of the symfony application. On calling the action it does nothing, It does not even gives HTTP error 404 or anything like that, means it founds the path to the action (note that upload method returns true), but the action cannot communicate with it. So to overcome this problem, we will write our upload script in a file under web directory and we can call that script directly from the upload() method. like:-
var item:FileReference = new FileReference();
path_to_upload_script = “……/uploader.php”;
item.upload(path_to_upload_script); // note that the parameter path should be absolute

Now you may require to do some database activity, may need to create some criteria and doSelects, wondering if thats possible from the script in the web directory? Then yes its possible, instead using:
sfContext::getInstance()->getController()->dispatch(); // that is used in index.php, we don’t need to dispatch the controller otherwise it will goto default homepage of our symfony app. We just need to get sfContext Instance. like:
sfContext::getInstance();

Now you are ready to go, you can call the script and can communicate with the database as well.

Share/Save/Bookmark

Jawad Khan Flash Actionscript, symfony , , , ,

Using Flash with Symfony!

July 21st, 2008

well being a flash developer and being a symfony developer feels really good :) .. Anyways, Symfony is awsome framework for developing today’s web-applications with its nice and fine MVC architecture. So symfony developers and flash developers should know how they will work together seamlessly.
First thing is to embed the flash into your template, we know that all the assets, graphics, javascripts, css etc belongs to the web folder of symfony’s project directory structure. So flash files will go here as well. I will recommend to create a folder named ‘flash’ .

to embed the flash in your template, you will use same <object> tag as done in the normal html files, but ofcourse you will give the relative or absolute url of your flash src, for this purpose we can use functions like _compute_public_path(…) in symfony. it will take flash file’s parameters, like its name, extension and folder. this function returns the path which will be used for the embed source.

Now we are done with embeding, and now we will focus on data communication b/w flash and symfony, guess what its even more easier..  The right way to communicate data is in xml format. e.g. if my flash movie requires usernames and birthdays for all the members of my site, I will create an action specifically for the flash component, let’s say executeGetBirthdays(), i will create the template against the action as well.. Now i will put the xml generation logic in my action and i will print the resultant xml in my templateSuccess.php. Now I want the template to not to display irrelevant header and other info and just needs to start with the xml document root tag, so that my flash component can easily parse it. Therefore, I will put following line in the action:
$this->setLayout(false);

Calling a symfony action from the flash is simple, just create an instance from the XML class..i.e
x1 = new XML();//xml class rather
x1.onLoad = function()
{
// xml parsing and rest of the stuff
}

x1.load(’getBirthDays’);

Share/Save/Bookmark

Jawad Khan Flash Actionscript, symfony , , , ,

Using sfCryptographp with custom validation

July 18th, 2008

Using sfCryptographp with custom validation..

I just had to use captcha with symfony, Symfony is a great MVC framework, but i really don’t like its default validation method, rather I like javascript validations, so that user don’t have to wait for the server response after missing just a text field. So, I put mostly fields checking validations with javascript and then check at the server for the specific fields which do require server response like checking username if its unique in the database. Such validations are also possible with the quick response using Ajax, but all we need to do is to avoid symfony’s default validation method in order to achieve that.

Now coming back to captcha, the php-captha is a great library for the normal php apps, It can also be used with symfony, but I gave preference to symfony’s already available pluggins..
so here is the pluggin: sfCryptographp
its installation is pretty straight forward.

$ symfony plugin-install http://plugins.symfony-project.com/sfCryptographpPlugin

After your done with installation, enable the crypt pluggin in your settings.yml file

like:

.settings:
enabled_modules:        [default, cryptographp]

Now, Open your template file, add following line:
<?php use_helper(’Cryptographp’); ?>

Now insert following code in anywhere within the form where you want captcha to be visible.

<?php echo cryptographp_picture(); ?>
<?php echo cryptographp_reload(); ?>

<?php echo input_tag(’captcha’); ?>

It was pretty easy, now if u test your template you can see captcha images, The above code lines are same as described in sfCryptographp wiki page. Now for the validation we will not follow the wiki, we will put some logic at the actions file. before we do it we need little modification in sfCryptographpValidator class, which can be found at:

plugins\sfCryptographpPlugin\lib\validator\sfCryptographpValidator.class.php

open that class

add another function like:

public function checkCode($value)
{
if (chk_crypt($value)) {
return true;
} else {
return false;
}

}

Now goto your actions class, put the following code at the place where you are handling form post actions.

// create an instance from sfCryptographpValidator class
// and use the new function to validate user entered text

$captcha = new sfCryptographpValidator();

if (!$captcha->checkCode($_POST['captcha']))
{
//user entered wrong code…
}

Share/Save/Bookmark

Jawad Khan PHP, symfony , ,