I need help getting description field ( called "caption" in my form) added to the form to be saved in action. This is for Tidypics plugin. By default you upload image first and then go add titles/descriptions to photos.
I have new form and action files (edited originals) that work fine in uploading photo but the description/caption field is not getting saved. I have an idea which part could be the problem and needs bit of editing to make it work, maybe someone with good "Elgg eye" can spot the problem and offer suggestion.
Here is the form code:
<?php
/**
* Basic uploader form
*
* This only handled uploading the images. Editing the titles and descriptions
* are handled with the edit forms.
*
* @uses $vars['entity']
*
* @author Cash Costello
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
*/
$album = $vars['entity'];
$access_id = $album->access_id;
$maxfilesize = (float) elgg_get_plugin_setting('maxfilesize', 'tidypics');
$instructions = elgg_echo("tidypics:uploader:upload");
$max = elgg_echo('tidypics:uploader:basic', array($maxfilesize));
//$title = elgg_extract('title', $vars, '');//THIS IS MY DESCRIPTION/CAPTION
$captions = elgg_extract('caption', $vars, '');
$list = '';
for ($x = 0; $x < 10; $x++) {
$list .= . elgg_view('input/file', array('name' => 'images[]')) . '<div><label>' . elgg_echo('caption') . '</label>' . elgg_view('input/longtext', array('name' => 'caption', 'value' => $captions,)) . '</div></div>';
}
$foot = elgg_view('input/hidden', array('name' => 'guid', 'value' => $album->getGUID()));
$foot .= elgg_view('input/submit', array('value' => elgg_echo("Upload")));
$form_body = <<<HTML
<div>
$list
</div>
<div class='elgg-foot'>
$foot
</div>
HTML;
echo elgg_view('input/form', array(
'body' => $form_body,
'action' => 'action/photos/image/upload',
'enctype' => 'multipart/form-data',
));
?>
Here is the action code ( I think problem is that save($data) is wrapped in foreach ($_FILES['images']['name'] so it only saves image url/file):
elgg_load_library('tidypics:upload');
$img_river_view = elgg_get_plugin_setting('img_river_view', 'tidypics');
$titles = get_input('title');//THIS IS THE DESCRIPTION/CAPTION INPUT FROM THE FORM
$captions = get_input('caption');
// test to make sure at least 1 image was selected by user
$num_images = 0;
foreach($_FILES['images']['name'] as $name) {
if (!empty($name)) {
$num_images++;
}
}
if ($num_images == 0) {
// have user try again
register_error(elgg_echo('tidypics:noimages'));
forward(REFERER);
}
// create the image object for each upload
$uploaded_images = array();
$not_uploaded = array();
$error_msgs = array();//THIS I THINK IS THE PROBLEM AS BELOW SAYS $result = $image->save($data);
foreach ($_FILES['images']['name'] as $index => $value) {
$data = array();
foreach ($_FILES['images'] as $key => $values) {
$data[$key] = $values[$index];
}
if (empty($data['name'])) {
continue;
}
//HERE I'M TRYING TO ADD CAPTION FIELD DATA TO BE ADDED TO $data
$captions = get_input('caption');
foreach ($_FILES['caption']['name'] as $keys => $caption) {
$capt[$keys] = $captions[$index];
}
$mime = tp_upload_get_mimetype($data['name']);
$image = new TidypicsImage();
$image->container_guid = $album->getGUID();
$image->setMimeType($mime);
$image->access_id = $album->access_id;
//$image->title = $imgtitle;
//$image->description = $captions; - NOT WORKING
try {
$result = $image->save($data); // THIS IS SAVING IMAGE $data WHICH I THINK IS ONLY SAVING $_FILES['images']['name'] DATA
} catch (Exception $e) {
array_push($not_uploaded, $data['name']);
array_push($error_msgs, $e->getMessage());
}
if ($result) {
array_push($uploaded_images, $image->getGUID());
$captions = get_input('caption');
$guids = $image->getGUID();
foreach ($guids as $key => $guid) {
$image = get_entity($guid);
// set description appropriately
$image->description = $captions[$key];
//$image->tags = string_to_tag_array($tags[$key]);
}
$image->description = $captions;
$image->save();
if ($img_river_view == "all") {
add_to_river('river/object/image/create', 'create', $image->getOwnerGUID(), $image->getGUID());
}
}
}
if (count($uploaded_images)) {
// Create a new batch object to contain these photos
$batch = new ElggObject();
$batch->subtype = "tidypics_batch";
$batch->access_id = $album->access_id;
$batch->container_guid = $album->getGUID();
if ($batch->save()) {
foreach ($uploaded_images as $uploaded_guid) {
add_entity_relationship($uploaded_guid, "belongs_to_batch", $batch->getGUID());
}
}
$album->prependImageList($uploaded_images);
// "added images to album" river
if ($img_river_view == "batch" && $album->new_album == false) {
add_to_river('river/object/tidypics_batch/create', 'create', $batch->getOwnerGUID(), $batch->getGUID());
}
// "created album" river
if ($album->new_album) {
$album->new_album = false;
$album->first_upload = true;
add_to_river('river/object/album/create', 'create', $album->getOwnerGUID(), $album->getGUID());
// "created album" notifications
// we throw the notification manually here so users are not told about the new album until
// there are at least a few photos in it
if ($album->shouldNotify()) {
object_notifications('create', 'object', $album);
$album->last_notified = time();
}
} else {
// "added image to album" notifications
if ($album->first_upload) {
$album->first_upload = false;
}
if ($album->shouldNotify()) {
object_notifications('create', 'object', $album);
$album->last_notified = time();
}
}
}
if (count($not_uploaded) > 0) {
if (count($uploaded_images) > 0) {
$error = sprintf(elgg_echo("tidypics:partialuploadfailure"), count($not_uploaded), count($not_uploaded) + count($uploaded_images)) . '<br />';
} else {
$error = elgg_echo("tidypics:completeuploadfailure") . '<br />';
}
$num_failures = count($not_uploaded);
for ($i = 0; $i < $num_failures; $i++) {
$error .= "{$not_uploaded[$i]}: {$error_msgs[$i]} <br />";
}
register_error($error);
if (count($uploaded_images) == 0) {
//upload failed, so forward to previous page
forward(REFERER);
} else {
// some images did upload so we fall through
}
} else {
system_message(elgg_echo('tidypics:upl_success'));
}
forward(REFERER);
//forward("photos/edit/$batch->guid");
Please give me suggestions, anything :)
Thanks.
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by Raül Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.