Random Image : Auto Select Sub-Categories

Phoca Gallery modules - modules for Phoca Gallery extension
totorman
Phoca Newbie
Phoca Newbie
Posts: 4
Joined: 14 May 2010, 21:59

Random Image : Auto Select Sub-Categories

Post by totorman »

Hi,
first of all, thx for your modules and plugins !!! They are great.

I'll begin this post by a thing i found into your components : when installing (at least the add image button plugin) some tools from you, i encountered an error which was solved like this :

Error :

Code: Select all

Fatal error: Class 'JSite' not found in xxxxxx/plugins/content/phocagallery.php
occurs on line 51 or 52 depending version of phoca

Resolution :
replace

Code: Select all

$menu = &JSite::getMenu();
with :

Code: Select all

global $mainframe; 
if ($mainframe->isSite()) $menu = &JSite::getMenu();
which might be error coming because of a confusion between frontend and backend. In fact the original line do work on some sites and on some it does not ...

Back to the original subject of this post ;)

I wanted to have a random image module, in which i haven't to go to configure each time i create a new phoca image sub-category.
So i've managed to modify your code to add a sub-category auto select feature.

You have just to select 1 or more categories into the list, and to enable a radio button => auto select sub-categories.
The random image will be taken from all sub-categories of the selected categories. So, there is no need to reconfigure the module if adding a sub-category into the images categories.

Here is the code, if someone want to try, or needs this thing.

in the "/modules/mod_photogallery_image/mod_phocagallery_image.xml" file add just after the list definition (line 61)

Code: Select all

<param name="get_subcategories" type="radio" default="1" label="Get Sub-categories images" description="Get Sub-categories images">
				<option value="0">No</option>
				<option value="1">Yes</option>
		</param>
This adds a radio button to enable/disable auto sub-categories selection

in the "/modules/mod_photogallery_image/mod_phocagallery_image.php" file
we need first to get the new parameter :

Code: Select all

$get_subcategories          = $params->get( 'get_subcategories',1);
put in line 94, at the end of the parameters lines.

and to finish, if the radio button is selected, we need to get all the childs of the categories which are selected in the options of the module.
Add these lines :

Code: Select all

// if we must look into subcategories
if ($get_subcategories == 1) {
   $found_subs = 1 ; // init
   $enplus = $implodeAllowedCategoriesArray ; // init
   // while we do have categories to explore
   while ($found_subs == 1) {
	   // select each selectionned categories
	   $categs = explode(',',$enplus) ;
	   
	   $found_subs = 0 ;
	   $nouv= "" ; // we have found nothing for now
	   foreach ($categs as $categ) {
		   $query = 'SELECT cc.id as idcat FROM #__phocagallery_categories AS cc'
		   . ' where cc.parent_id = '.$categ ;
		   $db->setQuery($query);
		   $res = $db->loadObjectList(); //search for childs of the category
		   if ($res) { foreach ($res as $r) { $nouv .= ','.$r->idcat; } }
	   }
	   // remove possible duplicated categories
	   $implodeAllowedCategoriesArray=implode(',',array_unique(explode(",",$implodeAllowedCategoriesArray.$nouv)));
	   //if we have new childs then go find childs of them
	   if ( $nouv != "" ) {
	   	  $enplus = substr($nouv,1,strlen($nouv)-1) ; // remove the trailing ','
		  $found_subs = 1 ; // go again into while loop
		  }
   }
}
juste before the image selection in the SQL database (line 521 for me), juste before the

Code: Select all

image='';
line

The code is'nt very "SQL aware" but seems to work, and does what i wanted.

If someone is interested by this, i'll be happy to have shared that.

I also must say that into the french translation, there are javascript errors coming because of some ['] characters that are trying to be printed.
example : the PHOCAGALLERY_WARNING_AUTHORIZE_ALL parameter generates a javascript error in FR when clicking the User button in administration (ps i use the beta version 2.7.1)

Thx again for your work. (yes i know ... for a first post, its not a little one :lol: )
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Random Image : Auto Select Sub-Categories

Post by Jan »

Hi, thank you for the guide, I will take a look at it for the next version.


Some questions:

- which exactly javascript error it produces? How to prevent from it?

- did you try the module with subcategories on more categories. The querey is in while loop. It is ok with time?


Thank you very much again.

Jan
If you find Phoca extensions useful, please support the project
totorman
Phoca Newbie
Phoca Newbie
Posts: 4
Joined: 14 May 2010, 21:59

Re: Random Image : Auto Select Sub-Categories

Post by totorman »

The Javascript error is because the script is generated with the traduction inside. And because in France we use the ' character into words, when you are for sample writing 'permet l'établissement d'une connexion'
the javascript code become incorrect because it didnt know what to do with the words after the second '

The only way to solve is to know which traduction line is going to go to javascript, and to put a \ before the ' like this :

PHOCAGALLERY_WARNING_AUTHORIZE_ALL=Confirmez-vous l\'autorisation de toutes les catégories et images de tous les utilisateurs

or maybe you have to search for the ' character and put a \ before each found ' before generating code.

and here is the generated code with the javascript error :

the error :

Code: Select all

Détails de l’erreur de la page Web

Agent utilisateur : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Horodateur : Mon, 17 May 2010 22:06:11 UTC


Message : ')' attendu
Ligne : 365
Caractère : 30
Code : 0
URI : http://xxx.yyy.com/administrator/index.php?option=com_phocagallery&view=phocagalleryusers
the javascript :

Code: Select all

<td class="button" id="toolbar-custom">
<a href="#" onclick="javascript:if(confirm('Confirmez-vous l'autorisation de toutes les catégories et images de tous les utilisateurs')){submitbutton('approveall');}" class="toolbar"><span class="icon-32-authorizeall" title="Valider Tout" type="Custom"></span>Valider Tout</a></td>

For the second point, i've not many many images to see if it have a very bad impact on the sql server.
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Random Image : Auto Select Sub-Categories

Post by Jan »

Hi, I changed the module and added addslashes code to the javascript.

Jan
If you find Phoca extensions useful, please support the project
totorman
Phoca Newbie
Phoca Newbie
Posts: 4
Joined: 14 May 2010, 21:59

Re: Random Image : Auto Select Sub-Categories

Post by totorman »

Hi,

did you do some tests on the "children" auto-selection render speed ?
ogt
Phoca Newbie
Phoca Newbie
Posts: 3
Joined: 14 Mar 2012, 09:06

Re: Random Image : Auto Select Sub-Categories

Post by ogt »

totorman wrote:
Error :

Code: Select all

Fatal error: Class 'JSite' not found in xxxxxx/plugins/content/phocagallery.php
occurs on line 51 or 52 depending version of phoca

Resolution :
replace

Code: Select all

$menu = &JSite::getMenu();
with :

Code: Select all

global $mainframe; 
if ($mainframe->isSite()) $menu = &JSite::getMenu();
which might be error coming because of a confusion between frontend and backend. In fact the original line do work on some sites and on some it does not ...
Hi all,

I also have this problem and I tried the fix above without success. Now I get an error at line 71 instead of 51. Any ideas how to fix this please? I Use Joomla 2.52 and it's not a broken installation :) Joomla works just fine with everything else. Phocamaps is version 2.0.4. Any help is most welcomed!

Greets,
Thomas
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Random Image : Auto Select Sub-Categories

Post by Jan »

Hi, which error you exactly get? Do you run latest version of component and plugin?
If you find Phoca extensions useful, please support the project
ogt
Phoca Newbie
Phoca Newbie
Posts: 3
Joined: 14 Mar 2012, 09:06

Re: Random Image : Auto Select Sub-Categories

Post by ogt »

I use Joomla 2.53, phoca plugin 2.05 and phoca 2.0.4 (all latest version). This is the error message when I try to save a article with phoca plugin code:

"Fatal error: Class 'JSite' not found in /components/com_phocamaps/helpers/route.php on line 71"
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48386
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Random Image : Auto Select Sub-Categories

Post by Jan »

Hi, I will take a look at it, for now testing without any such problem :idea:
If you find Phoca extensions useful, please support the project
vickipayne
Phoca Member
Phoca Member
Posts: 14
Joined: 10 Nov 2010, 18:30

Re: Random Image : Auto Select Sub-Categories

Post by vickipayne »

Thank you for the sub-category selection code. I am using it on a few sites and it is working very well.

The sites I am using it on do not have many Phoca images or categories yet, but they will. So, I would like to know if anyone has found any server time issues with the "while" loop (as Jan asked earlier in this thread).

Thanks,
Vicki
Post Reply