Showing posts with label cakePHP. Show all posts
Showing posts with label cakePHP. Show all posts

Wednesday, August 10, 2011

A JQuery upload plugin with cakePHP

Those who had been developing for a while since the early days of Ajax would agree that building a progress bar for file uploads is not trivial.  Here I would like to introduce jQuery File Upload plugin

It has an example which actually works out-of-the-box.  But for integration, although not really cake-y, we actually need to create and modify some files here and there.

Note that there can be many different implementations. I start by creating a UploadComponent object. This is a modified version of the examples/upload.php file. I replace the __construct() function with initialize()

function initialize($controller, $settings = array(), $options=null ) {
$this->controller =& $controller;
...
In the controller that calls the UploadComponent, we have a upload() function that handles the logic. It is also part of the examples/upload.php file. The basic JQuery File Upload plugin has the ability to Add, View and Delete files, therefore we will need POST, GET, and DELETE request detection.

class MyController extends AppController {
$var components = array('Upload');

...

function upload() {
          $this->layout = 'json';
          Configure::write('debug', 0);
  
          switch ($_SERVER['REQUEST_METHOD']) {
              case 'HEAD':
              case 'GET':
                  $this->Upload->get();
                  break;
              case 'POST':
                  $this->Upload->post();
                  break;
              case 'DELETE':
                  $this->Upload->delete();
                  break;
              default:
                  header('HTTP/1.0 405 Method Not Allowed');
          }
}

The view code is the most straightforward part. It is the modified version of the example/index.html file. You can embed the code into any existing view you have. Some required jquery files are the jQuery v. 1.6+ for the prop function, jQuery UI v. 1.8+, and jQuery Templates plugin v. 1.0+ although in the JQuery File Upload page, it is listed as optional. If you do not include this file you will encounter the error $.tmpl is missing in your browser console. (If you have Firebug. But I assume you all do since people who read this most probably are developers anyway).

Thursday, October 22, 2009

FB Connect and cakePHP

When building a website using cakePHP and FB Connect it is important to take note of the cakePHP routing.  If you have done some customizing in your cakephp routes.php file, it may cause some problems when you try to use the FB Connect login-button.

To be safe, turn off all routing for cakePHP when it accesses the xd_receiver.htm file.

/** 
* turn off cakephp routing for facebook connect xd_receiver
*/
Router::connect('/xd_receiver.htm', null);

Wednesday, September 09, 2009

Fixing default cakePHP pagination in FBML

CakePHP pagination generates relative URLs that look like

/posts/view/page:5

when you use

$paginator->prev()
$paginator->numbers()
$paginator->next()

This doesn't work in FBML, so you need to sort of hack the PaginatorHelper to display it as

http://apps.facebook.com/someapp/posts/view/page:5

So you change function link() to

function link($title, $url = array(), $options = array()) {
        $options = array_merge(array('model' => null, 'escape' => true), $options);
        $model = $options['model'];
        unset($options['model']);

        if (!empty($this->options)) {
            $options = array_merge($this->options, $options);
        }
        if (isset($options['url'])) {
            $url = array_merge((array)$options['url'], (array)$url);
            unset($options['url']);
        }
        if(!isset($_SERVER['HTTP_X_FB_USER_REMOTE_ADDR']) ) {
            $url = $this->url($url, true, $model);
            $obj = isset($options['update']) ? 'Ajax' : 'Html';
            $url = array_merge(array('page' => $this->current($model)), $url);
            $url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin'=>true)));
        } else {
            //if its part of the facebook app, preprend http://apps.facebook.com/dev_zenmanga to the $url
            $obj = isset($options['update']) ? 'Ajax' : 'Html';
            $url = 'http://apps.facebook.com/someapp' . substr(parent::url($url), true);
        }
        return $this->{$obj}->link($title, $url, $options);
    }

$_SERVER['HTTP_X_FB_USER_REMOTE_ADDR']
will print out the server IP that your facebook app is hosted on, so if that is set, then we know we are running on the facebook server.

A better way will be to write in your config/bootstrap.php

if(isset($_SERVER['HTTP_X_FB_USER_REMOTE_ADDR'])){
        Configure::write('App.base', 'http://apps.facebook.com/someapp/');
    }

Thursday, May 29, 2008

Project site for Informix dbo for cakephp 1.2

http://code.google.com/p/dboinformixdriver/

courtesy of jmomorales

Code is for Informix 7.3 but I run Informix 9.40 and its quite fine. Just implemented a fix for pagination.

Tuesday, May 20, 2008

Informix and CakePHP 1.2

Today, I tried some kick-ass stuff. Connecting an existing Informix database with cakePHP 1.2. First thing I tried is to use the adodb db wrapper which is built in cakephp. If you need to use adodb, you have to download it from here. Then you extract it and place it in app/vendor.

var $default = array(
'driver' => 'adodb',
'connect' => 'informix',
'persistent' => false,
'host' => 'myserver',
'port' => '9109',
'login' => 'informix',
'password' => 'mypass',
'database' => 'mydb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);

Somehow, this configuration setting works and cakephp's default homepage now shows that it can connect to a database. However, when I try to create a model, and scaffold a controller, it tells me that the table for the model cannot be found.

So then I scour the cyberspace again, looking for the possibility of the existence of a pdo_informix.php file, and I found it hidden in the cakephp google groups, written by Marcelo of San Luis, Argentina. The link can be found here. You need to change the code a little, to set the INFORMIXSERVER environment and get rid of the error

SQLSTATE=IX 000 SQLCODE=-25596


...
function connect()
{
$config = $this->config;
$connect = $config['connect'];
$this->connected = false;

$host = $config['database'] . '@' . $config['host'];
$argHostname = $config['host'];
if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
putenv("INFORMIXSERVER=".trim($argHostname));
if ($config['persistent'])
{
$this->connection = ifx_pconnect($host, $config['login'], $config['password']);
}
else
{
$this->connection = $connect($host, $config['login'], $config['password']);
}

$this->connected = ($this->connection !== false);
return $this->connected;
}
...


Hopefully, the rest of the code can be recycled. I have not tested them yet.