Author Topic: Small add-on for the new release  (Read 800 times)

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Small add-on for the new release
« on: July 02, 2015, 09:54:15 PM »
I'd like to propose an add-on for the next release:

In /framework/frontend.functions.php add the following function:

Code: [Select]
// Function to check if a module position has active modules
// Added by Frederick Giët, July 2 2015
public function hasModules($section_id = 1) {
global $database;
$sql = "select count(section_id) as counter from ".TABLE_PREFIX."sections where page_id = ".intval($this->page_id)." and block = ".intval($section_id);
$result = $database->query($sql);
$page = $result->fetchRow();
return intval($page[0]);
}

With this function you can build smarter templates:

Code: [Select]
<?php if($wb->hasModules(2)){ ?>
<div>
<?php page_content(2); ?>
</div>
<?php ?>
Webdesign/-Development, Freelance

> http://www.medialoft.be

Offline Martin Hecht

  • Betatester
  • **
  • Posts: 544
  • Gender: Male
    • meine Homepage
Re: Small add-on for the new release
« Reply #1 on: July 03, 2015, 10:14:13 AM »
i'm wondering if "hasModules" is a good name for this function. If I understand it correctly, the function checks if there are modules in a particular block of the current page and returns the number of sections found.
In the example for the template it looks like a check, if the $wb class supports modules at all, which is of course not what the function does.
Therefore, I would propose to call it something like "getModulesCountBySe ction"

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #2 on: July 03, 2015, 10:16:51 AM »
You're apparently not a frontend developer :)

We need a small function name for it's ease of use. Maybe countModules. This would be a more correct name since you receive the amount
Webdesign/-Development, Freelance

> http://www.medialoft.be

Offline DarkViper

  • Forum administrator
  • *****
  • Posts: 2976
  • Gender: Female
Re: Small add-on for the new release
« Reply #3 on: July 03, 2015, 10:18:51 AM »

In /framework/frontend.functions.php add the following function:
Quote
   // Function to check if a module position has active modules
   // Added by Frederick Giët, July 2 2015
   public function hasModules($section_id = 1) {
      global $database;
      $sql = "select count(section_id) as counter from ".TABLE_PREFIX."sections where page_id = ".intval($this->page_id)." and block = ".intval($section_id);
      $result = $database->query($sql);
      $page = $result->fetchRow();
      return intval($page[0]);
   }
The red marked code will throw a fatal error. (frontend.functions. php is not a class..)
There is no care for page settings. Not every visitor is allowed to see each page, not every section is visible at all times...
To give you an idea, i post a (not 100%) corrected version..
Code: [Select]
<?php
/**
 * returns true, if Block 'n' on current page has active Sections AND
 * the visitor is allowed to see those sections
 * @global object $database
 * @param integer $iBlockId
 * @return bool
 */
function hasBlockVisibleSections($iBlockId 1)
{
    global 
$database$wb;
    
// first test if the page itself has visible content
    
if ($wb->page_access_denied || $wb->page_no_active_sections) {
        return 
false;
    }
    
// now test if the requested block has active sections
    
$iNow time();
    
$sql 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'sections` '
         
'WHERE `page_id`='.intval($wb->page['page_id']).' AND '
         
.       '`block`='.intval($iBlockId).' AND '
         
.       '(`publ_start`=0 OR `publ_start`<='.$iNow.') AND '
         
.       '(`publ_end`=0 OR `publ_end`>='.$iNow.')';
    return (bool)
$database->get_one($sql);
}
The function page_content($iBlock) is part of file frontend.functions.php
From this it's a logic decision to place the depending function hasBlockVisibleSections($iBlock) in that file too.
Code: [Select]
<?php if(hasBlockVisibleSections(2)){ ?>
<div>
<?php page_content(2); ?>
</div>
<?php ?>
« Last Edit: July 03, 2015, 10:25:32 AM by DarkViper »
Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen

"You have to take the men as they are… but you can not leave them like that !" :-P
Das tägliche Stoßgebet: Oh Herr, wirf ihnen Hirn vom Himmel !

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #4 on: July 03, 2015, 10:21:18 AM »
i'm sorry, i meant class.frontend

i have a working version here, I've tested it naturally :)
Webdesign/-Development, Freelance

> http://www.medialoft.be

fischstäbchenbrenner

  • Guest
Re: Small add-on for the new release
« Reply #5 on: July 03, 2015, 10:49:24 AM »
Why dont you do this thing as it is usually done?:

Code: [Select]
ob_start();
page_content(2);
$page_content_2 = ob_get_contents();
ob_end_clean();

if ($page_content_2 != '') {...}

A page can be empty even though there are modules. Example: Code-Module.

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #6 on: July 03, 2015, 11:06:49 AM »
Because that's a retarded amount of code for a template builder. You're right about the code module, maybe add this check to the function i created.
Webdesign/-Development, Freelance

> http://www.medialoft.be

fischstäbchenbrenner

  • Guest
Re: Small add-on for the new release
« Reply #7 on: July 03, 2015, 11:34:46 AM »
It is a "retarded amount" using a save, simple and fast method - and do lots of database querys instead? I dont think so.



Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #8 on: July 03, 2015, 02:44:54 PM »
Allright, here we go:

First of all:

This:
Code: [Select]
<?php if($wb->hasModules(2)){ ?>
<div>
<?php page_content(2); ?>
</div>
<?php ?>

Is shorter and easier to remember and reuse then this:

Code: [Select]
<?php
ob_start();
page_content(2);
$page_content_2 ob_get_contents();
ob_end_clean();

if ($page_content_2 != '') {
?>

<div>
<?php echo $page_content_2?>
</div>
<?php
}
?>


Especially if you want to use it multiple times in a template to check on different blocks.

Second:

The page_content function runs 1 or 2 queries for the same result:

Code: [Select]
$sql  = 'SELECT `section_id`, `module`, `publ_start`, `publ_end` ';
$sql .= 'FROM `'.TABLE_PREFIX.'sections` ';
$sql .= 'WHERE `page_id`='.$page_id.' AND `block`='.$block.' ';
$sql .= 'ORDER BY `position`';

If no content is found:

Code: [Select]
$sql  = 'SELECT `section_id`, `module`, `publ_start`, `publ_end` ';
$sql .= 'FROM `'.TABLE_PREFIX.'sections` ';
$sql .= 'WHERE `page_id`='.$page_id.' AND `block`='.$block.' ';
$sql .= 'ORDER BY `position`';

While my function only does 1

Code: [Select]
$sql = "select count(section_id) as counter from ".TABLE_PREFIX."sections where page_id = ".intval($this->page_id)." and block = ".intval($section_id);
Where do you see me make 'lots of database querys'?
Webdesign/-Development, Freelance

> http://www.medialoft.be

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #9 on: July 03, 2015, 02:49:49 PM »
Also, the function returns the amount of modules which can be used for other stuff.
Webdesign/-Development, Freelance

> http://www.medialoft.be

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #10 on: July 03, 2015, 02:53:36 PM »
A page can be empty even though there are modules. Example: Code-Module.

Modules with hidden content can be shown in other places then the ones made variable in your template, since they probably won't have to published on that location.
Webdesign/-Development, Freelance

> http://www.medialoft.be

fischstäbchenbrenner

  • Guest
Re: Small add-on for the new release
« Reply #11 on: July 03, 2015, 03:29:15 PM »
There is no need to remember a function - its simply copy and paste. You wont make a new template every day.
And of course you know: a small number of chars doesnt make the code faster.

If you need these functions in your template - why dont you put it into your template code?
No module will need it, no other templates, its only for your template.

And - by the way: What about timebased output? Not only the sections, even modules themself can have whatever-based output. Some modules have an empty view.php (they only do something in the backend)

The only way to find out if there is any output, is to generate the output und THEN check, if there is any content.

Offline iceat

  • Posts: 108
  • Gender: Male
    • Medialoft
Re: Small add-on for the new release
« Reply #12 on: July 03, 2015, 04:08:03 PM »
- Correct, i forgot the date check, a small extension on my query and it's fixed. You could've told me that in a more constructive way :)
- Only i need it? I let you search the forum for my thread in the past where a lot of people were enthousiastic about it when i launched it as a plugin
- Professional site builders build new templates EVERY day
- My piece of code is faster
- ...

You sir, are just plain arrogant and that attitude is why after 10 years WebsiteBaker is still in the stone age.
Webdesign/-Development, Freelance

> http://www.medialoft.be

fischstäbchenbrenner

  • Guest
Re: Small add-on for the new release
« Reply #13 on: July 03, 2015, 04:21:00 PM »
I dont want to argue here.
fact is: there IS a simple working solution which works ALWAYS, in any cases.
Your solution does not work always.

Why dont you put your code into your templates, if you need it? It ist simple!

Quote
You sir, are just plain arrogant and that attitude is why after 10 years WebsiteBaker is still in the stone age.
As you see, my english ist not that good. But this sounds friendly to me - thanks!