News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Where does the posting go on?

Started by MLM, February 27, 2011, 04:14:35 PM

Previous topic - Next topic

MLM

I am wanting to add a featured work submission on the post.template. I added the layout but I can not figure out where and how it posts it to he db. I want to add my own MySQL query to insert into my own table.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

Post.php handles processing and sanitisation, before passing it to Subs-Post.php's createPost() function.

MLM

So if i have this on post.template:

<dl id="postAttachment2">
<dt>
Featured Work Submission:
</dt>
<dd class="smalltext">
Image URL: <input name="featured_work_image_url"></input>
</dd>

</dl>


How does it submit to the post.php? as what variable. and then how do i then pass it to subs-post to get put into db. I know how to use the smf db insert but just wondering how to get it there.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

It's submitted as a form to Post.php, in Post2() along with everything that's in the form. You'll see that appear in $_POST['featured_work_image_url'] if it was completed (if not, it will be empty)

What should it contain? Where's the field?

MLM

I dont get your question?

So it gets submitted to Post.php and then to subs-post.php?

Which function should i be putting it in? If i make my own function will it run it?

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

QuoteSo it gets submitted to Post.php and then to subs-post.php?

Yes. Like I said, Post.php will receive it first, in Post2(), it will check the content, sanitise it where necessary. It will throw it back to Post() if there's an error, or it will send it to createPost() in Subs-Post.php

You want help, here's the deal: you answer all the questions I ask, then I will answer yours. Because invariably the reason I'm asking is because the answer to my question will affect the answer to your question. So again, what should it contain, and where's the field going to end up in the database? (Is it per post, per topic, or something else?)

MLM

Sorry, It is going to be a url so i can sanitize it with regex. It is going in a separate table called featured_work_entries.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

In which case, you just need to add it to the table via a query in Post.php, just before the call to createPost() is actually made.

MLM

Is there a way to get the topic id that is being submitted?

What variable should i use to get the member name (not id)?

How do I return back to the message with everything intact if i get an "error" with the featured work?

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

You didn't say you wanted any of that stuff :P

So it's only on a new topic? (You really should be telling me this kind of stuff, you know, like I asked for?)

ascaland

You can retrieve the topic id using the $topic variable.
And since $user_id is accessible here you can easily just use $user_id['name'] (or $user_id['username'] if your looking for the username).

And if an error occurs you return Post2() with Post() using,
return Post();

And (someone correct me if im wrong) you need to set,
$_REQUEST['preview'] = true;
before returning to Post()?

Arantor

QuoteYou can retrieve the topic id using the $topic variable.

Only if there's already been a post and this is explictly a reply to it. If it's a new topic, $topic will not have been populated and will only exist after createPost() has been called, but will be in $topicOptions['id'] at that point. This seems to be the case of a new topic, and as such we can't rely on $topic.

QuoteAnd if an error occurs you return Post2() with Post() using,

Post2 already does this. If there's an issue with the content, you should be adding to $context['post_errors'] I think it is. That way, all content is sanitised first, returned back if there's an error, and after that point everything's known to be good.

This is why I wanted as much detail as possible before trying to help because Post2 alone is 800 lines of complexity, even before you look at the 1k lines of Post() as well, and their interplay is hideously complicated given everything that goes on.

ascaland

Quote from: Arantor on February 27, 2011, 08:28:33 PM
This is why I wanted as much detail as possible before trying to help because Post2 alone is 800 lines of complexity, even before you look at the 1k lines of Post() as well, and their interplay is hideously complicated given everything that goes on.

I agree, quite complicated to look at. Would,
$_REQUEST['preview'] = true;
need to be set by the way?

Arantor

Not if you do it properly and extend $context['post_errors'], which takes care of it all for you.

MLM

So if i wanted to return an error i could do this:
$context['post_errors'] = 'NOT AN IMAGE';

Also my thing is not working. If i put anything in my featured work inputs then it just directs me to some random board and doesnt post the topic.

I have my code above this: (in post.php)
// This is a new topic or an already existing one. Save it.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

QuoteSo if i wanted to return an error i could do this:

No, you have to add to the array, like all the others do, and do so to indicate an entry in $txt, like all the others do.


-sigh- I give up, you can't seem to answer quite simple questions, and I'm only going to get more frustrated by trying to help in this topic.

ascaland

Quote from: MLM on February 27, 2011, 08:45:26 PM
So if i wanted to return an error i could do this:
$context['post_errors'] = 'NOT AN IMAGE';

To return an error you would push a string value into the $post_errors array. The string value is what would be the suffix in the Errors language file. So, to help make things clearer if they arent already. You would push the error message as such,
$post_errors[] = 'posting_error_example';
And in the Errors.{language}.php file you would add,
$txt['error_posting_error_example'] = 'This would be displayed in the red warning box! Dont forget the error_ suffix.';

And the script would take care of the rest.

As for the second part, im not quite understanding but I think it would be easier if you showed us the code your using.

PS: Dont worry Arantor maybe I can take it from here if you need. :)

MLM

Alright i got my error done.

Arantor, why are you getting so frustrated I am understanding all of what you have said so far. Just explain a bit more.


Whenever i add stuff to the featured work inputs and try to post it just brings me to the board like it posted but nothing shows up..

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

#18
Quote from: MLM on February 27, 2011, 09:21:51 PM
Whenever i add stuff to the featured work inputs and try to post it just brings me to the board like it posted but nothing shows up..

In order for it to actually post you need to make sure you have the required form parameters in the script. Nevermind that part, I misunderstood. Can you please go over what your trying to do and post the code that doesnt seem to be working?

MLM


My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

So let me get this straight... Your adding a new field to the post template so when people post they have the option to set this field, which would then get sent to the database am I right?

MLM

post.php (right above this: // This is a new topic or an already existing one. Save it.)

// featured work stuff
$featured_work_image_url = $_POST['featured_work_image_url'];
$featured_work_image_title = $_POST['featured_work_image_title'];
$featured_work_image_creator = $context['username'];

if(!empty($featured_work_image_url))
{
$Regex = '/http://([\w-]+\.)+[\w-]+(/[\w- ./]*)+\.(?:gif|jpg|jpeg|png|bmp|GIF|JPEG|JPG|PNG|BMP|Gif|Jpg|Jpeg|Png|Bmp)$/';

// checking if this is even an image url
if(preg_match($Regex, $featured_work_image_url))
{

// figure out the dimensions...
list($image_width, $image_height, $image_type, $image_attr) = getimagesize($featured_work_image_url);

// making the proportions
if($image_width < 283 && $image_height < 143)
{
}
else
{

$image_ratio = $image_width/$image_height;

if($image_width > $image_height)
{

$image_ratio = $image_width/$image_height;

while ($image_width > 287)
{
$image_width = $image_width - $image_ratio;
$image_height--;
}

// is the image height still too big?
if($image_height > 143)
{
$image_ratio = $image_height/$image_width;

while ($image_height > 143)
{
$image_height = $image_height - $image_ratio;
$image_width--;
}
}
}
else
{

$image_ratio = $image_height/$image_width;

while ($image_height > 143)
{
$image_height = $image_height - $image_ratio;
$image_width--;
}

// is the image width still too big?
if($image_height > 287)
{
$image_ratio = $image_width/$image_height;

while ($image_width > 287)
{
$image_width = $image_width - $image_ratio;
$image_height--;
}
}
}
}

// rounding our values to the nearest pixel
$featured_image_width = round($image_width);
$featured_image_height = round($image_height);






$feature_text = "n";
$featured_work_topic_id = "UNKNOWN";

// insert everything into the db
            mysql_query("INSERT INTO  vp_featured_work_entries (topic_id, feature, title, creator, image, width, height) VALUES('$featured_work_topic_id', '$feature_text', '$featured_work_image_title', '$featured_work_image_creator', '$featured_work_image_url', '$featured_image_width', '$featured_image_height') ") or die(mysql_error());
       
}
else
{
$post_errors[] = 'featured_work_not_image_error';
}


}



post.template.php (right above: // Is visual verification enabled?)

// featured work Submission
echo '
<dl id="postAttachment2">
<dt>
Featured Work Submission:
</dt>
<dd class="smalltext">
<div class="featured_work_post_template_div">Title: <input name="featured_work_image_title"></input></div>
<div class="featured_work_post_template_div">Image URL: <input name="featured_work_image_url"></input></div>
</dd>

</dl>
';

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Ok, you have a couple of things mixed up. Lets start with Post.php.

Your creating the error way too late in the script. You should be creating the error above this snippet,
// Any mistakes?
if (!empty($post_errors))
{
loadLanguage('Errors');
// Previewing.
$_REQUEST['preview'] = true;

$context['post_error'] = array('messages' => array());
foreach ($post_errors as $post_error)
{
$context['post_error'][$post_error] = true;
if ($post_error == 'long_message')
$txt['error_' . $post_error] = sprintf($txt['error_' . $post_error], $modSettings['max_messageLength']);

$context['post_error']['messages'][] = $txt['error_' . $post_error];
}

return Post();
}

Because that is where each error is being checked and if so, returning back to the post template. This being said, your going to have to figure out which codes need to go where. So to do the error checking properly, you must move a part of the snippet higher up in the script.

Next, createPost() from Subs-Post.php is where you should be submitting the post data to the database. Maybe you should take a look at this modification here and see if you can figure it out,
http://custom.simplemachines.org/mods/index.php?mod=349

That should be more than enough to help you.
About your snippet in the Post.template.php file, your input tags should be fixed up a bit. Such as adding the type attribute, etc.

MLM

Thanks a bunch Project Evolution

My error seems to be still working without adding to that array thing up top.

I added my code to the subs-post above creatPost and now it is giving my error that it isn't a image url when i try to post. It is probably just my regex or coding though.

I am going to bed now so wont be able to reply until tomorrow afternoon.

Thanks for all the help so far and if you happen to find the proper regex for a image url, please post.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

MLM

Is there a variable to use so i can get the member name of the person posting for example $context['username']

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Quote from: Project Evolution on February 27, 2011, 08:23:39 PMAnd since $user_id is accessible here you can easily just use $user_id['name'] (or $user_id['username'] if your looking for the username).

Arantor

There is no such variable in SMF, look in $context['user']['id'] instead

MLM

$user_id['username'] does not seem to be working. Perhaps i need to call a global although it isnt it a function (just above the creatPost())

Also if i want to make sure it doesn't submit to db if the message/ thread/ post has errors how do i check and get all possible errors before submitting it.

Could i put my code underneath the creatPost function and if($post_errors) around my code.

Also if below could i get the topic id that is being submitted?

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Oops, I meant to say $user_info. Ive been thinking $user_id exists lately. :P

MLM

$user_info['username'] worked great thanks Project Evolution and Arantor!

Any info on other problems/ questions?

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

Arantor

QuoteAlso if i want to make sure it doesn't submit to db if the message/ thread/ post has errors how do i check and get all possible errors before submitting it.

Could i put my code underneath the creatPost function and if($post_errors) around my code.

Also if below could i get the topic id that is being submitted?

No, you can't do that, you HAVE to do it sooner than that like I already pointed out, putting all tests before return Post() in the case of errors.

-sigh- I would answer the rest of the questions then remember that all of the answers to all of those questions came from me earlier in this thread.

MLM

Well i put it under createpost() and seems to be working fine...

I am trying to do this to get the topic id but its not working.. There has got to be a way to find the topic id.
$featured_work_topic_id = createPost($topicOptions['id']);

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

MLM

Disregard all that info.

I dont know. I see what Arantor is saying now. I see all my answers in posts before. I have no idea how i could of missed them unless you edited afterwards.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

MLM

ugh, I am sure this is bothering you because it is for me but the $topicOptions['id']; really isnt working. All my code is below the createpost even. It works completely awesome except i always get a 0 for topic id. I have tried globaling it by having it equal the global and then using it in the db insertion.

I need a variable that well give me the topic id of the topic that is being posted.

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Can you show me the code and EXACTLY where your putting it.

MLM

Right above createAttachment() on subs-post - Its there because i think i have a better chance of getting the topic id. It was above createPost() before today..


         // featured work stuff
$featured_work_image_url = $_POST['featured_work_image_url'];
$featured_work_image_title = $_POST['featured_work_image_title'];
$featured_work_image_creator = $user_info['username'];
$featured_work_topic_id = $topicOptions['id'];

if(!empty($featured_work_image_url) && !empty($featured_work_image_title))
{
$Regex = '/http:\/\/(.*?)png|jpg|gif/';

// checking if this is even an image url
if(preg_match($Regex, $featured_work_image_url))
{

// figure out the dimensions...
list($image_width, $image_height, $image_type, $image_attr) = getimagesize($featured_work_image_url);

// making the proportions
if($image_width < 283 && $image_height < 143)
{
}
else
{

$image_ratio = $image_width/$image_height;

if($image_width > $image_height)
{

$image_ratio = $image_width/$image_height;

while ($image_width > 287)
{
$image_width = $image_width - $image_ratio;
$image_height--;
}

// is the image height still too big?
if($image_height > 143)
{
$image_ratio = $image_height/$image_width;

while ($image_height > 143)
{
$image_height = $image_height - $image_ratio;
$image_width--;
}
}
}
else
{

$image_ratio = $image_height/$image_width;

while ($image_height > 143)
{
$image_height = $image_height - $image_ratio;
$image_width--;
}

// is the image width still too big?
if($image_height > 287)
{
$image_ratio = $image_width/$image_height;

while ($image_width > 287)
{
$image_width = $image_width - $image_ratio;
$image_height--;
}
}
}
}

// rounding our values to the nearest pixel
$featured_image_width = round($image_width);
$featured_image_height = round($image_height);






$feature_text = "n";

// insert everything into the db
mysql_query("INSERT INTO  vp_featured_work_entries (topic_id, feature, title, creator, image, width, height) VALUES('$featured_work_topic_id', '$feature_text', '$featured_work_image_title', '$featured_work_image_creator', '$featured_work_image_url', '$featured_image_width', '$featured_image_height') ") or die(mysql_error());

}
else
{
$post_errors[] = 'featured_work_not_image_error';
}


}
else
{
if(empty($featured_work_image_url) && empty($featured_work_image_title))
{
}
else
{
if(empty($featured_work_image_url))
{
$post_errors[] = 'featured_work_no_image_url_error';
}

if(empty($featured_work_image_title))
{
$post_errors[] = 'featured_work_no_title_error';
}
}
}

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

I dont understand what you mean by above these functions you need to be more specific. And I see your still not taking Arantor's advice with handling the errors.

The majority of that code shouldnt even be in createPost() anyways. I suggest you take the time to look back at what we have been telling you to do because this obviously isnt working out.

MLM

Its not in createPost()...

It is below createPost() above createAttachment().

Errors work great the way they are set up now...

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Quote from: MLM on February 28, 2011, 09:24:03 PM
Its not in createPost()...

It is below createPost() above createAttachment().

Errors work great the way they are set up now...

So the code is outside of any function?

MLM


My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Quote from: MLM on February 28, 2011, 09:30:47 PM
Quote from: Project Evolution on February 28, 2011, 09:26:42 PM
So the code is outside of any function?

Correct

It shouldnt be there. Because now everytime you include the Subs-Post.php file its also going to be executing that code. And honestly I have no idea how the error handling can even work if $post_errors is no where to be found in the entire file.

MLM

Quote from: Project Evolution on February 28, 2011, 09:33:27 PM
It shouldnt be there. Because now everytime you include the Subs-Post.php file its also going to be executing that code. And honestly I have no idea how the error handling can even work if $post_errors is no where to be found in the entire file.

lol it does.. I will add to createPost() since i get what you are getting at and it also has the $topicOptions['id'] variable in it so hopefully it might work lol. I will report back soon!

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

MLM

No luck, tried adding to end of createPost() which makes it not run at all..

Also tried adding just above "// We need to know if the topic is approved. If we're told that's great - if not find out." right under the variable stuffs that go on but same effect as having it out..

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

MLM

My question still remains on how to get the topic id that is being posted. I have seen the answer on the first page but it doesn't work (Arantor). I have tried globalizing the the $topicOptions['id']...

id_topic in the _messages db is not auto incrementing so how do i get that number...

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Now im going to take a gamble. Find this in createPost(),
// They've posted, so they can make the view count go up one if they really want. (this is to keep views >= replies...)
$_SESSION['last_read_topic'] = 0;


Add your code above that.

MLM

YAAAAAAAAAAAAAAAAAAY!  ;D

Thank you so much  Project Evolution - Working perfectly even not posting if the topic isnt made.

Your support much much apreciated. Also thanks to Arantor!

TOPIC SOLVED!

My Themes:

My Mods:

Unsolved Threads:
  • None atm...

ascaland

Phew, thank goodness... I was starting to get worried.

Advertisement: