TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

April 19, 2024, 09:13:28 PM

Login with username, password and session length
Members
  • Total Members: 3,885
  • Latest: Growner
Stats
  • Total Posts: 195,165
  • Total Topics: 21,219
  • Online today: 266
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 1
  • Guests: 198
  • Total: 199
  • lurkalot

[Block] Custom Post Prefills (and associated hack)

Started by Thurnok, October 07, 2006, 10:44:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thurnok

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]
// 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:

akulion

wow Thanks!! u should release this as a mod down at SMF Thrunok - it would be greatly used!

Its awesome!

Thurnok

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.

Maya

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:

G6Cad


jacortina

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.

Thurnok

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

#7
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

Thurnok

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.  :)

Thurnok

Ok... updated the first post's code.