...

View Full Version : Resolved Need help on snippet of code;



Cliffo
03-23-2010, 09:21 PM
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;



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
03-23-2010, 09:42 PM
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.

Fou-Lu
03-23-2010, 09:46 PM
Where is $activetheme coming from?

Cliffo
03-23-2010, 09:50 PM
_setting.php (which is loaded before the _functions.php) i declared it there and to the proper location in the database.

Fou-Lu
03-23-2010, 10:01 PM
I hate doing this, but chances are this function is already in use:


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.

Cliffo
03-23-2010, 10:31 PM
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/

Fou-Lu
03-23-2010, 10:43 PM
Is this in any way related to the use of $calledfrom or no?

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:


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.

MattF
03-23-2010, 10:46 PM
Something like the following. You'll need to transpose it into your existing code.



if (is_file('./root/themes/'.$activetheme.'/templates/'.$template))
{
$file = './root/themes/'.$activetheme.'/templates/'.$template;
}
else
{
$file = './root/templates/'.$template;
}

Cliffo
03-23-2010, 10:51 PM
That looks like it does what i need, where do i put it exactly?

Cliffo
03-23-2010, 11:33 PM
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/'

Cliffo
03-24-2010, 12:24 AM
Got another for ya, this is the code i added to the admin panel to allow the user to change the activetheme;


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

Fou-Lu
03-24-2010, 12:43 AM
Lets wing it. Man I miss my PC :'(
I'll just stick with the tables here:


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

DJCMBear
03-24-2010, 01:07 AM
Active Theme DropDown:


<?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');
?>

Cliffo
03-24-2010, 01:28 AM
Active Theme DropDown:


<?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/ ?

DJCMBear
03-24-2010, 01:31 AM
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.

Cliffo
03-24-2010, 01:39 AM
Hmm that confused me, maybe i will just use the simple one i wrote where they type in the folder name lol

DJCMBear
03-24-2010, 01:43 AM
your themes are stored here right 'root/themes/'?

so you want to get each folder name inside the themes folder?

my function does that you just take off root/ so you have themes/ and thats it.

all you need to do is this:



<?php
print Theme_Folders('themes/','');
?>

Cliffo
03-24-2010, 02:11 AM
ya the folder containing the themes is /themes/ i have tried a lot of this nd i can not get this to display any option but - Active Theme -

DJCMBear
03-24-2010, 02:15 AM
thats because you cant start /themes/ with a / it has to be themes/

Cliffo
03-24-2010, 02:27 AM
I copied your code exactly


<?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');
?>

<table align="center">
<br />
<tr align="center">
<td><font color="red"><b>*Active Theme*</b></font></td>
</tr>
<tr align="center">
<?php
print Theme_Folders('themes/','');
?>
</tr>
<br />
</table>

Cliffo
03-24-2010, 02:32 AM
There is another section of the same files that does a similar function;



$langdirs = '';
$filepath = "../languages/";
if ($dh = opendir($filepath)) {
while($file = mb_substr(readdir($dh), 0,2)) {
if($file!="." AND $file!=".." AND is_dir($filepath.$file)) $langdirs .= '<option value="'.$file.'">'.$file.'</option>';
}
closedir($dh);
}
$lang = $ds['default_language'];
$langdirs = str_replace('"'.$lang.'"', '"'.$lang.'" selected="selected"', $langdirs);



<tr>
<td align="right"><select name="language" onmouseover="showWMTT('id40')" onmouseout="hideWMTT()"><?php echo $langdirs; ?></select></td>
<td><?php echo $_language->module['default_language']; ?></td>
</tr>

I tried coping that and changing lang to theme but it didnt work...

DJCMBear
03-24-2010, 02:33 AM
what does your server tree look like?

it worked for me and mine is like this:

root
-themplates
--themes
---theme_1
---theme_2
---theme_3
---theme_4

and i used:


print Theme_Folders('templates/themes/','');

and it printed this:


<select id="active_theme">
<option value="">- Active Themes -</option>
<option value="theme_1">theme_1</option>
<option value="theme_2">theme_2</option>
<option value="theme_3">theme_3</option>
<option value="theme_4">theme_4</option>
</select>


and you would need to delete the code i wrote



/* How to call the dropdown */
print Theme_Folders('PathtoThemes/','Current Theme Name');

Cliffo
03-24-2010, 02:36 AM
Nope thats prob the issue

root
-templates
-themes
--theme name1
--theme name2

DJCMBear
03-24-2010, 02:39 AM
ok now write this:


print Theme_Folders('templates/themes/','');

Cliffo
03-24-2010, 02:52 AM
This is what i put;


<?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;
}
?>

<table align="center">
<br />
<tr align="center">
<?php
print Theme_Folders('templates/themes/','');
?>
</tr>
</table>

.. still only giving -Active Theme - as an option.

also tried

print Theme_Folders('themes/','');

which i think would be correct but no go.


