Wednesday, March 31, 2010

Re: Can I do the following with cakephp?

On Apr 1, 12:46 am, Bankai <hgnelso...@gmail.com> wrote:
> - Export results of a database table to excel

Have a look at this article:
http://bakery.cakephp.org/articles/view/csv-helper-php5

Be sure to see the author's updated version here:
http://ifunk.net/cakephp/helpers/csv.php.txt

Finally, here's my updated version:
http://pastebin.com/3bfNQRyC

I made a couple of changes also. I remember I had to play around with
the headers a bit. Same as always when doing this sort of thing.

Here's a quick example of usage:

MembersController:
/**
* Get a CSV dump of the membership
*
* @param void
* @return void
* @access public
*/
public function admin_csv()
{
$this->set('data', $this->Member->getDataDump());
$this->set('filename', 'members_'.date('Y-m-d').'.csv');
Configure::write('debug', 0);
$this->layout = 'ajax';
$this->viewPath = 'elements';
$this->render('csv_dump');
}

Member:
/**
* retrieve Member data for CSV output
*
* @param void
* @return void
* @access public
*/
public function getDataDump()
{
$filters = array(
'conditions' => array('Member.admin' != 1),
'fields' => array(
'Member.id',
'Member.founding',
'Member.first_name',
'Member.last_name',
'Member.organisation',
'User.email',
'Member.address_1',
'Member.address_2',
'Member.city',
'Region.name',
'Country.name',
'Member.postal_code',
'Member.telephone',
'Member.fax'
),
'contain' => array('User', 'Region', 'Country'),
'order' => array('Member.last_name' => 'DESC')
);
$tmp = $this->find('all', $filters);

$data = array();

foreach ($tmp as $key => $row)
{
$data[] = array(
'id' => $row['Member']['id'],
'founding' => ($row['Member']['founding'] ? 'yes' : 'no'),
'first_name' => $row['Member']['first_name'],
'last_name' => $row['Member']['last_name'],
'organisation' => $row['Member']['organisation'],
'email' => $row['User']['email'],
'address_1' => $row['Member']['address_1'],
'address_2' => $row['Member']['address_2'],
'city' => $row['Member']['city'],
'region' => $row['Region']['name'],
'country' => $row['Country']['name'],
'postal_code' => $row['Member']['postal_code'],
'telephone' => $row['Member']['telephone'],
'fax' => $row['Member']['fax']
);
}

return $data;
}

app/views/elements/csv_dump.ctp:

<?php
if (isset($filename)) $csv->setFilename($filename);
$csv->clean_output = true;
$csv->addGrid($data);
echo $csv->render();


> - Have an autosave feature like google docs into my web app

That's a client-side thing. Use AJAX to periodically save your data.

> - Whats the best javascript framework (jquery, mootools, prototype,
> etc) to use along with cakephp

Until recently, Prototype was the only JS framework that Cake had
built-in methods for. But that has changed. I know jQuery is now
supported but I'm not sure of the others. I don't bother using the
helpers, myself, as I've always preferred jQuery (and so was forced
not to use the helpers).


> And now the biggie!
> - Lets say I live in a third world country (which I do), and the
> lights go out every once in a while. Can I develop a program with
> cakephp, where each workstation thats connected to the server (where
> my web app is) save its own copy of the work that they are doing?
> Until that connection resumes to the server? What I want to do is
> avoid loss of data if the lights go out somehow. We know that if we
> are working in a word doc, and the machine unfortunately freezes or
> shuts down for whatever reason, the next time we open MS Word it pops
> out a file recovery dialog, and we know the rest. I want to develop
> something like that with a cakephp in my web app.

The periodic autosave mentioned above is about all i can think of, and
so will only have saved up to n minutes before the power happens to
quit. Once the power dies, there's nothing your workstation can do to
send data to the server. It becomes a doorstop immediately.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

To unsubscribe, reply using "remove me" as the subject.

No comments: