Sunday, April 4, 2010

Having trouble saving data to db - stuck!!

Hi guys,

I'm having some trouble saving data to my database.

Normally I would use the standard method and use something like:

if (!empty($this->data))
{
$this->data['User']['user_id'] = $this->userId;
$this->{$this->postType}->Photo->create();
if ($this->{$this->postType}->Photo->save($this->data))
{
$this->Redirect->flashSuccess("Photo Saved.", array('controller'
=> 'client_albums', 'action' => 'view', $this->data['Photo']
['parent_id']));
}
else
{
$this->Redirect->flashWarning("Photo could not be saved. Please
try again.");
}


The issue is that I'm getting the data back from a jquery uploader
called uploadify so $this->data is not set.

I'm a little bit new to CakePHP still so I'm hoping someone could help
out?
I've pasted my code below - the relevant function is admin_upload.

Many thanks,
Michael

<?php
App::import('Controller', 'Posts');
class ClientAlbumsController extends PostsController {
var $name = 'ClientAlbums';
var $postType = 'ClientAlbum';

function beforeFilter() {


if ($this->action == 'admin_upload') {
//$this->output->set_header("HTTP/1.0 200
OK"); //echo "here";
//exit();
$this->Session->id($this->params['pass'][0]);
/*
$testid2 = $this->params['url']['testid'];
$ourFileName2 = "gimp2.txt";
$fh2 = fopen($ourFileName2, 'a') or die("can't open file");
fwrite($fh2, $testid2);
fclose($fh2);
*/
$this->Session->start();
//debug($this->Session->read());
}

parent::beforeFilter();

}

function admin_index()
{
$this->Redirect->urlToNamed();
$this->pageTitle = $this->name;
$conditions = isset($this->params['named']['tag']) ? $this-
>habtmConditions($this->params['named']['tag'],
$this->postType, 'Tag') : $this->searchConditions($this->params);
$this->{$this->postType}->recursive = 2;
//$photos = $this->Album->Photo->find('first');
//pr($postvar);
//$this->set('photos',$photos);
$this->set('posts', $this->paginate($this->postType, $conditions));
$this->set('controller',$this->name);

}

function admin_view($id = null)
{
parent::admin_view($id);

//set the id to the layout
$this->set('id_for_layout',$id);

// form uploaded
if (!empty($this->data))
{
// if cancel button pressed
if(isset($_POST['cancel'])) {
// redirect
$this->redirect(array('action'=>'index'));
}

// init
$this->data['User']['user_id'] = $this->userId;
//$this->{$this->postType}->Photo->create();

// need to find the id of the album and create a folder named after
the client-id/album-id

// find title of the album
$albumObject = $this->{$this->postType}->findbyId($id);
$clientId = $albumObject['ClientAlbum']['client_id'];

// create folder
$this->create_folder("/files/{$clientId}/{$id}");

// get uploaded files
$files = $this->_get_uploaded_files();
pr($files);
exit();

// loop through files
if(!empty($files)) {
foreach($files as $f) {
// copy file from tmp dir to album dir
$source = DS.'tmp'.DS.$f;
$dest = DS.'files'.DS.$clientId.DS.$id;
$fileOK = @copy(WWW_ROOT.$source,WWW_ROOT.$dest);
// delete file
@unlink(WWW_ROOT.$source);

// copy successful
if($fileOK) {
// init
$this->{$this->postType}->Photo->create();
// build data
$data = array(
'parent_id'=>$id,
'url'=>str_replace('\\','/',$dest)
);
// save contender item
$this->{$this->postType}->Photo->save(array('Photo'=>$data));
// resize image
$this->resize_image($dest,$this->cache_width);
}
}
}


/*
// if the photo saves successfully
if ($this->{$this->postType}->Photo->save($this->data))
{
$this->Redirect->flashSuccess("Photo Saved.", array('controller' =>
'client_albums', 'action' => 'view',
$this->data['Photo']['parent_id']));
}
// if there's a problem saving show the error
else
{
$this->Redirect->flashWarning("Photo could not be saved. Please try
again.");
}
*/
}

// set tooltips
$this->set('tooltips',TRUE);
// set swfupload
$this->set('swfupload',TRUE);

//$this->_lists();
}

function admin_add() {
$this->pageTitle = 'New Post';
if (!empty($this->data))
{
$this->data[$this->postType]['user_id'] = $this->userId;
$this->{$this->postType}->create();
if ($this->{$this->postType}->save($this->data))
{
$this->Redirect->flashSuccess("{$this->postType} Saved.",
array('action' => 'view', $this->{$this->postType}->id));
}
else
{
$this->Redirect->flashWarning("{$this->postType} could not be
saved. Please try again.");
}
}
$this->_lists();
}

function _lists()
{
$clients = $this->{$this->postType}->User->find('list',
array('conditions' => array('User.group_id' => '2')));
$this->set(compact('clients'));
$statuses = $this->{$this->postType}->Status->find('list');
$this->set(compact('statuses'));
}

/**
* Deals with file upload
*/
function admin_upload() {

$ourFileName = "gimp.txt";
$fh = fopen($ourFileName, 'a') or die("can't open file");
//fwrite($fh, $stringData);
//fclose($fh);

// upload file to tmp dir
echo move_uploaded_file($_FILES['Filedata']
['tmp_name'],WWW_ROOT.DS.'tmp'.DS.$_FILES['Filedata']['name']);

// find title of the album
$albumId = $this->params['url']['testid'];
$albumObject = $this->{$this->postType}->findbyId($albumId);
$clientId = $albumObject['ClientAlbum']['client_id'];

// create folder
$this->create_folder("/files/{$clientId}/{$albumId}");

// get uploaded files
$files = $this->_get_uploaded_files();
foreach($files as $f) {
$source = 'tmp'.DS.$f;
$dest = 'files'.DS.$clientId.DS.$albumId.DS.$_FILES['Filedata']
['name'];

//fwrite($fh, $source);
//fwrite($fh, "\n");
//fwrite($fh, $dest);
//fwrite($fh, "\n");
//fwrite($fh, "Command: " . WWW_ROOT.$source . "," . WWW_ROOT.
$dest . "\n");

$fileOK = @copy(WWW_ROOT.$source,WWW_ROOT.$dest);
// delete file
@unlink(WWW_ROOT.$source);

// copy successful
if($fileOK) {
// init
$this->data[$this->postType]['Photo']['title'] =
$_FILES['Filedata']['name'];
$this->data[$this->postType]['Photo']['type'] = 'Photo';
$this->data[$this->postType]['Photo']['attachmentName'] =
$_FILES['Filedata']['name'];
$this->data[$this->postType]['attachmentLocation'] = 'files';
$this->data[$this->postType]['parent_id'] = $albumId;
$this->{$this->postType}->Photo->create();

//$model->data[$model->name][$fields['attachmentType']] =
$attachment['type'];
$this->{$this->postType}->Photo->set($this->data);

/*
Trying to save the data array to a file to figure out whats going
on!
ob_start();
print_r($myarray);
$var = ob_get_contents();
ob_end_clean();
*/

fwrite($fh,$this->data[$this->postType]['Photo']['title']);
fwrite($fh, "\n");
fwrite($fh,$this->data[$this->postType]['Photo']['type']);
fwrite($fh, "\n");
fwrite($fh,$this->data[$this->postType]['Photo']
['attachmentName']);
fwrite($fh, "\n");
fwrite($fh,$this->data[$this->postType]['attachmentLocation']);
fwrite($fh, "\n");
fwrite($fh,$this->data[$this->postType]['parent_id']);
//fclose($fp);


// save photo item
$this->{$this->postType}->Photo->save($this->data);
// resize image
//$this->resize_image($dest,$this->cache_width);
}


}
fclose($fh);
}


/**
* Get a list of files in tmp dir
*/
function _get_uploaded_files($id = null) {
// ignore these
$ignore = array('.','..','.svn','.DS_Store');
// init
$dir = WWW_ROOT.DS.'tmp';


// if id is set
if($id != null) {
$dir .= DS.$id;
}

// get files
$files = @scandir($dir);
// if files found
if(!empty($files)) {
// loop and check
foreach($files as $k=>$f) {
if(in_array($f,$ignore)) {
unset($files[$k]);
}
}
}
//pr($files);
return $files;
}

}
?>

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: