@@ -658,6 +658,167 @@ public function search_edit($args = [])
658658 return true ;
659659 }
660660
661+ /**
662+ * Share a saved search with users and/or groups.
663+ *
664+ * @param id Search ID to share.
665+ * @param user_id Owner of the search (for validation).
666+ * @param user_ids Array of user IDs to share with.
667+ * @param group_ids Array of group IDs to share with.
668+ *
669+ * @return success
670+ */
671+ public function search_share ($ args = [])
672+ {
673+ OBFHelpers::require_args ($ args , ['id ' , 'user_id ' ]);
674+ OBFHelpers::default_args ($ args , ['user_ids ' => [], 'group_ids ' => []]);
675+
676+ // verify this search belongs to the user and is saved
677+ $ this ->db ->where ('id ' , $ args ['id ' ]);
678+ $ this ->db ->where ('user_id ' , $ args ['user_id ' ]);
679+ $ this ->db ->where ('type ' , 'saved ' );
680+ $ search = $ this ->db ->get_one ('media_searches ' );
681+
682+ if (!$ search ) {
683+ return false ;
684+ }
685+
686+ // remove existing shares for this search
687+ $ this ->db ->where ('search_id ' , $ args ['id ' ]);
688+ $ this ->db ->delete ('media_searches_shared ' );
689+
690+ // share with users
691+ if (!empty ($ args ['user_ids ' ])) {
692+ foreach ($ args ['user_ids ' ] as $ share_user_id ) {
693+ $ share_user_id = (int ) $ share_user_id ;
694+
695+ // don't share with self
696+ if ($ share_user_id == $ args ['user_id ' ]) {
697+ continue ;
698+ }
699+ $ this ->db ->insert ('media_searches_shared ' , [
700+ 'search_id ' => $ args ['id ' ],
701+ 'shared_by ' => $ args ['user_id ' ],
702+ 'shared_with_user_id ' => $ share_user_id ,
703+ ]);
704+ }
705+ }
706+
707+ // share with groups
708+ if (!empty ($ args ['group_ids ' ])) {
709+ foreach ($ args ['group_ids ' ] as $ group_id ) {
710+ $ this ->db ->insert ('media_searches_shared ' , [
711+ 'search_id ' => $ args ['id ' ],
712+ 'shared_by ' => $ args ['user_id ' ],
713+ 'shared_with_group_id ' => (int ) $ group_id ,
714+ ]);
715+ }
716+ }
717+
718+ return true ;
719+ }
720+
721+ /**
722+ * Remove all sharing for a saved search.
723+ *
724+ * @param id Search ID to unshare.
725+ * @param user_id Owner of the search (for validation).
726+ *
727+ * @return success
728+ */
729+ public function search_unshare ($ args = [])
730+ {
731+ OBFHelpers::require_args ($ args , ['id ' , 'user_id ' ]);
732+
733+ // verify this search belongs to the user
734+ $ this ->db ->where ('id ' , $ args ['id ' ]);
735+ $ this ->db ->where ('user_id ' , $ args ['user_id ' ]);
736+ $ search = $ this ->db ->get_one ('media_searches ' );
737+
738+ if (!$ search ) {
739+ return false ;
740+ }
741+
742+ $ this ->db ->where ('search_id ' , $ args ['id ' ]);
743+ return $ this ->db ->delete ('media_searches_shared ' );
744+ }
745+
746+ /**
747+ * Get searches shared with the current user (directly or via groups).
748+ *
749+ * @return searches
750+ */
751+ public function search_get_shared ()
752+ {
753+ if (!$ this ->user ->param ('id ' )) {
754+ return [];
755+ }
756+
757+ $ user_id = $ this ->db ->escape ($ this ->user ->param ('id ' ));
758+
759+ $ this ->db ->query ("
760+ SELECT DISTINCT ms.id, ms.query, ms.description, ms.`default`,
761+ mss.shared_by,
762+ u.display_name AS shared_by_name
763+ FROM media_searches ms
764+ JOIN media_searches_shared mss ON mss.search_id = ms.id
765+ LEFT JOIN users_to_groups utg ON utg.group_id = mss.shared_with_group_id
766+ LEFT JOIN users u ON u.id = mss.shared_by
767+ WHERE mss.shared_with_user_id = \"{$ user_id }\"
768+ OR utg.user_id = \"{$ user_id }\"
769+ " );
770+
771+ $ searches = $ this ->db ->assoc_list ();
772+ if (!is_array ($ searches )) {
773+ return [];
774+ }
775+
776+ foreach ($ searches as $ index => $ search ) {
777+ $ searches [$ index ]['query ' ] = unserialize ($ search ['query ' ]);
778+ }
779+
780+ return $ searches ;
781+ }
782+
783+ /**
784+ * Get the recipients (users and groups) a search is shared with.
785+ *
786+ * @param id Search ID.
787+ * @param user_id Owner of the search (for validation).
788+ *
789+ * @return recipients Array with 'user_ids' and 'group_ids'.
790+ */
791+ public function search_get_shared_recipients ($ args = [])
792+ {
793+ OBFHelpers::require_args ($ args , ['id ' , 'user_id ' ]);
794+
795+ // verify ownership
796+ $ this ->db ->where ('id ' , $ args ['id ' ]);
797+ $ this ->db ->where ('user_id ' , $ args ['user_id ' ]);
798+ $ search = $ this ->db ->get_one ('media_searches ' );
799+
800+ if (!$ search ) {
801+ return false ;
802+ }
803+
804+ $ this ->db ->where ('search_id ' , $ args ['id ' ]);
805+ $ shares = $ this ->db ->get ('media_searches_shared ' );
806+
807+ $ user_ids = [];
808+ $ group_ids = [];
809+
810+ foreach ($ shares as $ share ) {
811+ if (!empty ($ share ['shared_with_user_id ' ])) {
812+ $ user_ids [] = (int ) $ share ['shared_with_user_id ' ];
813+ }
814+ if (!empty ($ share ['shared_with_group_id ' ])) {
815+ $ group_ids [] = (int ) $ share ['shared_with_group_id ' ];
816+ }
817+ }
818+
819+ return ['user_ids ' => $ user_ids , 'group_ids ' => $ group_ids ];
820+ }
821+
661822 /**
662823 * Get captions URL for media item
663824 *
0 commit comments