solved jrGallery db_update_item listener

SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
I need to check a custom model for a reference to a gallery whenever a jrGallery title is changed, then update that custom item with the new gallery title.

jrGallery uses jrCore_db_update_multiple_items so the best trigger I can see is db_update_item, but that will run for each image in a gallery.

Is there a better listener to use?

Thanks!


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)

updated by @ultrajam: 03/31/18 04:01:00PM
michael
@michael
7 years ago
7,715 posts
That will update every image in that gallery. The 'gallery_title' and the 'gallery_title_url' will be updated.

What I think you're trying to avoid is having your custom modules check function fire too many times. If thats the case, what about setting a flag when the first one runs, then check if that flag has been set before running the second time.

similar to this (screenshot)
flags.jpg
flags.jpg  •  182KB

SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Yes, that's what I'm trying too avoid, but it's many separate db_update_item triggers - from a db_update_item listener I'm not sure how to unset the flag after the last image has been updated. There's no way of telling if it's the last iteration. Also no way of passing the old gallery title (which I would have to get from the db on the first item) between iterations, so I would be getting from and then writing to the database for each image.

Here is a listener with an fdebug to the log:
function ujHotspots_db_update_item_listener($_data, $_user, $_conf, $_args, $event)
{ if (isset($_args['module']) && $_args['module'] == 'jrGallery') { fdebug("ujHotspots_db_update_item_listener",$_data,$_args, $event); } return $_data; }

And here is the output ($_data,$_args, $event) for a 2 image gallery:
Quote: (2017-12-28T20:04:42+00:00.0.68599500 : 0.012)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
|ujHotspots_db_update_item_listener|

(2017-12-28T20:04:42+00:00.0.68599500 : 0.012)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
Array
(
[gallery_title] => Banksy Gallery
[gallery_title_url] => banksy-gallery
[gallery_order] => 2
[_updated] => 1514393591
)

(2017-12-28T20:04:42+00:00.0.68599500 : 0.012)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
Array
(
[_item_id] => 1
[module] => jrGallery
[jrcore_unique_trigger_id] => 9815d1287d1054c2ac358c9460db6667
)

(2017-12-28T20:04:42+00:00.0.68599500 : 0.012)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
|db_update_item|

(2017-12-28T20:04:42+00:00.0.68698600 : 0.013)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
|ujHotspots_db_update_item_listener|

(2017-12-28T20:04:42+00:00.0.68698600 : 0.013)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
Array
(
[gallery_title] => Banksy Gallery
[gallery_title_url] => banksy-gallery
[gallery_order] => 3
[_updated] => 1514393591
)

(2017-12-28T20:04:42+00:00.0.68698600 : 0.013)-(mem: 2MB)-(pid: 6174)-(ip: 92.238.255.12)-(user: SteveX)-(uri: POST /gallery/update_save/__ajax=1)
Array
(
[_item_id] => 2
[module] => jrGallery
[jrcore_unique_trigger_id] => 002d51479d35e48108c6a997fd295506
)



--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
I guess it's a separate listener, but I'm also going to need to update my module's datastore when the last gallery image is deleted. Just mentioning that here to remind myself later ;)


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
michael
@michael
7 years ago
7,715 posts
so you want that to only fire ONCE or once for each separate (something)?

function ujHotspots_db_update_item_listener($_data, $_user, $_conf, $_args, $event)
{ if (isset($_args['module']) && $_args['module'] == 'jrGallery' && !jrCore_get_flag('ujHotspots_already_done')) { fdebug("ujHotspots_db_update_item_listener",$_data,$_args, $event); jrCore_set_flag('ujHotspots_already_done', 1); } return $_data; }
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Ideally once. My hotspots module references the gallery using the gallery title. When the title changes I need to update the reference in the Hotspots datastore. And delete it if that gallery no longer exists.

For your code suggestion there where would I set the flag to off?


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)

updated by @ultrajam: 12/28/17 12:29:08PM
michael
@michael
7 years ago
7,715 posts
So putting flags in the code (as above) should do the trick.

--edit--
A flag will only last for this round of processing, as soon as the next round starts (a page refresh, another url call) the flag will be gone.
updated by @michael: 12/28/17 12:30:00PM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Ah OK, I hadn't realised that!

I will give it a try.

Thanks Michael :)


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Yes, that's going to work perfectly.

Thanks Man, much appreciated :)


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
michael
@michael
7 years ago
7,715 posts
Awesome! :)

Tags