TinyPortal

Development => Block Codes => Topic started by: Thurnok on October 07, 2006, 10:44:53 PM

Title: [Block] Custom Post Prefills (and associated hack)
Post by: Thurnok on October 07, 2006, 10:44:53 PM
Ever wished you could have the first post of a new Topic prefilled with some text before the user starts typing in their text?

For example, say you have a support board.  Perhaps you would like users to answer certain questions in that support board.  You could prefill the questions so that when they decide to post something, the text entry area already has some questions in it they can simply add their answers to.  For example:

1) SMF Version:
2) TP Version:
3) php Version:
4) MySQL Version:
5) Website URL:

Well, now you can!  This TP Block requires my SMF Hack of the same name found here (http://tpblocks.ccs-net.com/index.php?topic=30.msg48#msg48).
I'll also add the hack code here so you have it all from one location.
Modify your /Themes/default/Post.template.php file as below:

[FIND]
// Finally the most important bit - the actual text box to write in!
echo '
<tr>
<td valign="top" align="right"></td>
<td>
<textarea class="editor" name="', $context['post_box_name'], '" rows="', $context['post_box_rows'], '" cols="', $context['post_box_columns'], '" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onchange="storeCaret(this);" tabindex="', $context['tabindex']++, '"', isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? ' style="border: 1px solid red;"' : '', '>', $message, '</textarea>
</td>
</tr>';


[REPLACE WITH]

// Finally the most important bit - the actual text box to write in!

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
/////     Hack by Thurnok - October 1, 2006                /////
/////     Custom prefill info in first post of topics      /////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// this is a hack to /Themes/default/Post.template.php
// the replaced portion is commented out below
//
/////     Update: December 9, 2006                         /////
/////     Fix: undefined index is_first_post when PM'ing   /////
/////                                                      /////
/////     Update: November 6, 2006                         /////
/////     Fix: duplicate prefill when no subject           /////
////////////////////////////////////////////////////////////////
global $db_prefix;
// setup our prefill array
$myprefill = array();
$dbquery = "SELECT * FROM ".$db_prefix."posts_prefills";
if ($dbresult = mysql_query($dbquery)){
while ($row = mysql_fetch_assoc($dbresult)){
$myprefill[$row['board_id']] = $row['post_prefill'];
}
mysql_free_result($dbresult);
}
echo '
<tr>
<td valign="top" align="right"></td>
<td>
<textarea class="editor" name="', $context['post_box_name'], '" rows="', $context['post_box_rows'], '" cols="', $context['post_box_columns'], '" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onchange="storeCaret(this);" tabindex="', $context['tabindex']++, '"', isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? ' style="border: 1px solid red;"' : '', '>', !empty($context['is_first_post']) && empty($message) ? (in_array($context['current_board'], array_keys($myprefill)) ? $myprefill[$context['current_board']] : '') : '', $message, '</textarea>
</td>
</tr>';
// echo '
// <tr>
// <td valign="top" align="right"></td>
// <td>
// <textarea class="editor" name="', $context['post_box_name'], '" rows="', $context['post_box_rows'], '" cols="', $context['post_box_columns'], '" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onchange="storeCaret(this);" tabindex="', $context['tabindex']++, '"', isset($context['post_error']['no_message']) || isset($context['post_error']['long_message']) ? ' style="border: 1px solid red;"' : '', '>', $message, '</textarea>
// </td>
// </tr>';
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////



Here's the phpblock code:
////////////////////////////////////////////////////
// Custom Post Prefills block  -  ver. 1.1        //
////////////////////////////////////////////////////
// Developed by Ken McNicholas (a.k.a. Thurnok)
// Complete Computer Services
// October 7, 2006
//
// This block allows you to Add/Remove/Modify your custom prefills.
// This block is meant to be used with the Custom Post Prefills SMF Hack by myself.
//
// Thanks to akulion for the idea
// and jacortina for pointing out a fix for nested <textarea> tags
//////////////////////////////////////////////

// There are no user configurable variables for this program!

// globals for database vars
global $db_prefix;
// globals for user information
global $settings, $context;

// Admins allowed ONLY!
if (!$context['user']['is_admin']){
echo ' <p /> <p /> <p />
<center>Sorry, you are not authorized to use this block.</center>
';
} else {

// get our script url (including parameters - like ?page=6)
$myself = $_SERVER['REQUEST_URL'];

// define table names
define('PP_PREFILL_TABLE', $db_prefix . 'posts_prefills');
define('PP_BOARDS_TABLE', $db_prefix . 'boards');

$pp_allboards = array();
$pp_result = mysql_query('SELECT ID_BOARD, name FROM ' . PP_BOARDS_TABLE);
if ($pp_result){
while ($row = mysql_fetch_assoc($pp_result)){
$pp_allboards[$row['ID_BOARD']] = $row['name'];
}
mysql_free_result($pp_result);
} else {
die('Unexpected error! MySQL Error: ' . mysql_error());
}


/*
****************************************
****************************************
***         !! FUNCTIONS !!          ***
****************************************
****************************************
*/

// function to create table if not already there
function ppCreateTable($tablename) {
$dbquery = 'CREATE table '.$tablename.' (board_id SMALLINT UNSIGNED NOT NULL, post_prefill TEXT)';
// send create table query
if (!mysql_query($dbquery)){
die('Query Failed!  Table NOT Created! MySQL Error: ' . mysql_error());
}
}

function ppDeleteRow($ppbrd){
@mysql_query('DELETE FROM '.PP_PREFILL_TABLE.' WHERE board_id = '.$ppbrd);
}

function ppSaveRow($ppbrd, $pptxt){
if (mysql_num_rows(@mysql_query('SELECT board_id FROM '.PP_PREFILL_TABLE.' WHERE board_id = '.$ppbrd))){
// already in table, do UPDATE
$dbquery = 'UPDATE '.PP_PREFILL_TABLE.' SET post_prefill = "'.$pptxt.'" WHERE board_id = '.$ppbrd;
} else {
// not in table, do INSERT
$dbquery = 'INSERT INTO '.PP_PREFILL_TABLE.' VALUES('.$ppbrd.', "'.$pptxt.'")';
}
@mysql_query($dbquery);
}

// special function to make SQL safe
function quote_smart($value){
// Stripslashes
if (get_magic_quotes_gpc()){
$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}


/*
****************************************
****************************************
***  !! Table Validation Section !!  ***
****************************************
****************************************
*/

// verify posts_prefills table exists / create it if not
$pp_result = mysql_query('SELECT board_id FROM ' . PP_PREFILL_TABLE . ' LIMIT 1');
if (!$pp_result){
if (mysql_errno() == 1146){
// table doesn't exist, go create it
ppCreateTable(PP_PREFILL_TABLE);
} else {
die("Unexpected SQL error: " . mysql_error());
}
} else {
mysql_free_result($pp_result);
}

/*
****************************************
****************************************
***     !! Main Code Section !!      ***
****************************************
****************************************
*/

// set post vars to other vars
$pp_save = empty($_POST['pp_save']) ? 0 : 1;
$pp_delete = empty($_POST['pp_delete']) ? 0 : 1;
$pp_text = empty($_POST['pp_text']) ? '' : $_POST['pp_text'];
$pp_boards = empty($_POST['pp_boards']) ? 0 : $_POST['pp_boards'];

// if someone clicks Delete button
if ($pp_delete && $pp_boards){
ppDeleteRow($pp_boards);
}

if ($pp_save && $pp_boards && $pp_text != ""){
ppSaveRow($pp_boards, $pp_text);
}

$pp_result = @mysql_query('SELECT * FROM '.PP_PREFILL_TABLE);

$pp_array = array();
if ($pp_result){
while ($row = mysql_fetch_assoc($pp_result)){
$pp_array[$row['board_id']] = $row['post_prefill'];
}
mysql_free_result($pp_result);
}

//////////////////////////////////////////////////////////////////
/////////////           Javascript area              /////////////
//////////////////////////////////////////////////////////////////
// write out our javascript stuff

echo '
<script type="text/javascript">
<!--
// create our main array first
var pparray = new Array();

';

foreach ($pp_array AS $key => $value){
// convert value to URL encoding or javascript chokes!
echo '
pparray["'.$key.'"] = "'.rawurlencode($value).'";
';
}

echo '
function reloadForm(){
x = document.pp_form.pp_boards;
if(pparray[x.value] != undefined){
document.pp_form.pp_text.value = unescape(pparray[x.value]);
} else {
document.pp_form.pp_text.value = "";
}
}
//-->
</script>
';

echo '
<form action="' . $myself . '" method="post" name="pp_form" target="_self">
Select a board below to Add/Delete/Modify a Custom Post Prefill:<br />
<select name="pp_boards" onChange="reloadForm()">
';

foreach ($pp_allboards AS $key => $value){
if (empty($pp_first)){
$pp_first = $key;
}
echo '
<option value="'.$key.'">'.$value.'</option>
';
}

$pp_init_text = in_array($pp_first, array_keys($pp_array)) ? $pp_array[$pp_first] : '';

echo '
</select><br />
<TEXT'.'AREA name="pp_text" rows="10" cols="70">'.$pp_init_text.'
</TEXT'.'AREA><br />
<input type=submit name="pp_save" value="Save" /> &nbsp;&nbsp;<input type=submit name="pp_delete" value="Delete" />
</form>
';
}



Ok, that's it, don't forget to apply my SMF Hack - Custom Post Prefills as well otherwise it won't work.

Thanks to akulion for the idea, and jacortina for pointing out a way to keep in a block without the nested <textarea> tags problem.

Screenies of interface:
Title: Re: Custom Post Prefills
Post by: akulion on October 07, 2006, 10:50:48 PM
wow Thanks!! u should release this as a mod down at SMF Thrunok - it would be greatly used!

Its awesome!
Title: Re: Custom Post Prefills
Post by: Thurnok on October 07, 2006, 10:54:42 PM
I say we make them get and install TinyPortal to use it.  ;)

Well, actually, they can simply use the hack, and edit the database manually without TP and still do it.. but the block makes it easy to manage your boards with prefills.
Title: Re: Custom Post Prefills
Post by: Maya on October 07, 2006, 10:56:34 PM
No kidding... that would eliminate a ton of posts if u had post need to know stuff...hehe

:) oooo the potential..

nice one Thurnok :up:
Title: Re: Custom Post Prefills
Post by: G6Cad on October 08, 2006, 12:07:01 AM
This i must try right now :)
Title: Re: Custom Post Prefills
Post by: jacortina on October 08, 2006, 02:55:19 AM
Quote from: Thurnok on October 07, 2006, 10:44:53 PM
This is not a traditional block because you need to put a php file under your forum directory somewhere.  The reason being, a <textarea> is used in this block, and since <textarea> tags cannot be nested (the block editor uses one, so if you really want to see how it horks up your blocks, try putting the code in directly and save it), I simply put it into a php file and use a phpblock with an include() function in it.

Can't you simply break it up. This doesn't seem to cause any problems when I use it in a block (from the end of that file):

echo '
</select><br />
<TEXT'.'AREA name="pp_text" rows="10" cols="70">'.$pp_init_text.'
</TEXT'.'AREA><br />
<input type=submit name="pp_save" value="Save" />   <input type=submit name="pp_delete" value="Delete" />
</form>
';


They don't become actual textarea tags until eval'd/output.
Title: Re: Custom Post Prefills
Post by: Thurnok on October 08, 2006, 05:35:30 AM
Once you "Send" the block (save it), you can no longer edit it in the block manager.  You would have to edit it in phpMyAdmin from that point on.  The reason is the closing </textarea> tag from my block code, closes the <textarea> tag from the block manager's textarea code.  To see what I'm talking about, put the code into a php article and save it.  You'll notice the Save and Delete buttons will now show outside of the article editor's textarea and you can no longer use the Send button in that article manager.  Your only recourse now being, to delete the article.

The same thing happens in a phpblock (center/left/right), though there is a peculiar side effect.  You can delete all the text in the block manager's textarea and then click the Delete button (the one that is part of my form built into the block!) which is sitting "outside" the textarea at that point, and it seems to save the block as an empty block again.  But article is a no-go in that respect.  Anyway, its too horky.  But, see for yourself.  ;)
Title: Re: Custom Post Prefills
Post by: jacortina on October 08, 2006, 12:54:51 PM
I have tried it the way I showed you and there are no problems.

I have saved it, brought it up again to edit, saved it again.

echo '</text'.'area>'; isn't a closing tag untl it's served up as output.


EDIT:
See JPDeni's post here: http://www.tinyportal.net/smf/index.php?topic=6384.msg52028#msg52028
Title: Re: Custom Post Prefills
Post by: Thurnok on October 08, 2006, 11:07:55 PM
ah.. Sorry jacortina, I didn't notice you had code in your post, skimmed right over it.  Just read the text part.

Yes, I guess that should work in that case.  Good idea, thanks for the pointer.  :)
Title: Re: Custom Post Prefills
Post by: Thurnok on October 08, 2006, 11:24:34 PM
Ok... updated the first post's code.
Title: Re: Custom Post Prefills
Post by: Xarcell on November 04, 2006, 03:00:00 AM
Is there a demo of this available?
Title: Re: Custom Post Prefills
Post by: Thurnok on November 04, 2006, 04:37:53 AM
There's a "user's perspective" demo on my TP Blocks site (http://tpblocks.ccs-net.com) that only the TP Team can see.
Title: Re: Custom Post Prefills
Post by: Thurnok on November 06, 2006, 07:26:42 PM
Thanks to Xarcell for pointing out a small bug - if you don't put a subject in and click the Post button, SMF brings you back to the msg edit screen with the error telling you to put in a subject, and consequently, the Post Prefill would add a duplicate prefill at the end of the first one.

This has been fixed.  The new code is in the first post.  It was the SMF hack code (Post.template.php file) that needed the fix, not the block code, so you only need to update the one.
Title: Re: Custom Post Prefills
Post by: akulion on November 30, 2006, 10:48:44 AM
everytime i use this I think of thurnok! Thanks man this is the best ever feature to have!
Title: Re: Custom Post Prefills
Post by: Thurnok on December 01, 2006, 02:34:14 AM
Well, it was after all your idea.  So thanks for the inspiration.  :)
Title: Re: Custom Post Prefills
Post by: akulion on December 01, 2006, 06:18:05 AM
yea yea :P ideas are million under the sun but its the implementation that counts...after all actions speak louder than words! :D

So its all thanks to you! Youre the maaaan!
Title: Re: Custom Post Prefills
Post by: Thurnok on December 09, 2006, 09:23:35 PM
Fixed a minor bug.

If you were getting an "undefined index is_first_post" error in your log, it was due to $context['is_first_post'] not defined for PMs, only board posts.  This has been fixed.  The block code is unchanged.  You need to update your /Themes/default/Post.template.php file with the new HACK code from the first post.

This means, if you have already applied the HACK previously, locate it and replace the previous hack code with the new one from the first post.
Title: Re: [Block & Hack] Custom Post Prefills
Post by: marzi on July 17, 2007, 12:31:52 PM
Thurnok and Gobo,
I have your mod installed and it is working just as described. Thank you very much for sharing this with others.

Is it possible to code the message that has used the 'Custom post prefills' to be displayed in a more controlled manner. For example, if I have a board for only Recipe postings and I am using the 'Custom post prefills' for the data entry, is there a way (perhaps another block or TP article) to pull the data out of the 'smf_posts_prefills' table and control the display? I know the input does respond the BBC tags, not many casual users are aware of that.

I guess I got the idea from seeing the following:  http://recipes.egullet.org/recipes/r342.html  (http://recipes.egullet.org/recipes/r342.html) and the information contained in the footer.

(The above message is also posted on Thurnok's web site but as he is often very busy I know it can take him weeks sometimes before he has time to reply. I hope no protocol has been violated.)

Thankx_
marzi
Title: Re: [Block & Hack] Custom Post Prefills
Post by: Thurnok on July 18, 2007, 07:21:25 AM
No protocol breach as far as I can tell.  :)

Anyway, yes, I posted an answer to you at my site already, so rather than duplicate that, I'll let you fetch it there. hehe
Title: Re: [Block & Hack] Custom Post Prefills
Post by: CampCounselor on January 02, 2008, 02:37:08 PM
this is not working for me can someone get in touch with me concerning this. i have applied the changes to post.template.php and no change/
Title: Re: [Block & Hack] Custom Post Prefills
Post by: IchBin on January 02, 2008, 03:32:02 PM
Did you do this part as well?
QuoteOk, that's it, don't forget to apply my SMF Hack - Custom Post Prefills as well otherwise it won't work.
Title: Re: [Block & Hack] Custom Post Prefills
Post by: CampCounselor on January 02, 2008, 03:49:33 PM
duh...i am so stupid. thanks for the reminder IchBin
Title: Re: [Block & Hack] Custom Post Prefills
Post by: IchBin on January 02, 2008, 04:09:15 PM
I'm just glad that was it. lol Otherwise, your code looked good and I wouldn't have known what could have been wrong. :)
Title: Re: [Block & Hack] Custom Post Prefills
Post by: CampCounselor on January 02, 2008, 04:56:00 PM
where do i get that? been looking and can not find it
Title: Re: [Block & Hack] Custom Post Prefills
Post by: IchBin on January 03, 2008, 01:44:00 AM
Did you look on Thurnoks site? I think there's a link in his profile or signature.
Title: Re: [Block & Hack] Custom Post Prefills
Post by: CampCounselor on January 03, 2008, 03:02:58 AM
Thanks works great. I would like it to do how smf does it but that is okay.
Title: Re: [Block] Custom Post Prefills (and associated hack)
Post by: Thurnok on September 17, 2008, 08:47:01 PM
Updated topic for auto-indexing.
Title: Re: [Block] Custom Post Prefills (and associated hack)
Post by: sgilleland on December 05, 2008, 02:48:35 PM
Is there any way to make the canned questions bold or a different color?
Title: Re: [Block] Custom Post Prefills (and associated hack)
Post by: G6Cad on December 05, 2008, 02:58:40 PM
No