Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

August 19, 2022, 05:05:42 AM

Login with username, password and session length
Members
  • Total Members: 3843
  • Latest: Tricky
Stats
  • Total Posts: 193479
  • Total Topics: 21088
  • Online today: 51
  • Online ever: 1035
  • (November 26, 2020, 04:45:12 AM)
Users Online
Users: 1
Guests: 41
Total: 42

Author Topic: [Block] Custom Post Prefills (and associated hack)  (Read 29685 times)

0 Members and 1 Guest are viewing this topic.

Offline Thurnok

  • Friends
  • *
  • Posts: 784
[Block] Custom Post Prefills (and associated hack)
« on: October 07, 2006, 03: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.
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]
Code: [Select]
// 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]
Code: [Select]
// 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:
Code: [Select]
////////////////////////////////////////////////////
// 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:
« Last Edit: September 17, 2008, 01:46:13 PM by Thurnok »

akulion

  • Guest
Re: Custom Post Prefills
« Reply #1 on: October 07, 2006, 03:50:48 PM »
wow Thanks!! u should release this as a mod down at SMF Thrunok - it would be greatly used!

Its awesome!

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: Custom Post Prefills
« Reply #2 on: October 07, 2006, 03: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.

Offline Maya

  • Friends
  • *
  • Posts: 237
Re: Custom Post Prefills
« Reply #3 on: October 07, 2006, 03: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:

Offline G6Cad

  • Friends
  • *
  • Posts: 12643
    • FamiljeGodis
Re: Custom Post Prefills
« Reply #4 on: October 07, 2006, 05:07:01 PM »
This i must try right now :)

jacortina

  • Guest
Re: Custom Post Prefills
« Reply #5 on: October 07, 2006, 07:55:19 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):

Code: [Select]
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.

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: Custom Post Prefills
« Reply #6 on: October 07, 2006, 10:35:30 PM »
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.  ;)

jacortina

  • Guest
Re: Custom Post Prefills
« Reply #7 on: October 08, 2006, 05:54:51 AM »
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
« Last Edit: October 08, 2006, 08:03:29 AM by jacortina »

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: Custom Post Prefills
« Reply #8 on: October 08, 2006, 04: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.  :)

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: Custom Post Prefills
« Reply #9 on: October 08, 2006, 04:24:34 PM »
Ok... updated the first post's code.