@@ -352,6 +352,17 @@ protected function process(array $record): array
352352 return $ record ;
353353 }
354354
355+ /**
356+ * Add joins to the query
357+ *
358+ * @param object $query
359+ * @return object
360+ */
361+ protected function joins (object $ query ): object
362+ {
363+ return $ query ;
364+ }
365+
355366 /**
356367 * Sanitize the data before inserting or updating
357368 *
@@ -548,15 +559,23 @@ public function fetchAll(array $conditions = [], string $conjunction = 'AND'): a
548559 ->table ($ this ->table )
549560 ->select ('* ' )
550561 ->join ('owner ' , 'users ' , 'username ' )
562+ ->join ('owner.vcard ' , 'vcards ' , 'id ' )
551563 ->index ($ this ->primary )
552564 ->filter ()
553565 ->where ('id ' , 9999 , '<> ' );
554566
555- // Set the Owner
567+ // Verify the Organization
556568 if (array_key_exists ('organization ' ,$ this ->definition )){
557- $ Query ->join ('organization ' , 'organizations ' , 'id ' )->where ('organization ' , $ this ->Auth ->user ()->organization ()->id );
569+ if ($ this ->Auth ->isAuthenticated ()){
570+ $ Query ->join ('organization ' , 'organizations ' , 'id ' )
571+ ->join ('organization.vcard ' , 'vcards ' , 'id ' )
572+ ->where ('organization ' , $ this ->Auth ->user ()->organization ()->id );
573+ }
558574 }
559575
576+ // Apply Joins
577+ $ Query = $ this ->joins ($ Query );
578+
560579 // Check if the conditions are empty
561580 if (!empty ($ conditions )){
562581
@@ -601,7 +620,52 @@ public function fetchAll(array $conditions = [], string $conjunction = 'AND'): a
601620 */
602621 public function count (array $ conditions = [], string $ conjunction = 'AND ' ): int
603622 {
604- return count ($ this ->fetchAll ($ conditions , $ conjunction ));
623+ // Create the Query
624+ $ Query = $ this ->Database ->query ()
625+ ->table ($ this ->table )
626+ ->count ()
627+ ->join ('owner ' , 'users ' , 'username ' )
628+ ->join ('owner.vcard ' , 'vcards ' , 'id ' )
629+ ->index ($ this ->primary )
630+ ->filter ()
631+ ->where ('id ' , 9999 , '<> ' );
632+
633+ // Verify the Organization
634+ if (array_key_exists ('organization ' ,$ this ->definition )){
635+ if ($ this ->Auth ->isAuthenticated ()){
636+ $ Query ->join ('organization ' , 'organizations ' , 'id ' )
637+ ->join ('organization.vcard ' , 'vcards ' , 'id ' )
638+ ->where ('organization ' , $ this ->Auth ->user ()->organization ()->id );
639+ }
640+ }
641+
642+ // Apply Joins
643+ $ Query = $ this ->joins ($ Query );
644+
645+ // Check if the conditions are empty
646+ if (!empty ($ conditions )){
647+
648+ // Add a Filter
649+ $ Query ->filter ();
650+
651+ // Add the Conditions
652+ foreach ($ conditions as $ key => $ condition ){
653+
654+ // Check if the key exists in the definition
655+ if (!array_key_exists ($ condition ['key ' ], $ this ->definition )){
656+
657+ // Remove the key from the data
658+ unset($ conditions [$ key ]);
659+ continue ;
660+ }
661+
662+ // Add the condition to the Query
663+ $ Query ->where ($ condition ["key " ], $ condition ["value " ], $ condition ["operator " ], $ conjunction );
664+ }
665+ }
666+
667+ // Retrieve the count
668+ return $ Query ->execute ();
605669 }
606670
607671 /**
@@ -617,17 +681,25 @@ public function fetch(int $id): array
617681 ->table ($ this ->table )
618682 ->select ('* ' )
619683 ->join ('owner ' , 'users ' , 'username ' )
684+ ->join ('owner.vcard ' , 'vcards ' , 'id ' )
620685 ->filter ()
621686 ->where ('id ' , 9999 , '<> ' )
622687 ->filter ()
623688 ->where ($ this ->primary , $ id )
624689 ->limit (1 );
625690
626- // Set the Owner
691+ // Verify the Organization
627692 if (array_key_exists ('organization ' ,$ this ->definition )){
628- $ Query ->join ('organization ' , 'organizations ' , 'id ' )->where ('organization ' , $ this ->Auth ->user ()->organization ()->id );
693+ if ($ this ->Auth ->isAuthenticated ()){
694+ $ Query ->join ('organization ' , 'organizations ' , 'id ' )
695+ ->join ('organization.vcard ' , 'vcards ' , 'id ' )
696+ ->where ('organization ' , $ this ->Auth ->user ()->organization ()->id );
697+ }
629698 }
630699
700+ // Apply Joins
701+ $ Query = $ this ->joins ($ Query );
702+
631703 // Retrieve the record
632704 $ records = $ Query ->fetch ();
633705
0 commit comments