[TikiWiki-commits] [Git][tikiwiki/tiki][28.x] [BP][ENH] Trackers: Add File Gallery per Tracker Item Option

"Adrien Mbuya Maloba \(@adrienmaloba\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <68a71732695d6_2cd6dbc316e@gitlab-sidekiq-low-urgency-cpu-bound-v2-5546c8c99c-f789q.mail>

Adrien Mbuya Maloba pushed to branch 28.x at Tiki Wiki CMS Groupware / Tiki


Commits:
46e49bb1 by Adrien Mbuya Maloba at 2025-08-21T15:48:31+03:00
[BP][ENH] Trackers: Add File Gallery per Tracker Item Option
---
* [ENH] Trackers: Add File Gallery per Tracker Item Option
---
* [ENH] Trackers: Add File Gallery per Tracker Item Option

See merge request tikiwiki/tiki!6832

See merge request tikiwiki/tiki!8143

- - - - -


4 changed files:

- lib/core/Services/File/Utilities.php
- lib/core/Tracker/Field/Files.php
- lib/filegals/filegallib.php
- templates/trackerinput/files.tpl


Changes:

=====================================
lib/core/Services/File/Utilities.php
=====================================
@@ -67,19 +67,23 @@ class Services_File_Utilities
                     $galleryId = $filegallib->duplicate_file_gallery($parentGalleryId, $dir, '', $parentGalleryId);
                 }
                 // also copy any direct permissions of the parent gallery
-                $objectFactory = Perms_Reflection_Factory::getDefaultFactory();
-                $parentObject = $objectFactory->get('file gallery', $parentGalleryId);
-                $perms = $parentObject->getDirectPermissions();
-                if ($perms->getPermissionArray()) {
-                    $object = $objectFactory->get('file gallery', $galleryId);
-                    $permissionApplier = new Perms_Applier();
-                    $permissionApplier->addObject($object);
-                    $permissionApplier->apply($perms);
-                }
+                $this->copyParentPermissions($parentGalleryId, $galleryId);
             }
             $parentGalleryId = $galleryId;
         }
-
         return $this->checkTargetGallery($parentGalleryId);
     }
+
+    public function copyParentPermissions(int $parentGalleryId, int $galleryId): void
+    {
+        $objectFactory = Perms_Reflection_Factory::getDefaultFactory();
+        $parentObject = $objectFactory->get('file gallery', $parentGalleryId);
+        $perms = $parentObject->getDirectPermissions();
+        if ($perms->getPermissionArray()) {
+            $object = $objectFactory->get('file gallery', $galleryId);
+            $permissionApplier = new Perms_Applier();
+            $permissionApplier->addObject($object);
+            $permissionApplier->apply($perms);
+        }
+    }
 }


=====================================
lib/core/Tracker/Field/Files.php
=====================================
@@ -163,6 +163,21 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
                         'filter' => 'int',
                         'profile_reference' => 'file_gallery',
                     ],
+                    'fileGalleryPerTrackerItem' => [
+                        'name' => tr('File Gallery per Tracker Item'),
+                        'description' => tr('Create a sub-gallery for each tracker item.'),
+                        'filter' => 'alpha',
+                        'default' => 'n',
+                        'options' => [
+                            'n' => tr('No'),
+                            'y' => tr('Yes'),
+                        ],
+                    ],
+                    'fileGalleryPerTrackerItemName' => [
+                        'name' => tr('File Gallery per Tracker Item'),
+                        'description' => tr('Pattern of the name of sub-gallery to be created. Default value is "Field %fieldId% item %itemId%" where %fieldId% is the fieldId and %itemId% is the itemId. The gallery names will then be, for example "Events Photos item: 42".'),
+                        'filter' => 'text',
+                    ],
                     'indexGeometry' => [
                         'name' => tr('Index As Map Layer'),
                         'description' => tr('Index the files in a specific format for use in map searchlayers to display trails and features.'),
@@ -227,7 +242,7 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
 
     public function getFieldData(array $requestData = []): array
     {
-        global $prefs;
+        global $prefs, $user;
         $filegallib = TikiLib::lib('filegal');
 
         $galleryId = (int) $this->getOption('galleryId');
@@ -298,11 +313,18 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
             Feedback::error(tr('Files field: Gallery #%0 not found', $galleryId));
             return [];
         }
+
+        //If fileGalleryPerTrackerItem is set to yes
+        if ($this->trackerField->getOption('fileGalleryPerTrackerItem')) {
+            //Check permissions related to a specific gallery object
+            $perms = Perms::get('file gallery', $galleryId);
+            $canCreateGallery = $perms->create_file_galleries;
+        }
+
         if ($prefs['feature_use_fgal_for_user_files'] !== 'y' || $galinfo['type'] !== 'user') {
             $perms = Perms::get('file gallery', $galleryId);
             $canUpload = $perms->upload_files;
         } else {
-            global $user;
             $perms = TikiLib::lib('tiki')->get_local_perms($user, $galleryId, 'file gallery', $galinfo, false);     //get_perm_object($galleryId, 'file gallery', $galinfo);
             $canUpload = $perms['tiki_p_upload_files'] === 'y';
         }
@@ -330,6 +352,7 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
         return [
             'galleryId' => $galleryId,
             'canUpload' => $canUpload,
+            'canCreateGallery' => $canCreateGallery,
             'limit' => $count,
             'excessBehavior' => $excessBehavior,
             'files' => $fileInfo,
@@ -343,6 +366,7 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
             'gallerySearch' => $gallery_list,
             'requireTitle' => $this->getOption('requireTitle'),
             'directoryPattern' => $directoryPattern,
+            'fileGalleryPerTrackerItem' => $this->trackerField->getOption('fileGalleryPerTrackerItem'),
         ];
     }
 
@@ -625,6 +649,10 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
 
     public function handleSave($value, $oldValue)
     {
+        global $prefs, $user;
+        $filegallib = TikiLib::lib('filegal');
+        $relationlib = TikiLib::lib('relation');
+        $utilities = new Services_File_Utilities();
         $excessBehavior = $this->getOption('excessBehavior');
         $count = (int) $this->getOption('count');
 
@@ -643,6 +671,54 @@ class Tracker_Field_Files extends \Tracker\Field\AbstractItemField implements \T
 
         $new = array_diff($fileIds, explode(',', (string) $oldValue));
         $remove = array_diff(explode(',', (string) $oldValue), $fileIds);
+        //If there new uploaded files
+        if (! empty($new) && $this->trackerField->getOption('fileGalleryPerTrackerItem') === 'y') {
+            //Create new gallery and move all uploaded files
+            $fieldId = $this->getConfiguration('fieldId');
+            $itemId = $this->getItemId();
+            $galleryName = 'Field ' . $fieldId . ' Item ' . $itemId; //By default the pattern of gallery to be created: Field [FIELD_ID] Item [ITEM_ID]
+            if (! empty($this->trackerField->getOption('fileGalleryPerTrackerItemName'))) {
+                $galleryName = $this->trackerField->getOption('fileGalleryPerTrackerItemName');
+                $galleryName = preg_replace(['/%fieldId%/', '/%itemId%/'], [$fieldId, $itemId], $galleryName, 1);
+            }
+
+            $parentId = (int) $this->trackerField->getOption('galleryId');
+            //If there is no gallery choosen we'll take 'File Galleries' as gallery where the new one will be added
+            if ($parentId == 0) {
+                $parentId = $prefs['fgal_root_id'];
+            }
+
+            // Get relations between the current item and the sub-galleries
+            $relations = $relationlib->get_relations_from('trackeritem', $itemId, 'tiki.filegallery.attach');
+            foreach ($relations as $relation) {
+                if ($relation['fieldId'] === $fieldId) {
+                    $galleryId = (int) $relation['itemId'];
+                    break;
+                }
+            }
+
+            // Create a sub-gallery if none exists for the current Field
+            if (empty($galleryId)) {
+                if ($parentId == $prefs['fgal_root_id']) {
+                    $galleryId = $filegallib->replace_file_gallery(['name' => $galleryName, 'user' => $user]);
+                } else {
+                    $galleryId = $filegallib->duplicate_file_gallery($parentId, $galleryName, '', $parentId);
+                }
+                // Copy any direct permissions of the parent gallery
+                $utilities->copyParentPermissions($parentId, $galleryId);
+                // Add relation between the item and the new sub-gallery
+                $relationlib->add_relation('tiki.filegallery.attach', 'trackeritem', $itemId, 'file gallery', $galleryId, false, $fieldId);
+            }
+
+            $gal_info = $filegallib->get_file_gallery($galleryId);
+
+            if (! empty($gal_info)) {
+                //Move all files to the dedicated gallery
+                $filegallib->moveFilesToNewGallery($fileIds, $gal_info['galleryId']);
+            } else {
+                throw new Exception(tr('Gallery ID %0 not found', $galleryId));
+            }
+        }
 
         $itemId = $this->getItemId();
 


=====================================
lib/filegals/filegallib.php
=====================================
@@ -899,6 +899,18 @@ class FileGalLib extends TikiLib
         return $id;
     }
 
+    public function moveFilesToNewGallery($fileIds, $newGalleryId)
+    {
+        foreach ($fileIds as $fileId) {
+            if (! empty($newGalleryId)) {
+                $result = $this->set_file_gallery($fileId, $newGalleryId);
+                if (! $result && $result->numRows() !== 1) {
+                    throw new Exception(tr('An error occurred while moving file to the new gallery.'));
+                }
+            }
+        }
+    }
+
     public function change_file_handler($mime_type, $cmd)
     {
         $handlers = $this->table('tiki_file_handlers');


=====================================
templates/trackerinput/files.tpl
=====================================
@@ -4,7 +4,7 @@
     {assign var=actual_limit value=$field.limit|default:100}
 {/if}
 <div id="display_f{$field.fieldId|escape}" class="files-field display_f{$field.fieldId|escape} uninitialized {if !empty($data.replaceFile)}replace{/if}" data-galleryid="{$field.galleryId|escape}" data-firstfile="{$field.firstfile|escape}" data-filter="{$field.filter|escape}" data-limit="{$field.limit|escape}" data-item-id="{$item.itemId|escape}" data-field-id="{$field.fieldId|escape}" data-namefilter="{$field.namefilter|escape}" data-namefilter-error="{$field.namefilterError|escape}">
-    {if !empty($field.canUpload)}
+    {if (! empty($field.canUpload) && $field.fileGalleryPerTrackerItem !== 'y') || (! empty($field.canUpload) && $field.fileGalleryPerTrackerItem === 'y' && $field.canCreateGallery)}
         {if !empty($field.limit)}
             {remarksbox _type=info title="{tr}Attached files limitation{/tr}"}
                 {tr _0=$field.limit}The amount of files that can be attached to this item is limited to <strong>%0</strong>.{/tr}
@@ -100,8 +100,13 @@
             </fieldset>
         {/if}
     {else}
+        {if empty($field.canUpload)}
             {remarksbox type="error" close="n" title="{tr}You do not have permission to upload files to this gallery.{/tr}" }
             {/remarksbox}
+        {else}
+            {remarksbox type="error" close="n" title="{tr}You do not have permission to create galleries in this gallery.{/tr}" }
+            {/remarksbox}
+        {/if}
     {/if}
 </div>
 



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/46e49bb19a9e66337242fbac4466b3b7441d00e3

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/46e49bb19a9e66337242fbac4466b3b7441d00e3
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.