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

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 790
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online

[Block] Custom Page

Started by Thurnok, August 09, 2006, 08:13:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thurnok

I originally posted this in response to another topic ("Block that members can mod") but realized it really doesn't fit the title.  Though the original poster, I believe, was actually looking for a custom page per user that could be displayed in a block so I responded there, mistakingly perhaps.  So, to call a duck a duck, I figured I would make a topic based on the actual code I posted and move my original posts here.
----- moved -----
Ok.. I did one up this morning.  I would have done yesterday but was at a party.

The idea of having a profile entry for myspace was ok, but this would require manually adding it or the webmaster having to install the Custom Profile mod or something.  I'd prefer to not make a block snippet dependent on some mod being installed since not everyone will have or want to have some particular mod installed.  Therefore, this one does not require any mod being available.  You simply add your page in the form under the page itself.

First, here's the code (phpblock):
//////////////////////////////////////////////
// Custom Page 1.05
// Developed by Thurnok
// Complete Computer Services
// August 27, 2006
//
// Last updated - January 27, 2007
// - fix for PHP 5.x
//
// Custom Page allows you to give your users the ability to display
// a page of their choice in a centerblock or article based on their User ID.
// This project was developed due to a request on TinyPortal site for
// a centerblock that would display the currently logged in user's
// myspace site.
//
// This version allows your members to choose whether to allow other members
// to view their custom page or not.  Other members can use a pulldown to select
// a member that has their custom page set to public and hence view that member's page.
// It also updates your custompage table to accommodate this automatically.
//
// This script will create a table for the custom page using your current
// SMF table prefix followed by custompage (ex: smf_custompage) and using your
// current database credential information.
// NOTE: Your database user/permissions used for SMF must allow you to
// create a table or you will never be able to store the info.  You
// can create the table manually if necessary.
//
// Also note that a mysql connection is already alive and so creating a new connection
// and closing it is undesirable and causes problems in other blocks.  Therefore,
// you will notice the absence of mysql_connect() and mysql_close() functions
// used in this snippet.
//////////////////////////////////////////////

// User Configurable items

//   *****   LAYOUT OPTIONS   *****
// an IFRAME is necessary to keep the page "within" your site
// width and height of the IFRAME in your center block
$cp_width = "100%";
$cp_height = 600;

//   *****   SECURITY CONFIGURATION   *****
// Groups allowed to select and set their custom page
// This is in the form of their Group ID - example: array('3', '6', '9');
$cp_allowed_groups = array('');
// individual members not allowed to use even if in an allowed group
// uses the member ID - example: array('91', '16');
$cp_disallowed_members = array('');
// whether or not to allow users to make their site public
$cp_allow_public = true;

//   *****   DEFAULT STUFF   *****
// if the user is not allowed, or doesn't have a custom page yet, what
// page do you want to display to them in the IFRAME?
$cp_default_page = "http://www.google.com";

// whether to display the default page if not allowed or no page yet (set to a true value)
// or to not display any page in the block, only the form (set to a false value)
$cp_display_default = 1;

// if $cp_display_default = 0 (or some false value), and user doesn't have a page set,
// display this default text where the IFRAME would have been
$cp_default_text = "You currently do not have a custom page selected.<br />
Please use the form below to select a page.<br />
If you do not see the form below, it is because you do not have access to create a custom page.";

// you can set the tablename to other than custompage if you like
$cp_tablename = "custompage";
// and the title displayed
$cp_title = "Custom Page!";
// add some descriptive text here if you like to display under title
$cp_desc = "Your MySpace right here!  Use the form below to set your custom page.";

// if user's browser does not support IFRAMEs, display this where it would be
$cp_no_iframe = "<h1>Custom Page!</h1>
Your browser does not support IFRAMEs.  You should get a better browser to see what you are missing!<br />
Check out Mozilla or Firefox, they are great!<br />";


// Print our title - or comment the line to not display a title
echo '<center><b>' . $cp_title . '</b></center><br />';
echo '<font size=1>' . $cp_desc . '</font><p />';

//////////////////////////////////////////////
//
// The rest of this you should leave as is
// unless you are overly industrious :)
//
//////////////////////////////////////////////
// globals for database vars
global $db_prefix;
// globals for user information
global $context, $user_info, $ID_MEMBER;

$cp_id = $ID_MEMBER;
$cp_admin = false;

// set up all our functions ahead of time
// function to create table if not already there
function CustomPageCreateTable($cp_tablename) {
// set up the query that will create the table appropriately
$dbquery = "CREATE table $cp_tablename (id INT UNSIGNED NOT NULL PRIMARY KEY,
url TEXT, public tinyint(1) NOT NULL default '0', selected_user INT UNSIGNED);";
if (!mysql_query($dbquery)) {
die("Query Failed!  Table NOT Created!<br />\n");
}
}

// function to modify table to add "public" and "selected_user" columns
function CustomPageModifyTable($cp_tablename) {
// set up the query that will modify the table appropriately
$dbquery = "ALTER TABLE $cp_tablename ADD (public tinyint(1) NOT NULL default '0', selected_user INT UNSIGNED);";
if (!mysql_query($dbquery)) {
if (mysql_errno() == 1060){
// just a double precaution as part of the NULL fix :)
} else {
die("Query Failed!  Table NOT Modified!<br />\n" . mysql_errno() . ": " . mysql_error());
}
}
}

// function to add/edit a user and their custom page to/in the table
function CustomPageAdd($cp_tablename, $cp_id, $cp_url, $cp_selected_user, $cp_public=0) {
if ($cp_url != ''){
if (strtolower(substr($cp_url, 0, 7)) != "http://"){
$cp_url = "http://" . $cp_url;
}
}
// only add user if they do not already exist
$dbquery = "SELECT * FROM $cp_tablename
WHERE id = '" . $cp_id . "'";
$dbresult = mysql_query($dbquery);
if ($dbresult){
if ($row = mysql_fetch_assoc($dbresult)){
// if a row is found, then there's already this user in table, edit instead
CustomPageEdit($cp_tablename, $cp_id, $cp_url, $cp_selected_user, $cp_public);
return;
}
}
$dbquery = 'INSERT INTO '.$cp_tablename.' VALUES ('.$cp_id.', "'.$cp_url.'", '.$cp_public.', '.$cp_selected_user.');';
if (!mysql_query($dbquery)) {
die("Query Failed!  Custom Page NOT Inserted into database!<br />\n");
}
}

// function to edit a user's custom page in the table
function CustomPageEdit($cp_tablename, $cp_id, $cp_url, $cp_selected_user, $cp_public=0) {
// start our dbquery
$dbquery = "UPDATE $cp_tablename ";
// if no URL supplied, don't edit the URL
if ($cp_url != ''){
// add HTTP:// if necessary at front of link to prevent BASE URL applying in front of link provided
if (strtolower(substr($cp_url, 0, 7)) != "http://"){
$cp_url = "http://" . $cp_url;
}
// since an url was entered, add it to the update
$dbquery .= 'SET url = "'.$cp_url.'", public = '.$cp_public.', selected_user = '.$cp_selected_user.'
WHERE id = '.$cp_id.';';
} else {
// update all but the URL
$dbquery .= "SET public = $cp_public, selected_user = $cp_selected_user
WHERE id = $cp_id;";
}
if (!mysql_query($dbquery)) {
die("Query Failed!  Custom Page NOT Modified in database!<br />\n");
}
}

// function to delete Custom Page from the table
function CustomPageDel($cp_tablename, $cp_selected_user) {
// delete Custom Page with $cp_id
$dbquery = "DELETE FROM $cp_tablename WHERE id = $cp_selected_user;";
if (!mysql_query($dbquery)) {
die("Query Failed!  Custom Page NOT Deleted from database!<br />\n");
}
}


///////////  MAIN CODE HERE  ////////////
// Admins are always allowed to create a custom page
if ($context['user']['is_admin']){
$cp_admin = 1;
}

// convert $_POST vars to prevent undefined index errors
$cp_save = empty($_POST['cp_save']) ? '' : $_POST['cp_save'];
$cp_del = empty($_POST['cp_del']) ? '' : $_POST['cp_del'];
$cp_urlbox = empty($_POST['cp_urlbox']) ? '' : $_POST['cp_urlbox'];
$cp_public = empty($_POST['cp_public']) ? 0 : $_POST['cp_public'];
$cp_selected_user = empty($_POST['cp_selected_user']) || (!$cp_allow_public && !$cp_admin) ? $ID_MEMBER : $_POST['cp_selected_user'];

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

// put the SMF table prefix in front of your tablename from above
$cp_tablename = $db_prefix . $cp_tablename;
// do same for SMF Members table
$smf_members_table = $db_prefix . 'members';


////////////////   Security Checks  ////////////////
// check if user is in a group that is allowed to set a custom page
$cp_allowed = array_intersect($cp_allowed_groups, $user_info['groups']);

// don't let guests set page - would set default for all guests if they did
// also check if user is in the disallow array
if ($context['user']['is_guest'] || @in_array($ID_MEMBER, $cp_disallowed_members)){
$cp_allowed = false;
}

// Admins are always allowed to create a custom page
if ($cp_admin){
$cp_allowed = 1;
}
//////////////////////////////////////////////

// if someone just clicked Save, post info to database
if (!empty($cp_save) && $cp_allowed){
// if the urlbox had info in it, trim it
if (!empty($cp_urlbox)){
$cp_urlbox = trim($cp_urlbox);
} else {
$cp_urlbox = '';
}
CustomPageAdd($cp_tablename, $cp_id, $cp_urlbox, $cp_selected_user, $cp_public);
}

// if someone just deleted a page, remove it from database
if ($cp_del && $cp_admin){
CustomPageDel($cp_tablename, $cp_del);
}

////////////  MAIN DISPLAY CODE HERE  ///////////////
// set query to select all data for all users
$dbquery = "SELECT * FROM $cp_tablename;";
$dbresult = mysql_query($dbquery);

if ($dbresult){
// table exists - does it need an upate?
$row = mysql_fetch_assoc($dbresult);
if (in_array("public", array_keys($row))){
// no update needed here!
mysql_data_seek($dbresult, 0);
} else {
// YES!  then create the columns!
mysql_free_result($dbquery);
CustomPageModifyTable($cp_tablename);
// re-query the modified table
$dbresult = mysql_query($dbquery);
if (!$dbresult){
die("Unexpected error: " . mysql_error());
}
}
} else {
// no result, is it because table doesn't exist?
if (mysql_errno() == 1146){
// table doesn't exist, create it!
CustomPageCreateTable($cp_tablename);
// get our result again
$dbresult = mysql_query($dbquery);
if (!$dbresult) die("Unexpected error: " . mysql_error());
} else {
die("Unexpected error: " . mysql_error());
}
}

$cp_page = '';
$cp_selected_user = $ID_MEMBER;
$cp_selected_page = '';
// cycle through all rows
while ($row = mysql_fetch_assoc($dbresult)){
// add to public pages array if they have set to public (but not ourselves)
// or if the current user is an admin
if (($row["public"] != 0 || $cp_admin) && $row["id"] != $ID_MEMBER){
$cp_public_pages[$row["id"]] = $row["url"];
}
// if our row, get our public status, url, and our selected user
if ($row["id"] == $ID_MEMBER){
$cp_public = $row["public"];
$cp_page = $row["url"];
$cp_urlbox = $cp_page;
$cp_selected_user = $row["selected_user"];
}
}
$cp_public == 0 ? $cp_checked = '' : $cp_checked = "CHECKED";

// free previous result set
mysql_free_result($dbresult);
// if there were some public pages in table, setup the public users array
if (!empty($cp_public_pages)){
$dbquery = "SELECT ID_MEMBER, memberName FROM $smf_members_table
WHERE ID_MEMBER IN (" . implode(",", array_keys($cp_public_pages)) . ");";
$dbresult = mysql_query($dbquery);
if (!$dbresult) die("Unexpected error: " . mysql_error());
$cp_public_users = array();
while ($row = mysql_fetch_assoc($dbresult)){
$cp_public_users[$row["ID_MEMBER"]] = $row["memberName"];
}
// free the result for good measure
mysql_free_result($dbresult);
}

// get our selected user's page
if (!empty($cp_public_pages) && @in_array($cp_selected_user, @array_keys($cp_public_pages))){
$cp_selected_page = $cp_public_pages[$cp_selected_user];
}
// if the selected page is null "" then select our own page if it not ""
if (empty($cp_selected_page)){
if (!empty($cp_page)){
$cp_selected_page = $cp_page;
} else {
$cp_display_default ? $cp_selected_page = $cp_default_page : $cp_selected_page = '';
}
}

// display the $cp_selected_page to the user
if (!empty($cp_selected_page)){
echo '<center><IFRAME NAME="CustomPage" ALIGN=middle SRC="' . $cp_selected_page . '" WIDTH=' . $cp_width . ' HEIGHT=' . $cp_height . '>
' . $cp_no_iframe . '
</IFRAME></center>';
} else {
if ($cp_display_default){
echo '<center><IFRAME id="CustomPageID" NAME="CustomPage" ALIGN=middle SRC="' . $cp_default_page . '" WIDTH=' . $cp_width . ' HEIGHT=' . $cp_height . '>
' . $cp_no_iframe . '
</IFRAME></center>';
} else {
echo '<br />' . $cp_default_text . '<p />';
}
}

// only show form if user is allowed to add/edit their custom page
if ($cp_allowed){
//////////////////////////////////////////////////////////////////
/////////////           Javascript area              /////////////
//////////////////////////////////////////////////////////////////
// write out our javascript stuff

echo '
<script type="text/javascript">
<!--
// create our main array first
var jarray = new Array(); // list of member ids from pull down
var jarray2 = new Array(); // public page user ids
var jarray3 = new Array(); // public page urls
var jarray4 = new Array(); // array of public page user ids => public page urls
jarray[0] = '.$ID_MEMBER.';
jarray4['.$ID_MEMBER.'] = "'.$cp_page.'";
';
if (!empty($cp_public_pages)){
// create a list of public page user ids
$cp_public_user_list = implode(",", array_keys($cp_public_users));
$cp_page_ids = implode(",", array_keys($cp_public_pages));
foreach($cp_public_pages AS $key => $value){
if (empty($cp_page_urls)){
$cp_page_urls = '"' . $value . '"';
} else {
$cp_page_urls .= ',"' . $value . '"';
}
}
echo '
jarray.push('.$cp_public_user_list.');
// if our javascript is huge, big bandwidth waster, save some bandwidth here
jarray2.push('.$cp_page_ids.');
jarray3.push('.$cp_page_urls.');
y = jarray2.length;
for (x=0;x<y;x++){
page_id = jarray2[x];
jarray4[page_id] = jarray3[x];
}
';
}
echo '
function selectedChanged(){
s_id = jarray[document.cp_form.cp_selected_user.selectedIndex];
s_url = jarray4[s_id];
window.frames.CustomPage.location.href = s_url;
}
//-->
</script>
';


// start our form and table
echo '
<p /><form name="cp_form" action="' . $myself . '" method=post>
<table width="100%" border=0>
<tr>
<td width="40%">My Custom Page info:</td>
<td width="10%"></td>
<td width="40%">Public Member Pages</td>
<td width="10%"></td>
</tr>
<tr>
<td width="40%">URL: <input type=text name=cp_urlbox size="50" value="' . $cp_urlbox . '" /></td>
<td width="10%"></td>
<td width="40%" align="left">
<select name=cp_selected_user onChange="selectedChanged()">';
if ($cp_selected_user == $ID_MEMBER){
echo '
<option value="'.$ID_MEMBER.'" SELECTED>' . $user_info["username"] . '</option>
';
} else {
echo '
<option value="'.$ID_MEMBER.'">' . $user_info["username"] . '</option>
';
}
if (!empty($cp_public_users)){
foreach ($cp_public_users as $key => $value){
echo '

';
if ($cp_selected_user == $key){
echo '
<option value="'.$key.'" SELECTED>' . $value . '</option>
';
} else {
echo '
<option value="'.$key.'">' . $value . '</option>
';
}
}
}
echo '
</select>
</td>
<td width="10%"></td>
</tr>
<tr>
<td width="40%"><label><input type=checkbox name=cp_public value="1" ' . $cp_checked . ' />Make my page public</label></td>
<td width="10%"></td>
<td width="40%"></td>
<td width="10%"></td>
</tr>
<tr>
<td colspan="4" align="center"><input type=submit name=cp_save value="Save" /></td>
</tr>
</table>
</form>
';
}


See the next post for usage info and features.
Also, you can demo at my TP Blocks site: http://tpblocks.ccs-net.com (register to see both a "user" and "admin" perspective of various blocks).

Thurnok

Custom Page

Features:
  • Allow your users to select their own page to display on their frontpage (center block).
  • Can display a default page setup by you, for your users to see initially until they add their own page (or always if they do not have add access).
  • Can display a default text message instead of the default page.
  • Access for a user to add their own page via membergroups (if not in membergroups you give access, they see the default page or text message you setup).
  • Access to deny individual users even if they are in a membergroup with access.
  • Admins always have access.
  • Configure the size of the IFRAME that page displays in to fit your site appropriately.
  • Creates a table to hold member's custom page automatically if it doesn't already exist
Configuration Items:
  • $cp_width and $cp_height - width and height of the IFRAME holding the custom page
  • $cp_allowed_groups - array to hold allowed groups - example: array('7', '15', '11')
  • $cp_disallowed_members - array to hold denied individuals regardless of group
  • $cp_default_page - default page to display when user has no access or hasn't created their own yet ("http://www.myfavoritesite.com")
  • $cp_display_default - true/false, whether or not to display default page - false lets you display default text (see next)
  • $cp_default_text - text to display instead of a default page when $cp_display_default = false
  • $cp_no_iframe - text to display if the user's browser doesn't support IFRAMEs (relatively uncommon nowadays)
The rest is pretty self-explanitory and the code is moderately commented.

To Do's:
  • Add administrative feature to allow admin to view actual page any member has selected
  • Add administrative features for admin to remove or change individual pages for any member (default page per member for one reason)
  • Option for webmaster to set a random or rotating default page
and of course any other suggestions here   :)

Thurnok

Additional usage of Custom Page:

You can optionally put the code into a php Article.  The suggested usage in this case would be to give a menu item (My Custom Page for example) and let the user click the menu item to display/add/edit their custom page.

When creating the article, you must make a php article and simply put the same code above into it.  You should set the article to not display on the frontpage.  Then your users can click the Custom Page menu item you create to display the full body article (it of course being their custom page).  This in effect gives them a one click link to their custom page and allows you to make more of it visible (set the IFRAME width/height parameters larger) since you can set the article to not display left/right blocks, center blocks, etc.

Create your article in the admin control panel.  Then move your mouse over the article in the list and look at the link properties in your browser's status bar to see what page number the article is listed as.  Then create your menu item link using:
http://www.mysmfsite.com/index.php?page=<the page number you read>

That's pretty much it...

Avinash

I like the sound of this, but the demo site is down :(

Wartog

Very nice. however is thier anyway to resize the site shown in the block?

Thurnok

Quote from: Avinash on August 09, 2006, 11:22:23 PM
I like the sound of this, but the demo site is down :(
There is no demo site.  The link there is an example of what you would put in a menu to call it from an article.

Thurnok

You can resize the IFRAME as mentioned by changing the variables $cp_width and $cp_height.  I assume that is what you are asking.  If you are asking is there a way to zoom a website in and out of an IFRAME then the answer is not really.

Wartog

I mean how to get the iframe to show the whole webpages width.
like it only show half the width and you have to scroll.

Id like to have it display the whole webpages width inside the iframe without the scroll .

IchBin

is your width/height set to 100%?

Thurnok

Change the variables I mentioned, that resizes the IFRAME (which is what creates the scroll bars).

Things to keep in mind:
1) If a website's page is using static sizing, and your IFRAME is using smaller dimensions than that of the page being loaded into, you will get scrollbars (assuming the defaults for IFRAME), or you will simply see only the amount of the page that fits the IFRAME size.  That's simply how IFRAME works.

2) You cannot zoom a webpage (a.k.a. scale it) to fit in the IFRAME.  At least not in pure HTML/PHP.  It would be a relatively complicated discussion to further explain it so just assume that it cannot be done in a practical manner for this venue.

3) IFRAME's size can be set static or relative.  In other words you can use 100% for width and height settings instead of a set number of pixels.  In the case of a TP Center block, width set to 100% works fine.  However, in the case of Height, the IFRAME will get the initial block height from TP which will be relatively small (same is true in an article) where as setting a static setting (an actual pixel size) will force the TP Block to expand to the size set.  In the Custom Page block, set $cp_width = "100%"; if you like.  Don't forget the double quotes however.

4) When using a static size for IFRAME's dimensions, keep in mind the resolutions of your members differ and therefore could cause them to have to scroll right to see right side blocks if you set width statically too high for their resolution.

This website is proudly hosted on Crocweb Cloud Website Hosting.