PDA

View Full Version : How to show php widget in article pages only?


SilverBoy
01-27-2014, 11:48 PM
Hi

I tried to display a PHP widget in article preview page and hide it from the layout in the section page, but I failed until now :(

So, any body can help me in this? and how can I pass variables to this widget? like parent section id, author name and author id for example?

Another question, how I can show any other widget like "last articles" widget in the article page only and hide it from the section page or show it in section page and hide it in the article preview page? what a specific condition I must add to the template?

Thanks in advance.

SilverBoy
01-29-2014, 05:25 AM
Please help !!

tbworld
01-29-2014, 05:30 AM
Hi

I tried to display a PHP widget in article preview page and hide it from the layout in the section page, but I failed until now :(

So, any body can help me in this? and how can I pass variables to this widget? like parent section id, author name and author id for example?


Lets see your widget code and maybe I can give you a hand with the first part of your question. The second part of your question confuses me, as I do not understand on what condition you need to display the widget -- except not on both renders.

SilverBoy
01-29-2014, 06:01 AM
Thank you for your reply

I tried in the first step to add condition to the widget template (I just use the default php eval widget to understand things), but I didn't figure out what is the condition that I must use in template to say this page is section or article !!

Then I tried to make plugin to bypass variables and stuck there too, I don't know even to let the plugin know which nodeid he is in !!

So by simple words, could you please help me in make widgets appear in article page of some sections (not all article pages) and hide them from their parent sections?

To be more specific, I want to add this php widgets,
1- the last articles from the same author and show it in the articles in opinion section articles only, and of curse it doesn't make sense to show it in the section page it self, so I need to bypass author information to the widget to make the query.
2- the related articles to this article, I already add them as special field in the DB when add/edit articles, so I need to by pass these fields too (It stored in cms_node table)
3- the last articles from this section, so I want to use the default recent article widget but need a condition to prevent it from appearing in the section page.

And of curse I need to bypass the issection value to the widget to display the widget of hide it.

I wish I made things clear to you now :).

Thanks in advance.

--------------- Added 1390979373 at 1390979373 ---------------

I forget the forth widget that I want, I want to add the most popular articles widget (the code of it is here in vb.org) but want to show it for the current section not for the whole site, so I need to bypass sectionid to it to modify the query.

Thanks.

tbworld
01-29-2014, 06:20 AM
If you show your code, I will try to assist you in what you are working on, but only one step at a time. So I only need to know what your current programming question is and how I can exactly help you. I am replying to your request for help because you seem adept enough to handle this. If you need someone to do this for you please submit your inquiry to the unpaid/paid request forums.


I tried in the first step to add condition to the widget template (I just use the default php eval widget to understand things), but I didn't figure out what is the condition that I must use in template to say this page is section or article !! Show me the code and your condition for this widget... and carefully explain what you want (just) this conditional to do. :)

Have you successfully cloned a widget by using a new template? This is where you would add the additional template conditionals and variables you require -- of course they would have to be available from the PHP code.

SilverBoy
01-30-2014, 01:01 AM
Hi again and thanks for your caring :)

Actually I can't give you all attempts that I made because they are a lot (I was trying for 3 days without any luck).

But here is the main things that I did
1- I duplicated the template that used by php eval widgets and name it the same name with "test" at the end.
2- I checked the cms_node table and find that the main fields that I must concentrate on are nodeid and issection.
3- I tried to add set of conditions to the template that I made without any luck.
4- I find the way to get the nodeid in the widget and print it in the widget it self and this is very big step but the problem that I faced here is the cache problem, the same nodeid printed in the widget in any section or article, so this make me crazy and if I make the full code to show what I want it will not updated in time !!

here is the code of the widget
$mycontentid = $this->content->getNodeId();
$issecion_get = vB::$db->query_first("SELECT nodeid, issection FROM ".TABLE_PREFIX."cms_node WHERE nodeid = '$mycontentid'");
$output = '';
$output .='<div class = "cms_widget_post_bit"><h4 class="cms_widget_post_header">'.$issecion_get[nodeid].$issecion_get[issection].' </h4></div>';

This widget shows the right nodeid and issecion values but need to remove the cache !!

Then for the template it self to display the widget or hide it I tried to make a plugin using this code
$mycontentid = $this->content->getNodeId();
$issecion_get = vB::$db->query_first("SELECT nodeid, issection FROM ".TABLE_PREFIX."cms_node WHERE nodeid = '$mycontentid'");
$issecion = $issecion_get[issection];
$mynodeid = $issecion_get[nodeid];

but I don't know what is the propriate hook must I use, I tried too many hooks without results !!

and in the widget template I add a condition regarding the valuse of this code
{vb:raw issection}<br>{vb:raw mynodeid}
<vb:if condition="!$issection">
<div class="cms_widget">
<div class="block">
<div class="cms_widget_header widget_header">
<h3><img src="{vb:stylevar imgdir_siteicons}/php.png" alt="" /> {vb:raw title}</h3>
</div>
<div class="cms_widget_content widget_content">
{vb:raw output}
</div>
</div>
</div>
</vb:if>
You note that I even tried to print the values before rendering the widget but without luck too.

So please help me in this tow things:
1- disable the cache for this widget.
2- make the right condition using the plugin or any better way to can control when I want to show the widget and when I want to hide it.

At the end, this is something like case study and I think many users will get benefit of this if we can implement it, and you can imagine how many things you can do with php widgets if you get the power points of them.

Thanks in advance.

--------------- Added 1391049060 at 1391049060 ---------------

Another point, I want a way to control the cache time for php evaluation widgets, because I think in some widgets that are fixed in their section and don't need to refresh with every page load.

tbworld
01-30-2014, 01:57 AM
Thanks for posting in detail, I will get bck to you after I review all this... :)

SilverBoy
01-30-2014, 02:01 AM
Thank you, and your effort very appreciated.

Dead Eddie
01-31-2014, 01:01 AM
So please help me in this tow things:
1- disable the cache for this widget.
2- make the right condition using the plugin or any better way to can control when I want to show the widget and when I want to hide it.


0. You already have access to the content's properties. $this->content->getUsername(); $this->content->isSection(); etc. Look at the public functions of vBCms_Item_Content & vBCms_Item_Content_Article for the methods you can call.

You really don't need to directly query the database again for on-page content.

1. Caching is done in vBCms_Widget_ExecPhp::getPageView() between lines 174 & 175. For testing, I added the NodeId to the hash to make the cache unique to the current node...you can just comment out the reading of the cache on 175 if you want to.

This was my code in line 174 for testing:

$hash = $this->getHash($this->widget->getId() . _ . $this->content->getNodeId());


But, no hooks. You have to modify the code.

2. Again, you'll have to directly modify the widget's code. Around line 173, add the following line:


$view->issection = $this->content->IsSection()?1:0;


Then your conditional should work.

My sample widget code:

$output = 'Username: ' . $this->content->getUsername() . '<br/>';
$output.='Content Type Id: ' . $this->content->getContentTypeId() . '<br />';
$output.= 'Is Section: ' . $this->content->IsSection() . '<br />';


My edited widget class code:


$view = new vBCms_View_Widget($config['template_name']);
$view->class = $this->widget->getClass();
$view->title = $view->widget_title = $this->widget->getTitle();
$view->description = $this->widget->getDescription();
$view->issection = $this->content->IsSection()?1:0;

$hash = $this->getHash($this->widget->getId() . _ . $this->content->getNodeId());
$view->output = vB_Cache::instance()->read($hash, true, true);


Edit: In lieu of editing core code, you could also subclass the PHP widget and implement a new getPageView method. You'd have to install a new version of the widget...but it would make your changes less global.

SilverBoy
01-31-2014, 01:34 AM
Thank you Dead Eddie for your help, I will make my tests and let you know.
But what about the default widgets like "recent articles widget"? could we make them show and hide as we want?

Now I just have one question, Is the cashing now working for every individual node? not for every widget? if this is write, is this will make any bad impact on the system performance (too many cashing, a lot of memory space to save them)?

Thanks :).

--------------- Added 1391138658 at 1391138658 ---------------

Hi Dead Eddie again
I think with this implementation we will may face some problems regarding other PHP eval exec widgets that didn't need to refresh every time, specially when we talking about high traffic board and CMS, so is there a way to keep caching for other widgets, I mean is there a way to deal with caching time like what other default widgets have and put the value to 0 for widgets that display unique value for every page and make it 30 minutes for example for widgets that display the same thing in all pages?

Thanks in advance.

--------------- Added 1391139712 at 1391139712 ---------------

How stupid I'm :)
I just put 0 in the caching time for this widget and it works fine :)

Dead Eddie
01-31-2014, 03:07 AM
Thank you Dead Eddie for your help, I will make my tests and let you know.
But what about the default widgets like "recent articles widget"? could we make them show and hide as we want?


For the default widgets, you'd have to do something like was done above.

Alternatively, you can play games with hidden sections to get the widgets you want on the page.

SilverBoy
01-31-2014, 03:13 AM
Thank you.

I will check what I can do and let you know.

BTW, do you have an effective way to hide the primary content if I want to make one of the sections just a set of widgets no articles or messages tells there is no content here :).

--------------- Added 1391156790 at 1391156790 ---------------

Another Question, and sorry for that, is there a way to add tags to the search criteria for recent article widget?

I looked at the widget file, and I don't know from where I have to start, specially because the tags stored in the cms_contentinfo "keywords field" as a string, and need work to divide it by commas to get the desired tag.

Thanks in advance.

Dead Eddie
01-31-2014, 11:41 AM
BTW, do you have an effective way to hide the primary content if I want to make one of the sections just a set of widgets no articles or messages tells there is no content here :).


I believe there is either an article or a mod on here to to this.


Another Question, and sorry for that, is there a way to add tags to the search criteria for recent article widget?

I looked at the widget file, and I don't know from where I have to start, specially because the tags stored in the cms_contentinfo "keywords field" as a string, and need work to divide it by commas to get the desired tag.

Thanks in advance.

Use the tag/tagcontent tables to pull back the specific tags you want to filter by. :)

SilverBoy
02-01-2014, 01:41 AM
Thank you Dead Eddie for your help.

I have another question now :) (I'm greedy :p )
If I have a PHP direct execution content in one of my sections, and I put PHP code for example to bring me the last article from one section or category or tag for example, is there a way to split result on pages?

Let we say the query returns 50 articles (rows) and I want to display only 10 in the same page, but I want to be still under the same node (php eval), I mean I want to show them just like the sections and categories when they split result to pages.

Is some thing like this possible?

Dead Eddie
02-01-2014, 04:46 AM
Probably?

Not sure if it's the best way, though. Can you be a little more specific (either in here or via PM) about your requirements, and why sections & categories aren't working for you?

SilverBoy
02-01-2014, 05:34 AM
The PM in it's way :D .