root
--------templates
--------admin
--------themes
---------------------theme1
---------------------theme2
--------js
--------ect ect

DJCMBear
03-24-2010, 03:04 AM
<?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;
}
?>

<table align="center">
<br />
<tr align="center">
<?php
print Theme_Folders('templates/themes/','');
?>
</tr>
</table>

root
--------templates
--------admin
--------themes
---------------------theme1
---------------------theme2
--------js
--------ect ect

That looks ok but from the tree your themes folder isnt in the templates folder so it would be themes/ have you tried themes without the / at the end?

EDIT:
And are you putting this Themes_Folders on a page in your root or inside a different folder?

Cliffo
03-24-2010, 03:47 AM
the file that has the stuff to change this is in;

mywebsite.com
mywebsite.com/admin
mywebsite.com/admin/settings.php


themes folder is NOT inside templates

mywebsite.com
mywebsite.com/themes
mywebsite.com/themes/themes1
mywebsite.com/themes/theme2
mywebsite.com/templates
mywebsite.com/admin
mywebsite.com/admin/settings.php (all this coding is here to change the theme)

DJCMBear
03-24-2010, 04:19 AM
the file that has the stuff to change this is in;

mywebsite.com
mywebsite.com/admin
mywebsite.com/admin/settings.php


themes folder is NOT inside templates

mywebsite.com
mywebsite.com/themes
mywebsite.com/themes/themes1
mywebsite.com/themes/theme2
mywebsite.com/templates
mywebsite.com/admin
mywebsite.com/admin/settings.php (all this coding is here to change the theme)

ok now that i have *all* the information try this:


print Theme_Folders('../themes/','');

Cliffo
03-24-2010, 04:28 AM
Okay it is giving the folders as options now, sorry about that i am very new to this, it still is not functioning fully though. Which ever folder they select, that folders name needs to be set as $activetheme, also the default option highlighted shouldn't be -Active Theme- is should be the name of the &activetheme

DJCMBear
03-24-2010, 04:35 AM
Okay it is giving the folders as options now, sorry about that i am very new to this, it still is not functioning fully though. Which ever folder they select, that folders name needs to be set as $activetheme, also the default option highlighted shouldn't be -Active Theme- is should be the name of the &activetheme

thats what the 2nd option is for....

for each user im guessing you call out their current theme and set it as $activetheme
so all you should do is this



print Theme_Folders('../themes/',$activetheme);

Cliffo
03-24-2010, 04:37 AM
Okay now it is showing the $activetheme as the default option, but when they change the option it doesn't save to $activetheme

DJCMBear
03-24-2010, 04:39 AM
Okay now it is showing the $activetheme as the default option, but when they change the option it doesn't save to $activetheme

what do you mean it doesn't save to $activetheme?

Cliffo
03-24-2010, 04:42 AM
the drop down list shows all the folders inside of the themes folder, when the select one of the folders and click input it should save which ever folders name they choose to the $activetheme row in my database.


The code i was using before where they just typed the folders name did save that data to the databse, here it is;


<table align="center">
<br />
<tr align="center">
<td><font color="red"><b>*Active Theme*</b></font></td>
</tr>
<tr align="center">
<td align="center"><input type="text" name="activetheme" value="<?php echo $ds['activetheme']; ?>" /></td>
</tr>
<tr align="center">
<td align="center"><i>(Put the exact name of a theme folder that is located inside /themes)</i></td>
</tr>
<br />
</table>

DJCMBear
03-24-2010, 05:01 AM
replace the code i gave you earlier with this new rewrite:


<?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 name=\"activetheme\">\n";
foreach ($files As $file){
if($current == $file){
$select = " SELECTED";
}
$theme .= "\t<option value=\"$file\"$select>$file</option>\n";
}
$theme .= "</select>\n";
return $theme;
}
?>

Cliffo
03-24-2010, 05:21 AM
Awesome that works, thanks a ton! The only thing not quite the same is it doesnt have the activetheme highlighted by default, just whichever is first in the list. This can be an issue if they are changing othe setting besides the theme.

DJCMBear
03-24-2010, 05:37 AM
Should all be solved now 100%



<?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 name=\"activetheme\">\n";
foreach ($files As $file){
if($current == $file){
$select = " SELECTED";
}else{
$select = "";
}
$theme .= "\t<option value=\"$file\"$select>$file</option>\n";
}
$theme .= "</select>\n";
return $theme;
}
?>

Cliffo
03-24-2010, 05:48 AM
You are amazing, feel free to pm me your paypal email for a donation =)

DJCMBear
03-24-2010, 05:52 AM
i dont know if your get my pm message so thought id ask in here aswell if i send you my paypal email how much you willing to pay?

Cliffo
03-24-2010, 06:19 AM
Thanks again, how can i set this ticket to resolved =)

DJCMBear
03-24-2010, 06:21 AM
I have recieved payment thanks for the donation and if you ever need help again you know where to come. And yup Resolved Issue, I am not sure about that one is it anywhere at the top of this topic?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum