Announcement

  •  » Engine
  •  » How to determine photo vs album?

#1 2018-01-24 03:40:44

windracer
Member
St. Pete, FL
2014-12-28
448

How to determine photo vs album?

Is there a way to determine, given an id, if it's for a category or image?

I'm working on my Physical Photo Move plugin, adding support for physical directories. As it stands (for images), the plugin uses the $_GET['tab'] as the id to retrieve the information from the images and image_categories tables.

Now that I'm also working with categories, I've found that there is overlap in the ids for images and categories. So if, for example, I'm trying to edit a category with the same id as a photo (as returned by $_GET['tab']) the plugin gets confused.

I've been perusing the source code in GitHub trying to find a function or something that will assist with this but have come up empty. Could someone please point to a way that I can determine, in admin.php of my plugin, if the item being worked on is a 'photo' or 'album'? I'm so close to having this done and then I ran into this bug while testing.

Thanks!

Last edited by windracer (2018-01-24 03:57:07)

Offline

 

#2 2018-01-24 09:29:38

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: How to determine photo vs album?

Hi windracer,

images.id and categories.id are independant. There is a category (=album) with id=1 and an image (=photo) with id=1 too.

Can you give me an example of $_GET['tab'] where you can get confused between categories.id and images.id?

Offline

 

#3 2018-01-24 15:25:06

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Hi Patrick.

My plugin adds a tab to the admin area of a physical photo or album. When I use $_GET['tab'] (in admin.php) on an album, I get '35' but I also have an image with an id that returns '35' to $_GET['tab'] to the plugin. I need to be able to figure out if my plugin tab is being accessed from a photo or album and I can't rely on just the id.

Offline

 

#4 2018-01-24 16:52:46

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: How to determine photo vs album?

Show me more of your code, I'll tell you :-)

Offline

 

#5 2018-01-24 18:16:06

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Appreciate it! :)

https://github.com/jradwan/Piwigo-physi … /admin.php

I wrote what I thought would be a good function to determine the item type based on id. In admin.php:

Code:

// retrieve the item id and type
$item_id = ($_GET['image_id']);
$item_type = ppm_check_item_type($item_id);

ppm_check_item_type is in include/functions.inc.php:

Code:

/ determine if an item is a photo or album
function ppm_check_item_type($item_id)
{
  $item_type = 'none';
  $item_info = get_image_infos($item_id);
  if (!is_null($item_info))
  {
    // this is a photo
    $item_type = 'photo';
  }
  else
  {
    // this is a physical album
    $item_type = 'album';
  }
  return $item_type;
}

My mistake there is assuming that if get_image_infos returns no information, the item is an album. But if an album and a photo have the same id, that code mistakenly identifies the item as a photo and then my later code fails because it expects to be working on an album and not a photo. Hope I'm explaining that enough ...

Last edited by windracer (2018-01-24 18:16:26)

Offline

 

#6 2018-01-27 03:32:56

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Any ideas? I'm going to try and poke around with this more this weekend ...

Offline

 

#7 2018-01-27 14:12:48

flop25
Piwigo Team
2006-07-06
7037

Re: How to determine photo vs album?

I think plg wanted the actual code to know what you want to do then

i don't see on which page you have an url with a $_GET['tab'] existing and being potentially an image or an album...


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#8 2018-01-27 21:34:54

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

I linked to my Github repo above where the full code is.

Here's my test album with an id of 35:

https://photos.radwans.net/index.php?/category/35

(elsewhere in the gallery, there is a photo that has an id of 35 as well) When I go to my plugin's Move tab in the admin area for album 35, I get this:

Code:

Warning:  [mysql error 1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

SELECT *
  FROM categories
  WHERE id = 
; in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php on line 845


Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php:226 Stack trace: #0 /usr/local/piwigo-beta/include/functions_category.inc.php(176): pwg_db_fetch_assoc(false) #1 /usr/local/piwigo-beta/plugins/physical_photo_move/admin.php(112): get_cat_info(NULL) #2 /usr/local/piwigo-beta/admin/plugin.php(68): include_once('/usr/local/piwi...') #3 /usr/local/piwigo-beta/admin.php(311): include('/usr/local/piwi...') #4 {main} thrown in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php on line 226

So admin.php gets an image_id of 35 and passes it to my ppm_check_item_type function. ppm_check_item_type, as I have it written now (see post above), makes a call to get_image_infos with the id and if it gets a null return, knows this is an album. But since id 35 exists as a photo, it returns an item_type of 'photo' instead. And that causes code issues later on.

So I'm trying to figure out how better to write the ppm_check_item_type function to better determine if the id I am working with is a category or image.

Offline

 

#9 2018-01-27 22:20:00

flop25
Piwigo Team
2006-07-06
7037

Re: How to determine photo vs album?

windracer wrote:

I linked to my Github repo above where the full code is.

oops sorry

so you're adding a link depending to the source  https://github.com/jradwan/Piwigo-physi … nc.php#L37 so why you're not adding a second parameter in order to add the photo/album info?


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#10 2018-01-28 19:10:12

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Oh, so something like this?

Code:

    $sheets['ppm'] = array(
      'caption' => l10n('MOVE_BUTTON'),
      'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$_GET['image_id'],
      'item' => 'photo'
      );

And then use that 'item' array element in my check function?

Offline

 

#11 2018-01-28 20:01:06

flop25
Piwigo Team
2006-07-06
7037

Re: How to determine photo vs album?

1- don't use a GET/POST or anything that the suer can manipulate, directly in an url, for safety reasons ( check XSS)
Use our function check_input_parameter()  [Github] Piwigo file admin/photo.php@L35
2-  why not just : 'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$image_id_checked'.'&ppm_type=image'  There you're sending a second $_GET, the $_GET['ppm_type']


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#12 2018-01-29 03:10:29

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Thanks for the suggestions, I'll give 'em a shot ...

Offline

 

#13 2018-01-29 04:03:26

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

That worked, flop25, thanks for the suggestion.

In main.inc.php I set

Code:

'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$_GET['image_id'].'&ppm_type='.$id,

Where $id will be 'photo' or 'album'. Then in admin.php I added the parameter check:

Code:

check_input_parameter('ppm_type', $_GET, false, '/^(photo|album)$/');

And used the new parameter in my if statements directly, eliminating the need for my separate ppm_check_item_type function:

Code:

if ($_GET['ppm_type'] == 'photo')
{
...
}
elseif ($_GET['ppm_type'] == 'album')
{
...
}

Seems to work! Still need to do some testing, but I think that'll do it. Thanks again.

Offline

 

#14 2018-01-29 08:37:08

flop25
Piwigo Team
2006-07-06
7037

Re: How to determine photo vs album?

Great news! And thanks for your  plug-in! It will be quite important for users sticking with physical categories. You're a quick learner doing a very good job. I learnt like you, but creating a  plug-in was easier at that time


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#15 2018-01-30 03:18:26

windracer
Member
St. Pete, FL
2014-12-28
448

Re: How to determine photo vs album?

Thanks for the kind words and your continued assistance!

I've released v2.00 of the plugin now.

Offline

 
  •  » Engine
  •  » How to determine photo vs album?

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact