sNews Forum

sNews 1.6 (previous version) => Programming => Topic started by: centered on February 03, 2008, 04:22:26 PM

Title: [TESTING] Template Mod v2 Multiple Layouts dB driven
Post by: centered on February 03, 2008, 04:22:26 PM
Now that version 1 is done and can be downloaded here: http://snewscms.com/forum/index.php?topic=6711.0
I can begin working on version 2 for multiple templates. 

My brainstorming here:

1. Add templateid to Articles and Categories dbtables to assign a template to a category or an article/page.
2. Add new field in admin to assing templates to categories and articles/pages
3. Remove default from current template page or change to sitewide template. Sitewide button to regulate adding multiple templates or not.
4. New admin section for multiple layouts (laid out form for #2)???? Unsure
5. For multiple templates - need to add something to show template for home page
      -- Possible solution - add Home/Archives/Contact/Sitemap to database.  Put templates in db, then assign template

???
Somehow give the user a choice for a sitewide template or multiple layouts, or both.  Or just lay it out and let the user decide for each item??


I would like some ideas and feedback.  Thanks!


----------------2.7.08 --- UPDATE-----------------------
Releasing Beta 1 of this project.  Let me know of bugs bugs and all bugs...
http://www.mediafire.com/?2hbnhiboo4c

BUG FIX-
Find:
Code: [Select]
mysql_query("INSERT INTO ".db('prefix')."tempAssoc(name, categoryid, templateid) VALUES('$name', '$catTempID', '$catTemp')");
Replace with:
Code: [Select]
mysql_query("INSERT INTO ".db('prefix')."tempAssoc(name, categoryid, position, templateid) VALUES('$name', '$catTempID', '-1', '$catTemp')");
Find:
Code: [Select]
$cquery = $query." categoryid > 0 AND position = -1 ORDER BY id";Replace:
Code: [Select]
$cquery = $query." categoryid > 0 AND position = -1 ORDER BY catorder,id";
Find case(isset($_POST['add_template'])) replace the add_template and edit_template
Code: [Select]
case(isset($_POST['add_template'])):
// v2
mysql_query("INSERT INTO ".db('prefix')."templates(name, template, css) VALUES('$name', '$template', '$temp_css')");
if ($apply == 'on') {
$tempID = retrieve('id','templates','name',$name);
mysql_query("INSERT INTO ".db('prefix')."tempAssoc(templateid) VALUES('$tempID')");
}
break;
case(isset($_POST['edit_template'])):
// v2
mysql_query("UPDATE ".db('prefix')."templates SET name = '$name', template = '$template', css = '$temp_css' WHERE id = $id LIMIT 1;");
if ($apply == 'on') {
$tempID = retrieve('id','templates','name',$name);
mysql_query("UPDATE ".db('prefix')."tempAssoc SET templateid = '$id'");
}
break;
Title: Re: [WIP] Template Mod v2
Post by: maxou on February 03, 2008, 07:17:57 PM
Hi EQ  :)

I don't understand, with your next mod we can get a template for Categories and Articles.
So there was not a template for all pages but a template for Categories and a template for Articles ?
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 12:37:20 AM
Currently there is one template for sNews. 

If you want more, there are other options:

Single layouts:
My template mod that I linked above, which puts the markup and stylesheets in the database and is switchable through the admin.
Bakercad's Template switcher  - one template for whole site - http://snewscms.com/forum/index.php?topic=5330.0

Multiple Layouts for say categories/articles/pages or combinations:
Joost has a multiple layout 'mod' here: http://snewscms.com/forum/index.php?topic=6133.0
Codetwist does too: http://snewscms.com/forum/index.php?topic=5323.0

Joost and codetwist's functions are good but you would have to manually edit the main index file.  This new way, keeps all files in the database and would be selectable within the admin sections. 

Say i want template one for category two, I can select that in the new or edit section.  I can choose template two for category one, but have template three for a certain article within that category. 

With that being said.  I have figured out further functionality... hopefully:
Currently in my mod, the templates have a default checkbox,
v2 will replace that with a sitewide template checkbox.  If the checkbox is unchecked, then it will display the multiple template options.
   -- Home would be under pages and would have the first template automatically.
   -- The user could select a template for a category, the articles would inherit the category template if no other template is selected.

what I have so far...
Title: Re: [WIP] Template Mod v2
Post by: Keyrocks on February 04, 2008, 03:15:41 AM
Quote from: equilni
Say i want template one for category two, I can select that in the new or edit section.  I can choose template two for category one, but have template three for a certain article within that category. 

With that being said.  I have figured out further functionality... hopefully:
Currently in my mod, the templates have a default checkbox,
v2 will replace that with a sitewide template checkbox.  If the checkbox is unchecked, then it will display the multiple template options.
   -- Home would be under pages and would have the first template automatically.
   -- The user could select a template for a category, the articles would inherit the category template if no other template is selected.
what I have so far...

Keep on stretching your limits there EQ. So... the short form of what you're thinking is... your V2 will work like the V1... ability to insert several templates into the dbase table(s) and then choose which of them each category will use from the Add New or Edit Article or Page panels? If so... that would really be cool. Though, just having the templates in a templates folder would be fine enough.

Which causes me to wonder... could Bakercad's template system be enhanced a bit to do the same thing... without having to put the template stuff into the database through another set of Admin panels? Just a thought... I'll have to pass this by the Baker.
Title: Re: [WIP] Template Mod v2
Post by: Joost on February 04, 2008, 03:56:43 AM
@Jason

Question: Is it because you want to add an online template editor, your so eager to stuff templates in a database?
FYI, if that's the case. I have some experience with CMSMadeSimple. CMSMadeSimple stuffs templates and stylesheets in the database. I find it highly uncomfortable, as there is no highlighter. So most of the time I edited offline, using my favourite editor and pasted it in the textaerea afterwards.
I think files and an upload folder will do.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 03:58:33 AM
Doug,
My assumption is yes, you can mix Joost/Codetwist's Multi-layout's mod with Baker's Template Switcher mod.  Remember, Joost/Codetwist mod does everything manually, nothing is noted in the db or the admin section

This mod would take everything and put in the db and the admin like v1, but allow for multiple layouts/templates based on a Joost/Codetwist/myown combination.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 04:09:08 AM
@Jason

Question: Is it because you want to add an online template editor, your so eager to stuff templates in a database?
FYI, if that's the case. I have some experience with CMSMadeSimple. CMSMadeSimple stuffs templates and stylesheets in the database. I find it highly uncomfortable, as there is no highlighter. So most of the time I edited offline, using my favourite editor and pasted it in the textaerea afterwards.
I think files and an upload folder will do.

Actually, CMSmadesimple is exactly why I did this mod in the first place.  I have done the same procedure as you Joost, work offline then upload the markup to CMSms. I like having the option to have a secondary layout for a page or two if needed.

This mod, should make it easy to do multitemplates.

*EDIT*
To answer your question, v1 added the template editor to store the template and css in the database.  Less of a need for extra files, or bigger files.  Keeps the bandwidth down.
Title: Re: [WIP] Template Mod v2
Post by: Joost on February 04, 2008, 04:36:43 AM
I don't see how it will keep the bandwidth down. It will surely consume more cpu resources.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 04:50:37 AM
I don't see how it will keep the bandwidth down. It will surely consume more cpu resources.

1 less CSS file and the index file is cut to a single line. 

My template v1 mod is 148kb extracted
Default sNews 1.6 extracted is 340kb
My fresh WP install is 4.2MB
My CMSms folder on a fresh install equaling 14.1MB extracted

I would value the space that the CMS would have, so I can put more content if in terms of images, documents, pdf's etc.

Let me ask: what is better, having more files or more data in the database?

How will keeping the templates in the database consume more CPU resources?
Title: Re: [WIP] Template Mod v2
Post by: Joost on February 04, 2008, 06:29:18 AM
The 'one line approach' I can understand, it can be done with 'regular' files also.
Let me provide you my second hand knowledge* ;). Done some reading on the subject.

Files, like stylesheets, don't have to be processed. The server simply sends it to the browser (lets forget about packaging, which is the same for any data). This uses more ram and disk space, but doesn't use that much cpu resources.
Processed data consumes more processing time (cpu) and uses less ram and disk space.

So a lot depends on server configuration (hard- and software). A database server will have a powerful processor and relatively little ram, while a file server might have a lot of ram.
Take note that even when a host provides a separate database server, php is still running on the webserver itself.
What's best? Since I am talking from theoretical knowledge and not from life time experience, I cannot say what's the right balance between processed data and files.

Quote from Wikipedia:

Quote
... a File Server is designed primarily to enable the rapid storage and retrieval of data where the heavy computation is provided by the workstations.

* 'theoretical knowledge' sounds better
Title: Re: [WIP] Template Mod v2
Post by: philmoz on February 04, 2008, 10:00:45 AM
yep, agree with Joost.
Personally, I would register the template location, but not store the template (complete) in db.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 11:35:22 AM
http://www.sitepoint.com/forums/

Go to this link and do a search.  Tell me how long it takes.  That is consuming CPU resources, because it is searching the db through thousands of records and performing heavy computations, then serving up the pages. 

Is a template not like a article, ONE article that is text with some HTML that is processed once? Can I say that a template is text that has some php that is encoded to be stored and then processed once?

This isn't heavy computations nor massive queries we are talking about here...
Code: [Select]
// Version One
function index() {
$result = mysql_query("SELECT template FROM ".db('prefix')."templates WHERE main_temp = 'YES'");
while ($r = mysql_fetch_array($result)) {
$template = $r['template']; $ins = strpos($template, '[/func]');
    if ($ins > 0) {
        $temp = str_replace('[func]', '|&|', $template);
        $temp = str_replace('[/func]', '|&|', $temp);
$temp = explode('|&|', $temp); $num=count($temp)-1; $i=1;
        while ($i <= $num) {
        $func = explode(':|:',$temp[$i]);
        ob_start();
        $returned = call_user_func_array($func[0],explode(',',$func[1]));
        $temp[$i]= ob_get_clean();
        if (empty($temp[$i])){$temp[$i]=$returned;}
          $i=$i+2;
        } $template = implode($temp);
} echo $template; }}

// Builds the css links from the db
function css_links() {
$result = mysql_query("SELECT css FROM ".db('prefix')."templates WHERE main_temp = 'YES'");
while ($r = mysql_fetch_array($result)) {
$style = explode(',',$r['css']);
foreach($style as $style) {
$cssresult = mysql_query("SELECT name,media FROM ".db('prefix')."css WHERE id = '$style'");
while ($cr = mysql_fetch_array($cssresult)) {
echo '<style type="text/css">@import url("index.php?style='.$cr['name'].'") '.$cr['media'].';</style>';
}}
}}

function snews_startup() {
connect_to_db();
// template editor
$styleid = clean($_GET['style']);
$style = retrieve('name','css','name',$styleid);
if (!empty($style)) {
header("Content-type: text/css");
$css = retrieve('css','css','name',$styleid);
echo $css; break;
}
// template editor end



If files are faster, then why aren't text-databases more popular?  Why doesn't sNews store the articles in text files, and categories as folders?
Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 04, 2008, 11:39:48 AM
Jason I agree with Joost and Phimoz. (Personally)

But I'm glad you continue this great mod. I think the user is free of choose.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 11:43:14 AM
yep, agree with Joost.
Personally, I would register the template location, but not store the template (complete) in db.

Why? You would do that for an image or Word Documents or PDFs, but a template can be done differently thanks to you!

Let me ask what is the use then of your Function use in article's mod?  That is how this mod is possible!

Let me ask another question, why the reluctancy to store a template or css file in a database?
Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 04, 2008, 11:52:57 AM
It's a wonderfull ideia put multiple templates in table, and user can switch to template as he wishes (by temporal time). Like Cristmas, Summer etc.

I like your ideia Jason.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 12:03:09 PM
Jason I agree with Joost and Phimoz. (Personally)

But I'm glad you continue this great mod. I think the user is free of choose.
It's a wonderfull ideia put multiple templates in table, and user can switch to template as he wishes (by temporal time). Like Cristmas, Summer etc.

I like your ideia Jason.

Rui, sometimes you confuse me.  You agree with Joost and Philmoz that this is not a good idea, but then you think it is a wonderful idea. 

I am lost...
Title: Re: [WIP] Template Mod v2
Post by: Joost on February 04, 2008, 12:03:42 PM
If files are faster, then why aren't text-databases more popular?  Why doesn't sNews store the articles in text files, and categories as folders?

Serverside caching, the way - as I recall - you've been exploring and like the one posted by Meint,  is in fact creating a text-database. ;)

The first sNews engine I've used has got a text-database. 1.3. However, after that it hasn't been maintained.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 12:07:56 PM
1.4 textdb came out afterwards.  Then I have been trying to make a 1.5/1.6 version myself.. stupid substring isn't supported!

Again, why the reluctancy to store a template and css in the database?
Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 04, 2008, 12:17:15 PM

Rui, sometimes you confuse me.  You agree with Joost and Philmoz that this is not a good idea, but then you think it is a wonderful idea. 

I am lost...

I'm sorry Jason, don't get me wrong.

Personally I don't know if I will use this mod, because I'll expect several posts on Civil Engineering and lots of articles, but for my clients I'll put this mod for sure.
Title: Re: [WIP] Template Mod v2
Post by: philmoz on February 04, 2008, 12:29:47 PM
As I see it, you would be adding an extra layer of complexity to the system.
Currently, If I want to update css or layout, I can make changes locally, and ftp results into correct place.

If developed locally, and stored in db, I would have to (in some manner) have a way of transfering my new stuff to update the online old stuff, without possibility of copy/paste error.

Really, it comes down to a similar rant over the net, about storing images in db, or their locations.
Granted, many, many images will bloat the db, and likely cause strain, whereas the code you are proposing, will be 'reasonably' negligible in  size.

It is why I used the word "personally" in my original reply. My personalchoice, rightly or wrongly, would be to store location not code.
Easier to write, easier to manage IMHO.. :D
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 12:47:19 PM
I'll expect several posts on Civil Engineering and lots of articles

So your saying that this mod is bad for those using alot of stored data in the database??  I am trying to understand why is it bad compared to why it is good for others and your clients

As I see it, you would be adding an extra layer of complexity to the system.
Currently, If I want to update css or layout, I can make changes locally, and ftp results into correct place.

And that is not complex enough?

Write the index, write the css, possibly create the folders, upload it into the FTP, check permissions, refresh page, wish for the best.
This way:
Write the index, write the css, Control-a, control-c, go to browser, control v, save (or command for the apple users)

Wow this is how CMSmadesimeple and Textpattern does this...

If developed locally, and stored in db, I would have to (in some manner) have a way of transfering my new stuff to update the online old stuff, without possibility of copy/paste error.

Would that error also come from the FTP program too?  FTP copies your files then pastes them on the server.  What is the difference?

I think there is more chance of a mess copying and pasting line by line mods than selecting an entire html file and pasting it.

Really, it comes down to a similar rant over the net, about storing images in db, or their locations.
Granted, many, many images will bloat the db, and likely cause strain, whereas the code you are proposing, will be 'reasonably' negligible in  size.
Agreed, I wouldn't store an IMAGE in the database, but I would store text in the database. 

This is text until it is processed:
Code: [Select]
<div id="logo"><h1>[func]s:|:website_title[/func]</h1></div>Or
Code: [Select]
body { background: #fff; }
Quote
It is why I used the word "personally" in my original reply. My personalchoice, rightly or wrongly, would be to store location not code.
Easier to write, easier to manage IMHO..

You have voiced your personal opinion, I am voicing mine.  But i do question and seek knowledge, because perhaps my idea may be wrong.

Title: Re: [WIP] Template Mod v2
Post by: Keyrocks on February 04, 2008, 02:43:15 PM
@ EQ.... I don't think anyone is saying that your idea is... "bad" or "wrong". I think it's more a question of efficiency... as in is one approach more efficient than the other in terms of resource consumption (load) and where the consumption (loading) occurs. For me... it doesn't matter a whole bunch where the load is (server or users memory, etc.) as long as the site continues to peform without adverse affects.

Being able to create new mods - for sNews or any other CMS - is fun. It allows you to create new features that you want to have in your CMS. You do it first for yourself and... with sNews... you share it with your fellow members in case they might like to use it too. That doesn't mean we all (fellow members) will find it useful.

In my case... I would not use this mod in a client's site mainly because my clients are not script writers... they just want to add, edit and delete content. If they want different templates for each category, then I would make and provide the templates as hard-coded files and they are happy. Templates do not need to change on a daily basis as content does... once they are ready for use... they stay that way... so they are fine as static files. I take care of FTP'ing file changes to their sites for them... they don't want or need to FTP anything.

I base my "personal opinions" on the premise that the templates are something the developer takes care of just like the rest of the backend stuff.  :)
Title: Re: [WIP] Template Mod v2
Post by: Fred K on February 04, 2008, 03:13:37 PM
Quote from: Keyrox
the premise that the templates are something the developer takes care of just like the rest of the backend stuff

I'd agree with most of what Key's just said. As for the mod (or mods) itself, I'm going to test run v1 before I say anything. But from the mod post it appears as if template _creation_ is done within the Admin interface, and that's where my mind cringes a little. Probably just gut reaction, or not grasping the concept fully, and as I said, I'm going to test it first.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 04:37:54 PM
In my case... I would not use this mod in a client's site mainly because my clients are not script writers... they just want to add, edit and delete content. If they want different templates for each category, then I would make and provide the templates as hard-coded files and they are happy. Templates do not need to change on a daily basis as content does... once they are ready for use... they stay that way... so they are fine as static files. I take care of FTP'ing file changes to their sites for them... they don't want or need to FTP anything.

I base my "personal opinions" on the premise that the templates are something the developer takes care of just like the rest of the backend stuff.  :)

