Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-23-2010, 08:21 PM   PM User | #1
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
Need help on snippet of code;

I am using a cms called webspell and i am trying to make a custom theme / theme mod. My php experience is minimal so i need help rewriting a piece of code, here is it;

Code:
function gettemplate($template,$endung="html", $calledfrom="root") {
     $templatefolder = "templates";
     if($calledfrom=='root') {
          return str_replace("\"", "\\\"", $GLOBALS['_language']->replace(file_get_contents($templatefolder."/".$template.".".$endung)));
     }
     elseif($calledfrom=='admin') {
          return str_replace("\"", "\\\"", $GLOBALS['_language']->replace(file_get_contents("../".$templatefolder."/".$template.".".$endung)));
     }
}
Basically that code will call on the .html template files for users from the root/templates folder. What i am trying to do is get it to look for the template files in the root/themes/$activetheme/templates directory, and if the file is not located there it will then look for it in the root/templates directory. Is this possible and if so can someone rewrite that segment of code to do this (for users not admin), thanks!
Cliffo is offline   Reply With Quote
Old 03-23-2010, 08:42 PM   PM User | #2
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
I am not sure how much coding this requires (or if its possible) but if is a bit more involved than i am thinking i can pay what it is worth through paypal.
Cliffo is offline   Reply With Quote
Old 03-23-2010, 08:46 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Where is $activetheme coming from?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-23-2010, 08:50 PM   PM User | #4
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
_setting.php (which is loaded before the _functions.php) i declared it there and to the proper location in the database.
Cliffo is offline   Reply With Quote
Old 03-23-2010, 09:01 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I hate doing this, but chances are this function is already in use:
PHP Code:
function gettemplate($template,$endung="html"$calledfrom="root") {
     global 
$activetheme;
     
$templatefolder "templates/themes/" $activetheme;

     if(
$calledfrom=='root') {
          return 
str_replace("\"""\\\""$GLOBALS['_language']->replace(file_get_contents($templatefolder."/".$template.".".$endung)));
     }
     elseif(
$calledfrom=='admin') {
          return 
str_replace("\"""\\\""$GLOBALS['_language']->replace(file_get_contents("../".$templatefolder."/".$template.".".$endung)));
     }

Try that.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-23-2010, 09:31 PM   PM User | #6
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
I am not sure if that code is actually doing what i need. The templates folder is root/templates/ by default. I want it to look for templates files inside of root/themes/$activetheme/templates/ and if it does not find the file located there it will then load the one that is in root/templates/
Cliffo is offline   Reply With Quote
Old 03-23-2010, 09:43 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is this in any way related to the use of $calledfrom or no?
Edit:
No it won't, that has just the purpose of changing directory. You can actually get around that, but we won't get into that right now.
Change like so:
PHP Code:
function gettemplate($template,$endung="html"$calledfrom="root")

    global 
$activetheme

    
$defaultFolder 'root/templates/';
    
$customFolder 'root/themes/' $activetheme '/templates/';
    
$templateFile $template '.' $endung;

    
$usingTemplate $customFolder $templateFile;

    if (
$calledfrom <> 'root')
    {
        
$usingTemplate '/../' $usingTemplate;
    }

    if (!
is_readable($usingTemplate))
    {
        
$usingTemplate $defaultFolder $templateFile;
    }

    return 
str_replace("\"""\\\""$GLOBALS['_language']->replace(file_get_contents($usingTemplate))); 


Try that.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 03-23-2010 at 09:54 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Cliffo (03-23-2010)
Old 03-23-2010, 09:46 PM   PM User | #8
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Something like the following. You'll need to transpose it into your existing code.

Code:
if (is_file('./root/themes/'.$activetheme.'/templates/'.$template))
{
    $file = './root/themes/'.$activetheme.'/templates/'.$template;
}
else
{
    $file = './root/templates/'.$template;
}
MattF is offline   Reply With Quote
Users who have thanked MattF for this post:
Cliffo (03-23-2010)
Old 03-23-2010, 09:51 PM   PM User | #9
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
That looks like it does what i need, where do i put it exactly?
Cliffo is offline   Reply With Quote
Old 03-23-2010, 10:33 PM   PM User | #10
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
Quote:
function gettemplate($template,$endung="html", $calledfrom="root")
{
global $activetheme;

$defaultFolder = 'templates/';
$customFolder = 'themes/' . $activetheme . '/templates/';
$templateFile = $template . '.' . $endung;

$usingTemplate = $customFolder . $templateFile;

if ($calledfrom <> 'root')
{
$usingTemplate = '/../' . $usingTemplate;
}

if (!is_readable($usingTemplate))
{
$usingTemplate = $defaultFolder . $templateFile;
}

return str_replace("\"", "\\\"", $GLOBALS['_language']->replace(file_get_contents($usingTemplate)));

}
THAT IS IT THANK YOU!!!

With one minor changed, i simply removed "root" from the two 'root/templates/'

Last edited by Cliffo; 03-23-2010 at 10:43 PM..
Cliffo is offline   Reply With Quote
Old 03-23-2010, 11:24 PM   PM User | #11
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
Got another for ya, this is the code i added to the admin panel to allow the user to change the activetheme;

PHP Code:
<tr>
        <td align="right"><input type="text" name="activetheme" value="<?php echo $ds['activetheme']; ?>" size="15" onmouseover="showWMTT('id44')" onmouseout="hideWMTT()" /></td>
        <td><b>Active Theme</b> <i>(make sure the theme is inside the /themes folder and put the exact theme folder name here without slashes(Example: default)</i></td>
      </tr>
How can i make that option a drop down with a list showing every folder inside of root/themes/ that way we can avoid the whole typo issue i foresee happening.
Cliffo is offline   Reply With Quote
Old 03-23-2010, 11:43 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Lets wing it. Man I miss my PC :'(
I'll just stick with the tables here:
PHP Code:
<tr> 
        <td align="right"><select name="activetheme">
<?php
$di 
= new DirectoryIterator('/root/themes');
foreach (
$di AS $file)
{
    if (
$file->isDir() && !$file->isDot())
    {
        
sprintf('<option value="%s">%s</option>' PHP_EOL$file->getFileName(), $file->getFileName());
    }
}
?>
</select></td>
        <td><b>Active Theme</b> <i>(make sure the theme is inside the /themes folder and put the exact theme folder name here without slashes(Example: default)</i></td> 
      </tr>
Yeah, something like that; you'll need to readd you're js calls though, I don't know what they do so I don't know if you'll need them / can use them. If you don't have DirectoryIterator, someone else will show you how to do it with a filescan or glob call.
I gotta go home now
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-24-2010, 12:07 AM   PM User | #13
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Active Theme DropDown:
Code:
<?php # Read Theme Folders
function Theme_Folders($dir,$current){
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  if(strpos($filename,".") === false){
    $files[] = $filename;
  }
}
$theme = "<select id=\"theme\">\n";
$theme .= "\t<option value=\"\">- Active Themes -</option>\n";
foreach ($files As $file){
  if($current == $file){ 
    $select = " SELECTED"; 
  }
  $theme .= "\t<option value=\"$file\"$select>$file</option>\n";
}
$theme .= "</select>";
return $theme;
}
/* How to call the dropdown */
print Theme_Folders('PathtoThemes/','Current Theme Name');
?>

Last edited by DJCMBear; 03-24-2010 at 12:15 AM..
DJCMBear is offline   Reply With Quote
Old 03-24-2010, 12:28 AM   PM User | #14
Cliffo
New Coder

 
Join Date: Mar 2010
Posts: 78
Thanks: 18
Thanked 1 Time in 1 Post
Cliffo is an unknown quantity at this point
Quote:
Originally Posted by DJCMBear View Post
Active Theme DropDown:
Code:
<?php # Read Theme Folders
function Theme_Folders($dir,$current){
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  if(strpos($filename,".") === false){
    $files[] = $filename;
  }
}
$theme = "<select id=\"theme\">\n";
$theme .= "\t<option value=\"\">- Active Themes -</option>\n";
foreach ($files As $file){
  if($current == $file){ 
    $select = " SELECTED"; 
  }
  $theme .= "\t<option value=\"$file\"$select>$file</option>\n";
}
$theme .= "</select>";
return $theme;
}
/* How to call the dropdown */
print Theme_Folders('PathtoThemes/','Current Theme Name');
?>
Neither of those is working, this one seems close, is it pulling the options from the folders inside of root/themes/ ?
Cliffo is offline   Reply With Quote
Old 03-24-2010, 12:31 AM   PM User | #15
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Quote:
Originally Posted by Cliffo View Post
Neither of those is working, this one seems close, is it pulling the options from the folders inside of root/themes/ ?
You would need to write: print Theme_Folders('themes/','Current Theme Name');

And then the current theme name would be defined from your database from the row of the user.

it looks inside the defined folder path and calls out the folder names into and array to be put into a foreach function which creates the html options for the select box it works 100% because it was tested.

Last edited by DJCMBear; 03-24-2010 at 12:35 AM..
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:48 PM.


Advertisement
Log in to turn off these ads.