I am modifying an existing plugin and simply added an additional input line in the form as to acquire one additional piece of data from the user.
The existing plug in gathers: Title, Description and Sale Price. I want the poster to be able to add the "Regular Price"
I duplicated the code for the "Sale Price" in /views/default/forms/YYY/save.php and tailored it for "Regular Price" with the following modified code:
$regular_label = elgg_echo('Regular Price')."*";
$regular_input = $currency.elgg_view('input/text', array(
'name' => 'regular',
'id' => 'YYY_regular',
'value' => $vars['regular']
I then similarly modifed /actions/YYY/save.php to add 'regular' to ElggObject with the following code:
$YYY = new ElggObject();
$YYY->subtype = 'YYY';
$new_post = TRUE;
}
$values = array(
'title' => '',
'description' => '', 'sale' =>",
'regular' => '',
'container_guid' => (int)get_input('container_guid'),
);
Finally I modifed /views/defaul/object/YYY.php and added the code:
$body .= "" . elgg_echo('Regular Price') . " {$currency}{$YYY->regular}****" ;
$body .= "";
On the output, I am seeing the entered data as normal for "Title", "Description", and "Sale Price", however for "Regular Price" the output is "Regualr Price $***" not seeing the entered data amount as I do for "Sale Price".
What am I missing? It doesn't seem like Regular Price is getting added to the database.
Thanks in Advance!
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.
You are not collecting / saving the value in your actions. Change the action code as following to get and store the posted data.
Hi Webgalli, thanks for the response. However I *think* I already have that covered in the following array:
$values = array(
'title' => '',
'description' => '',
'sale' =>",
'regular' => '',
'container_guid' => (int)get_input('container_guid'),
);
I tried adding the line you gave me after the above array, but NetBeans gave me an error: "unexpected =>". I even get that error when I remove 'description' =>", from the array.
Should I remove the array all together? If so, how would I make sure all everything get posted?
What are you even doing with the array? You haven't shown us how you are setting/saving the data.
Matt, i guess that is my real question. How do I set/save the data for 'regular'. What command should I be looking for in the existing plugin to save 'regular' along with 'sale'? I have been searching for it but can't seem to figure it out with my limited PHP and Elgg knowledge.
I appreciate the help!
There are 3 things you would need to modify: the input form, the save action and the output view. As I understand it you might have modified the input and output but not the save action. This means that the data that a user entered is simply not saved and therefore you can't see it in the output.
Check out the save action of the plugin (it might not have the name "save.php" but there should be such an action file within the actions folder of the plugin). Within the save action the input is processed and saved. Try to understand how it works and update it accordingly to your needs.
@iionly. Thanks for the help. Which elgg command should I be searching for that procceses the input?
Also, do I need to restart the apache, elgg, MySQL services after making these types of changes to an existing and active plugin?
I don't think there will be any specific command or function in the action file that you need to look for. Rather you would need to look how the already existing input fields are processed. I would expect there's first something like
$value = get_input(a_value);
There will also be some code that will determine which entity the values should be added to, for example something like
$guid = get_input(guid);
$entity = get_entity($guid);
Then you could save your input value to this entity:
$entity->value = $value;
But I can't say exactly how the action file will look like. Best advice: first find the action file and then try to understand what each line of code does.
There shouldn't be any need to restart Apache, Mysql or something else. But if you have the caching enabled on your site (simple cache etc.) you might need to flush the cache for modifcations in the plugin files to be taken into account.
@iionly
OK the good news is that I have been looking for the right things in the right locations. The bad news is my limited PHP/Elgg skills are preventing me from making heads or tails of what I am missing. Any chance I can send you the code so you could help me solve this riddle? I am happy to pay $ for your time. LMK, thanks!
Sorry, but I'm only a hobby Elgg coder not interested in dealing with custom jobs (paid or not). If the plugin you want to modify is free (i.e. you are allowed to post the action file code without violating the license!!!), you could post the relevant code here and I might be able to help. But that's not a promise as I can't say for sure that I know what would be to change nor if a single file alone is enough to be modified.
You shouldn't be too impatient when you just start with Elgg and/or PHP. It's not a matter of minutes to understand the code. I would suggest to be patient and really try to understand what is going on. If you can post the code I can take a look but in the long run it's better to learn how to modify at least little things on your own.
@iionly, I appreciate the advise and trust me when I say I have reached the limit of my programming talent. I have been trying to get through this bump in the road for more than a week before I came here for some advise. Mostly, because like you said I knew long term it would be good for me to learn this myself, but alas I cannot figure it out. I have done a ton of customization of the plugin myself already but it when I try to do anything that has to do with saving input data to the database, I am at a loss.
If you or anyone else in this community could help this newbie out, I would be greatly appreciative! Here is my modified code for the save.php that supposed to save the input data. The ONLY change I made is adding the 'regular' line to be similar to the others in the array. The input save.php code is on my original post:
<?php
// start a new sticky form session in case of failure
elgg_make_sticky_form('sbuy');
// store errors to pass along
$error = FALSE;
$error_forward_url = REFERER;
$user = elgg_get_logged_in_user_entity();
// edit or create a new entity
$sbuy_id = get_input('guid');
if ($sbuy_id) {
$sbuy = get_entity($sbuy_id);
if (!(elgg_instanceof($sbuy, 'object', 'sbuy') && $sbuy->canEdit()&&(wi_get_entity_status($sbuy->getGUID())=="active"||wi_get_entity_status($sbuy->getGUID())=="expired"))) {
register_error(elgg_echo('sbuys:error:post_not_found'));
forward(get_input('forward', REFERER));
}
// save some data for revisions once we save the new edit
//$revision_text = $sbuy->description;
$new_post = false;
} else {
$sbuy = new ElggObject();
$sbuy->subtype = 'sbuy';
$new_post = TRUE;
}
// set defaults and required values.
$values = array(
'title' => '',
'description' => '',
'sale' => '',
'regular' => '',
'access_id' => ACCESS_PUBLIC,
'container_guid' => (int)get_input('container_guid'),
);
// fail if a required entity isn't set
$required = array('title', 'description', ''sale', 'regular');
$budget = false;
// load from POST and do sanity and access checking
foreach ($values as $name => $default) {
$value = get_input($name, $default);
if (in_array($name, $required) && empty($value)) {
$error = elgg_echo("sbuys:error:missing:$name");
}
if ($error) {
break;
}
switch ($name) {
case 'tags':
if ($value) {
$values[$name] = string_to_tag_array($value);
} else {
unset ($values[$name]);
}
break;
case 'container_guid':
// this can't be empty or saving the base entity fails
if (!empty($value)) {
if (can_write_to_container($user->getGUID(), $value)) {
$values[$name] = $value;
} else {
$error = elgg_echo("sbuys:error:cannot_write_to_container");
}
} else {
unset($values[$name]);
}
break;
case 'sbuycategory':
$sbuycategory_guids = $value;
unset ($values[$name]);
break;
case 'budget':
if (is_numeric($value)) {
$values[$name] = $value;
$budget = true;
}
elseif (strlen($value)>0){
$error = elgg_echo("sbuys:error:budget_num");
unset ($values[$name]);
}
else {
}
break;
case 'budget_type':
if ($budget) {
$values[$name] = $value;
}
break;
// don't try to set the guid
case 'guid':
unset($values['guid']);
break;
case 'expiry_date':
if (wi_check_date_not_past($value) && wi_check_date_format($value)) {
$values[$name] = $value;
} else {
$error = elgg_echo("sbuys:error:expiry_date_early");
}
break;
default:
$values[$name] = $value;
break;
}
}
if ((!empty($_FILES['upload']['name']))) {
//check if error firled is set, means image size is bigger than upload_file_size set in cli php.ini
if ($_FILES['upload']['error']) {
if ($_FILES['upload']['error'] == 1) {
$error = elgg_echo('sbuy:image_mem');
} else {
$error = elgg_echo('sbuy:unk_error');
}
}
if(!$error){
$mime = $_FILES['upload']['type'];
// must be an image
if (!wi_upload_check_format($mime)) {
$error = elgg_echo('sbuy:not_image');
}
}
}
// assign values to the entity, stopping on error.
if (!$error) {
foreach ($values as $name => $value) {
if (FALSE === ($sbuy->$name = $value)) {
$error = elgg_echo('sbuys:error:cannot_save' . "$name=$value");
break;
}
}
}
// only try to save base entity if no errors
if (!$error) {
$sbuy_id = $sbuy->save();
if ($sbuy_id) {
// Now see if we have a file icon
if ((isset($_FILES['upload']['name'])) && (substr_count($_FILES['upload']['type'],'image/'))) {
$prefix = "sbuy/".$sbuy_id;
$filehandler = new ElggFile();
$filehandler->owner_guid = elgg_get_logged_in_user_guid();
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('upload'));
$filehandler->close();
$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),25,25, true);
$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),70,70, true);
$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),153,153, true);
$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(),200,200, false);
if ($thumbtiny) {
$thumb = new ElggFile();
$thumb->owner_guid = elgg_get_logged_in_user_guid();
$thumb->setMimeType('image/jpeg');
$thumb->setFilename($prefix."tiny.jpg");
$thumb->open("write");
$thumb->write($thumbtiny);
$thumb->close();
$thumb->setFilename($prefix."small.jpg");
$thumb->open("write");
$thumb->write($thumbsmall);
$thumb->close();
$thumb->setFilename($prefix."medium.jpg");
$thumb->open("write");
$thumb->write($thumbmedium);
$thumb->close();
$thumb->setFilename($prefix."large.jpg");
$thumb->open("write");
$thumb->write($thumblarge);
$thumb->close();
}
}
//add relationship between user and sbuy
if(!wi_remove_entities_relationship("expired_sbuy", $user, $sbuy)){
register_error(elgg_echo('sbuys:save:failure'));
forward(REFERER);
}
//add relationship between user and sbuy
if(!wi_add_entities_relationship("active_sbuy", $user, $sbuy)){
register_error(elgg_echo('sbuys:save:failure'));
forward(REFERER);
}
// remove sticky form entries
elgg_clear_sticky_form('sbuy');
if(!wi_save_entity_category("sbuy", $sbuy->guid, $sbuycategory_guids, "sbuy")){
register_error(elgg_echo('sbuys:save:failure'));
forward(REFERER);
}
// Add to river
add_to_river('river/object/sbuy/create','create',elgg_get_logged_in_user_guid(),$sbuy_id);
if($new_post){
$forward = "added";
}
else{
$forward = "edited";
}
forward("auction/confirm/".$sbuy_id."/".$forward);
} else {
register_error(elgg_echo('sbuys:save:failure'));
forward($error_forward_url);
}
} else {
register_error($error);
forward($error_forward_url);
}
- Previous
- 1
- 2
- Next
You must log in to post replies.