Right, Fred made the same statements when I started version 1 of this mod, and I included a on/off switch to turn the whole thing on or off.  When it's off, it looks like a normal installation.

Not saying this is wrong or right, but what is the best way of making this easier for users and better for everyone.

Fred try it out and come back to me.  Turn the thing on to use it

UPDATED!!
Here is my to do list:
1. Change main_temp to function as “Show on all categories and article pages”   DONE
1a. Remove main_temp from db table DONE
1b. If checked, then make all templateids in 3 to the selected template  DONE
2. Remove templateid from articles and categories table (my version)  DONE
3. Add new table tempAssoc – with fields (id, name, categoryid, catorder, articleid, templateid)  DONE 
4. Add to new table the default pages  DONE
4a. Add to processing new insert and update into processing for tempAssoc
     ------- Template DONE
     ------- Categories/Catorder/Articles – Need to be done
----------------------------------------------------------------------------
5. Add select box for templates to form_articles/categories - If template editor is on
6. Add new admin section (function) called Structure.  Show entire site structure similar to sitemap, have select boxes with templates to be selected.  Have save button at end. If template editor is on
7. Add 5,6 to processing. If template editor is on
8. Add new function to determine template id.  Utilize Joost's function for multiple layouts.  Return the $templateid.  If template editor is on
9. Adjust index(), css_links(), and check_default() for new sql: id=$templateid
10. Test.
Title: Re: [WIP] Template Mod v2
Post by: Fred K on February 04, 2008, 08:32:54 PM
Jason, I can't use this package. Here's what I did:


This is what I got:
Quote
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Apache/2.0.59 (Unix) PHP/5.2.5 DAV/2 Server at localhost Port 8888

So you see, I cannot use his package...
Ideas?

EDIT:/* Just to confirm that things are ok, generally, I switched to another database and copypasted in some code from another template in the index file. Works perfectly. So there's something wrong witht the package files. */EDIT

EDIT 2:/* Just tried with converting text encoding in all files to UTF-8 instead of since that's what I always set my db collation to, and also checked that line endings were set to Unix, 'cause that usually works. I even went so far as to copypaste all of the code into fresh documents to make sure that UTF-8 encoding and Unix line endings would stick (sometimes they don't) ... and I still get Error 500 ...*/EDIT
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 08:47:56 PM
Fred,

Can you:
1. Redownload or unzip the package
2. Create db and add your db information to sNews.php
3. Run it normally before you try anything fancy.

I built this on a Mac so this so should as is.
Title: Re: [WIP] Template Mod v2
Post by: Fred K on February 04, 2008, 09:16:37 PM
Yeah, it works when I manually unzip the archive. Auto-decompressing (when download link is simply clicked) didn't work. Usually it just works though...

Anyway, my only note so far is regarding the "Attach stylesheet" feature in the Template Editor. It has no option control -- as far as I can tell (but I might be misunderstanding things) there's no way to unattach a stylesheet that is attached to a template? If you look at the basic template, there are two stylesheets attached to it. Now, suppose I edit them to merge the two into one basic stylesheet ... I can't unattach the one that I don't need ... unless I go over to the stylesheets panel and delete the unwanted stylesheet? Is that a correct understanding, or am I out to lunch? :P

(Which reminds me, completely off topic, that I need to make dinner...)
Title: Re: [WIP] Template Mod v2
Post by: Fred K on February 04, 2008, 09:19:27 PM
Side note -- if default charset is set to UTF-8 in sql file, then snews.php and index.php files should be saved with UTF-8 encoding as well. Otherwise it is likely that there will be problems with 'foreign' characters -- accents, diacritics, whathaveyou.
Just thought I'd mention it.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 10:03:06 PM
Anyway, my only note so far is regarding the "Attach stylesheet" feature in the Template Editor. It has no option control -- as far as I can tell (but I might be misunderstanding things) there's no way to unattach a stylesheet that is attached to a template? If you look at the basic template, there are two stylesheets attached to it. Now, suppose I edit them to merge the two into one basic stylesheet ... I can't unattach the one that I don't need ... unless I go over to the stylesheets panel and delete the unwanted stylesheet? Is that a correct understanding, or am I out to lunch? :P

Thanks for looking at it.  I will check it out.  I didn't note that problem before, but I will test when I get back home. 

Overall what was your opinion?
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 10:03:34 PM
Side note -- if default charset is set to UTF-8 in sql file, then snews.php and index.php files should be saved with UTF-8 encoding as well. Otherwise it is likely that there will be problems with 'foreign' characters -- accents, diacritics, whathaveyou.
Just thought I'd mention it.

hmm I think smultron is doing a number on my files....

Also seeing 3 out of the 4 top mod threads about multiple templates, I think there may be a market for this mod....
Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 04, 2008, 11:09:00 PM

So your saying that this mod is bad for those using alot of stored data in the database??  I am trying to understand why is it bad compared to why it is good for others and your clients


I never told that Jason, I said "I don't Know" for my site, means I must try if it is fast. Like I said: "I like your ideia".
I can garanty to you, If is efficient with a lots data in database, I'll add this mod to my my website.

If you need help, as always I'm here.

One ideia, added by You or by me, to this mod:
- Making change template automatically by date or event, will be nice.

About my sNews Events, this mod will be perfect.

Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 04, 2008, 11:17:44 PM
I think about it. I change my mind.

Now I think is good ideia, because in only text. Images can be put in only folder.
So We have

snews.php
.htaccess (can be removed if you use your snews without .htaccess)
folder ->images
readme.htm (instructions)

and about style.css is in table ?
Title: Re: [WIP] Template Mod v2
Post by: centered on February 04, 2008, 11:31:19 PM
All here: http://snewscms.com/forum/index.php?topic=6711.msg45845#msg45845
Title: Re: [WIP] Template Mod v2
Post by: centered on February 05, 2008, 01:18:40 AM
Code: [Select]
function get_tempID() {
$categorySEF = get_id('category');
$catID = retrieve('id','categories','seftitle',$categorySEF);
$articleSEF = get_id('article');
$artID = retrieve('id','articles','seftitle',$articleSEF);
if (!empty($articleSEF)) {
$tempID = retrieve('templateid','tempAssoc','articleid',$artID); }
elseif (!empty($catID)) {
$tempID = retrieve('templateid','tempAssoc','categoryid',$catID); }
elseif (empty($catID)) {
$defaultSEF = ucfirst(strtolower($categorySEF));
$tempID = retrieve('templateid','tempAssoc','name',$defaultSEF); }
elseif (check_category($categorySEF) == true) {
$tempID = retrieve('templateid','tempAssoc','name','Home'); }
return $tempID;
}

// Builds the template from the database and parse the [func] tags
function index() {
// v2
$tempID = get_tempID();
$result = mysql_query("SELECT * FROM ".db('prefix')."templates WHERE id = $tempID LIMIT 1"); //  WHERE main_temp = 'YES'
while ($r = mysql_fetch_array($result)) {
$template = $r['template']; $ins = strpos($template, '[/func]');
    if ($ins > 0) {
        $temp = str_replace('[func]', '|&|', $template);
        $temp = str_replace('[/func]', '|&|', $temp);
$temp = explode('|&|', $temp); $num=count($temp)-1; $i=1;
        while ($i <= $num) {
        $func = explode(':|:',$temp[$i]);
        ob_start();
        $returned = call_user_func_array($func[0],explode(',',$func[1]));
        $temp[$i]= ob_get_clean();
        if (empty($temp[$i])){$temp[$i]=$returned;}
          $i=$i+2;
        } $template = implode($temp);
} echo $template; }}

// Builds the css links from the db
function css_links() {
// v2
$tempID = get_tempID();
$result = mysql_query("SELECT * FROM ".db('prefix')."templates WHERE id = $tempID"); // WHERE main_temp = 'YES'
while ($r = mysql_fetch_array($result)) {
$style = explode(',',$r['css']);
foreach($style as $style) {
$cssresult = mysql_query("SELECT * FROM ".db('prefix')."css WHERE id = '$style'");
while ($cr = mysql_fetch_array($cssresult)) {
echo '<style type="text/css">@import url("index.php?style='.$cr['name'].'") '.$cr['media'].';</style>';
}}
}}
(http://img232.imageshack.us/img232/7136/cattempgy0.jpg)
http://img232.imageshack.us/img232/7136/cattempgy0.jpg
Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 05, 2008, 01:30:09 AM
Hello again Jason.

I think your index(); has a IE 7.0 bug (for me is not working). I start see your code.

Quote
function index() {
   $result = mysql_query("SELECT * FROM ".db('prefix')."templates WHERE main_temp = 'YES' LIMIT 1");
...
Title: Re: [WIP] Template Mod v2
Post by: centered on February 05, 2008, 04:13:45 AM
a Little teaser:
(http://img225.imageshack.us/img225/1540/selectiondw2.jpg)
http://img225.imageshack.us/img225/1540/selectiondw2.jpg


Title: Re: [WIP] Template Mod v2
Post by: Rui Mendes on February 05, 2008, 10:54:20 AM
Excellent ideia, your get_tempID, gives me ideia create a newfunction for Gallery
Title: Re: [WIP] Template Mod v2
Post by: Patric Ahlqvist on February 05, 2008, 10:55:33 AM
Teasers aren't fair, Jase ;)
Title: Re: [WIP] Template Mod v2
Post by: centered on February 05, 2008, 12:58:02 PM
Excellent ideia, your get_tempID, gives me ideia create a newfunction for Gallery

Thanks.  If you have an idea why home or anything in cat_list is not working, let me know.

Teasers aren't fair, Jase ;)

oh but they are!

EDIT ----------------------------------------------

NEW IDEA -
 - Since I have Home, Archive, Contact, and Sitemap in the tempAssoc Table, might as well put Administration and 404 in the table as well, since others may want to utilize thier own Admin and 404 templates.

Db table tempAssoc looks like this:
Fields: id, name, categoryid, catorder, articleid, pageid, templateid
Ex.        1, Home, NULL, NULL, NULL, NULL, 1
   2, catOne, 1, 1, NULL, NULL, NULL, 2
   3, catTwo, 2, 0, NULL, NULL, NULL, 2
   4, artOne, NULL, NULL, 1, NULL, 1
   5, pageOne, NULL, NULL, NULL, 2, 1


Progress:
 - There is still alot of work to do and it is very early to determine anything definitive.
Right now, I can:
 - Create/edit a template and apply it to all pages
 - Create/edit categories and pages and select templates (picture above) (need to work on Pages)
 - Show a new section called Structure to outline the whole site (teaser picture) and choose templates for each.  (still a heavy WIP - no pages and no save button)
 - Other than Home (/) and anything in the admin, The functionality works, but alas flawed

Title: Re: [WIP] Template Mod v2
Post by: centered on February 06, 2008, 12:51:49 AM
Thanks to codetwist, switching works perfectly!

Code: [Select]
$l['adminSEF'] = array(login,administration,admin_category,admin_article,article_new,extra_new,page_new,categories,articles,extra_contents,pages,settings,files,logout,templates,admin_templates,stylesheets,admin_stylesheets,structure);


} return $l[$variable];
}
/*** END OF SETTINGS & VARIABLES, EDIT BELOW THIS LINE ONLY IF YOU KNOW WHAT YOU'RE DOING ***/
// template editor - new functions

// v2
function getContentKeyData() {
   //
   $strResult = 'unknown';
   $arrResult = array();
   //
   $homeSEF      = s('home_sef');
   $homePageId   = s('display_page');
   //
   //if ( $categorySEF  = getGetParm('category') ) {
   if ( $categorySEF  = get_id('category') ) {
      $categoryId = retrieve('id', 'categories', 'seftitle', $categorySEF);
   }
   else {
      $categorySEF = $homeSEF;
   }
   $categoryId = isset($categoryId) ? $categoryId : 0;
   //if ( $itemSEF = getGetParm('article') ) {
   if ( $itemSEF = get_id('article') ) {
      if ( $itemId = retrieve('id', 'articles', 'seftitle', $itemSEF) ) {
         $itemPosition = retrieve('position', 'articles', 'id', $itemId);
      }
   }
   $itemId = isset($itemId) ? $itemId : 0;
   if ( $categoryId == 0 )  {
      if ( check_category( $categorySEF ) && ($categorySEF != $homeSEF) ) {
         // Dealing with built-in pages - archive, sitemap, contact
         $strResult = 'built-in';
      }
      else {
         if ( $itemId > 0 ){
            // Specific item requested (either page or article)
            if ( $itemPosition > 0 ) {
               if ( $itemPosition == 3 ) {
                  $strResult = 'page';
               }
               elseif ( $itemPosition == 1 ) {
                  $strResult = 'article';
               }
            }
         }
         elseif ( ($categorySEF == $homeSEF) && ($homePageId > 0) ) {
            // Dealing with home replacement page
            $strResult = 'homepage';
         }
         else {
            // Dealing with built-in home
            $strResult = 'home';
         }
      }
   }
   elseif ( $itemId > 0 ) {
      // Dealing with real category
      $strResult = 'article';
   }
   elseif ( $categoryId > 0 ) {
      // Dealing with real category
      $strResult = 'category';
   }
   $arrResult['itemType'] = $strResult;
   switch ( $strResult ) {
      case 'category' :
         $arrResult['itemId'] = $categoryId;
      case 'home' :
      case 'built-in' :
         $arrResult['itemSEF'] = $categorySEF;
         break;
      case 'page' :
      case 'article' :
         $arrResult['itemSEF'] = $itemSEF;
         $arrResult['itemId'] = $itemId;
         break;
      case 'homepage' :
         $arrResult['itemSEF'] = $categorySEF;
         $arrResult['itemId'] = $homePageId;
         break;
      case 'unknown' :
      default :
         break;
   }
   return $arrResult;
}

// v2
function get_tempID() {
$curItem = getContentKeyData();
$itemSEF  = $curItem['itemSEF'];
$artID = retrieve('id','articles','seftitle',$itemSEF);
$catID = retrieve('id','categories','seftitle',$itemSEF);
$list = l('adminSEF'); if (in_array($itemSEF, $list)) { $listed = $itemSEF; }
if (!empty($artID)) {
$tempID = retrieve('templateid','tempAssoc','articleid',$artID); }
elseif (empty($artID) && !empty($catID)) {
$tempID = retrieve('templateid','tempAssoc','categoryid',$catID); }
elseif (empty($artID) && empty($catID) && !empty($listed)) {
$tempID = retrieve('templateid','tempAssoc','name','Administration'); }
elseif (empty($artID) && empty($catID) && empty($listed)) {
$defaultSEF = ucfirst(strtolower($itemSEF));
$tempID = retrieve('templateid','tempAssoc','name',$defaultSEF); }
elseif ($itemSEF = 'home') { $tempID = retrieve('templateid','tempAssoc','name','Home'); }
return $tempID;
}

// Builds the template from the database and parse the [func] tags
function index() {
// v2
$tempID = get_tempID();
$result = mysql_query("SELECT template FROM ".db('prefix')."templates WHERE id = $tempID LIMIT 1"); //  WHERE id = $tempID // WHERE main_temp = 'YES'
while ($r = mysql_fetch_array($result)) {
$template = $r['template']; $ins = strpos($template, '[/func]');
    if ($ins > 0) {
        $temp = str_replace('[func]', '|&|', $template);
        $temp = str_replace('[/func]', '|&|', $temp);
$temp = explode('|&|', $temp); $num=count($temp)-1; $i=1;
        while ($i <= $num) {
        $func = explode(':|:',$temp[$i]);
        ob_start();
        $returned = call_user_func_array($func[0],explode(',',$func[1]));
        $temp[$i]= ob_get_clean();
        if (empty($temp[$i])){$temp[$i]=$returned;}
          $i=$i+2;
        } $template = implode($temp);
} echo $template; }}

// Builds the css links from the db
function css_links() {
// v2
$tempID = get_tempID();
$result = mysql_query("SELECT * FROM ".db('prefix')."templates WHERE id = $tempID LIMIT 1"); //  WHERE id = $tempID // WHERE main_temp = 'YES'
while ($r = mysql_fetch_array($result)) {
$style = explode(',',$r['css']);
foreach($style as $style) {
$cssresult = mysql_query("SELECT * FROM ".db('prefix')."css WHERE id = '$style'");
while ($cr = mysql_fetch_array($cssresult)) {
echo '<style type="text/css">@import url("index.php?style='.$cr['name'].'") '.$cr['media'].';</style>';
}}
}}


Title: Re: [WIP] Template Mod v2
Post by: centered on February 06, 2008, 12:43:32 PM
Alpha Testing ONLY!
Things Break and do not work in alpha versions!!

You can edit or create a new category/article/page and assign a template to it then play around with the site

Structure DOES NOT WORK
http://www.mediafire.com/?ddwzxdfmxrc

To do:
1. Save button in structure
2. Remove pageid from tempAssoc dbTable
3. Filter Template selection from Extras.
4. Add 404 to tempAssoc
5. More testing

Title: Re: [WIP] Template Mod v2
Post by: Sven on February 07, 2008, 07:49:24 AM
a Little teaser:
(http://img225.imageshack.us/img225/1540/selectiondw2.jpg)
http://img225.imageshack.us/img225/1540/selectiondw2.jpg
:o
Am I still dreaming?
Gonna have one more coffee and be back to see.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 07, 2008, 10:20:58 AM
Don't thank me too quickly...
.. but you can drool some more if you like!
(http://img230.imageshack.us/img230/2998/blahgm2.jpg)
http://img230.imageshack.us/img230/2998/blahgm2.jpg

Rui, I may need assistance if you may, I can't get Sven's dream to save anything...

How would you go about saving the above picture?!?!

Code I currently have is
Code: [Select]
function t_select($tempID) {
echo ' <select name = "strucID">';
$tempresult = mysql_query("SELECT * FROM ".db('prefix')."templates ORDER BY id ASC");
while ($t = mysql_fetch_array($tempresult)) {
echo '<option value="'.$t['id'].'"';
if ($t['id'] == $tempID) { echo ' selected="selected"'; }
echo '>'.$t['name'].'</option>';
}
echo '</select>';
}

function structure() {
if (s('enable_editor') == 'on') {
echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', 'index.php?action=process&amp;task=save_structure', '');
echo html_input('fieldset', '', '', '', '', '', '', '', '', '', '', '', '', '', l('structure'));
echo '<h3><strong>'.l('pages').'</strong></h3>';
$query = "SELECT id,name,templateid,categoryid FROM ".db('prefix')."tempAssoc WHERE";
// Default pages and admin
$dquery = $query." id IN(1,2) ORDER BY id";
$dresult = mysql_query($dquery);
while ($d = mysql_fetch_array($dresult)) {
echo '<p>'.$d['name'];
t_select($d['templateid']);
echo '</p>';
}
// User Pages
$pquery = $query." position = 3 ORDER BY id";
$presult = mysql_query($pquery);
while ($p = mysql_fetch_array($presult)) {
echo '<p>'.$p['name'];
t_select($p['templateid']);
echo '</p>';
}
$dquery = $query." id IN(3,4,5) ORDER BY id";
$dresult = mysql_query($dquery);
while ($d = mysql_fetch_array($dresult)) {
echo '<p>'.$d['name'];
t_select($d['templateid']);
echo '</p>';
}
// Categories and Articles
echo '<h3><strong>'.l('articles').'</strong></h3>';
echo '<p name="'.l('home').'">'.l('home');
t_select('Home');
echo '</p>';
$aquery = $query." position = 1 AND categoryid = 0";
$aresult = mysql_query($aquery);
while ($a = mysql_fetch_array($aresult)) {
echo '<p class="subcat">'.$a['name'];
t_select($a['templateid']);
echo '</p>';
}
$cquery = $query." categoryid > 0 AND position = -1 ORDER BY id";
$cresult = mysql_query($cquery);
while ($c = mysql_fetch_array($cresult)) {
echo '<p>'.$c['name'];
t_select($c['templateid']);
echo '</p>';
$artquery = $query." position = 1 AND categoryid = ".$c['categoryid']." ORDER BY id";
$artresult = mysql_query($artquery);
while ($art = mysql_fetch_array($artresult)) {
echo '<p class="subcat">'.$art['name'];
t_select($art['templateid']);
echo '</p>';
}
}
echo html_input('submit', 'save', 'save', l('save'), '', 'button', '', '', '', '', '', '', '', '', '');
echo '</fieldset></form>';
}
}

I have tried putting a name="name" in the paragraph tag where is shows the name: echo '<p>'.$d['name']; Then trying to return the POST value, but there is no value returned.

Also when I change multiple items, the POST data returns 1 item

BREAK IT DOWN FURTHER...
What I have:
Code: [Select]
<form method="post" action="index.php?action=process&amp;task=save_structure" id="post">
<p>itemName-A
   <select name = "strucID">
      <option value="1" selected="selected">Option1</option>
      <option value="2">Option2</option></select>
</p>
<p>itemName-B
   <select name = "strucID">
      <option value="1">Option1</option>
      <option value="2" selected="selected">Option2</option></select>
</p>
.. etc
<input type="submit" name="save" id="save" class="button" value="Save" />
</form>

This has me stumped as to how to return the POST data for each item and it's corresponding name.

When it is done, I would like it to go into the database:
Code: [Select]
mysql_query("UPDATE dbTable SET structureID = '$strucID' WHERE name = '$itemName';");
Everythign in-between is up in the air

Title: Re: [WIP] Template Mod v2
Post by: Sven on February 07, 2008, 12:19:59 PM
Awakened now...
What a challenge, Jason!
This gonna be the ultimate template manager.
I wish I could help you, but,
you know...
yes: you know. ;)
Cheer on.
Title: Re: [WIP] Template Mod v2
Post by: centered on February 07, 2008, 12:29:46 PM
Awakened now...
What a challenge, Jason!
This gonna be the ultimate template manager.
I wish I could you, but,
you know...
yes: you know. ;)
Cheer on.

Thanks, Actually the filed version of this will be the one everyone wants, that will be version 3 of this mod.  Pretty much it will be how Joost/Codetwist's mods are for multiple layouts, except instead of the template and css in the database, they will be in a templates folder.  Like Bakercad's template switcher, I will be able to get all php files and put them in a select box, then save the selected file to the db and call it up. 

Similar to this mod and bakercad's mod, v3 will have a sitewide checkbox to apply a template to the whole site.  I want to make this 'Structure' work so it can be included in v3. Since it will be a good part of this whole mod
Title: Re: [WIP] Template Mod v2
Post by: centered on February 07, 2008, 12:49:53 PM
hmm I have an idea.... add a name to the option value... comma delimiter, then explode it!
Code: [Select]
function t_select($name,$tempID) {
echo ' <select name="strucID[]">';
$tempresult = mysql_query("SELECT id,name FROM ".db('prefix')."templates ORDER BY id ASC");
while ($t = mysql_fetch_array($tempresult)) {
echo '<option value="'.$name.','.$t['id'].'"';
if ($t['id'] == $tempID) { echo ' selected="selected"'; }
echo '>'.$t['name'].'</option>';
}
echo '</select>';
}

....
$dresult = mysql_query($dquery);
while ($d = mysql_fetch_array($dresult)) {
echo '<p>'.$d['name'];
t_select($d['name'],$d['templateid']);
echo '</p>';
}
.....

Down in processing:
Code: [Select]
$strucID = $_POST['strucID'];
and further:
Code: [Select]
switch ($task) {
// v2
// in testing
case 'save_structure':
if (isset($_POST['save'])) {
// test code!!!
$home = explode(',',$strucID['0']);
$itemName = $home['0'];
$templateID = $home['1'];
echo $itemName.'/'.$templateID;

mysql_query("UPDATE ".db('prefix')."tempAssoc SET templateid = '$templateID' WHERE name = '$itemName';");
echo notification(0,'','structure');
}
break;

$home['0'] gives me the itemName needed $home['1'] gives me the new templateid needed....

.. which works BTW!!

BUT!!!

Now how to get that in a foreach statement with an incremental value....?!?!?!?!?!?


EDIT ------------------

I have a feeling it may be close to :
Code: [Select]
0, Incremental value
foreach ($strucID as $id) {
$newID = explode(',',$id[$incremental_value]);
$itemName = $newID['0'];
$templateID = $newID['1'];
}
mysql_query("UPDATE ".db('prefix')."tempAssoc SET templateid = '$templateID' WHERE name = '$itemName';");
Title: Re: [WIP] Template Mod v2
Post by: centered on February 07, 2008, 06:21:06 PM
got it working!!

Now to fix some bugs... expect a beta this weekend hopefully.  I am getting a new computer tommorow, hence the delay...
Title: Re: [TESTING] Template Mod v2 Multiple Layouts
Post by: centered on February 08, 2008, 12:45:56 AM
Updated first post Beta release

More teasers:
(http://img218.imageshack.us/img218/3825/cateditdr4.jpg)
http://img218.imageshack.us/img218/3825/cateditdr4.jpg

(http://img352.imageshack.us/img352/3913/templateviewme4.jpg)
http://img352.imageshack.us/img352/3913/templateviewme4.jpg
This is to fix Fred's 'bug' of selecting no stylesheet
Title: Re: [TESTING] Template Mod v2 Multiple Layouts dB driven
Post by: Edgardo on August 02, 2012, 05:25:55 AM
Hi, and wooow, terrific, grate, dont know what to sead, Is working? Just read the entire post and .. was looking for a way to test thigs and templates and css, Im new on this, and is been dificult to find a good looking site, and also some mods dont get as nice as I spect, this is what Im looking for, so where sould i start?, the only thing is this looks like the never ending story, jja ;D