| | |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use JVBase\rest\PermissionHandler; |
| | | use JVBase\base\Site; |
| | | use JVBase\managers\CustomTable; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | |
| | | parent::__construct(); |
| | | $this->cache = Cache::for($this->taxonomy, 'term', DAY_IN_SECONDS); |
| | | $this->setupTables(); |
| | | $this->setupCacheConnections(); |
| | | } |
| | | |
| | | add_action('init', [$this, 'registerContentTermsExecutors'], 5); |
| | |
| | | } |
| | | } |
| | | |
| | | protected function setupCacheConnections(): void |
| | | { |
| | | $this->cache |
| | | ->connect('taxonomy') |
| | | ->connect('user'); |
| | | } |
| | | |
| | | public function registerRoutes(): void |
| | | { |
| | | if (!$this->registrar->hasFeature('is_content')) { |
| | |
| | | $status = $request->get_param('status'); |
| | | $page = (int)$request->get_param('page'); |
| | | |
| | | $cacheKey = $this->cache->generateKey(compact('termID', 'status', 'page')); |
| | | |
| | | return $this->cache->remember($cacheKey, function() use ($termID, $status, $page) { |
| | | return $this->cache->remember(['id'=> $termID, 'status' => $status, 'page' => $page], function() use ($termID, $status, $page) { |
| | | $perPage = 20; |
| | | $offset = ($page - 1) * $perPage; |
| | | |
| | |
| | | if ($status === 'active') { |
| | | $query = $query->where(['end_date' => null]); |
| | | } elseif ($status === 'inactive') { |
| | | $query = $query->whereNotNull('end_date'); |
| | | $query = $query->where(['end_date'=> ['NOT NULL', null]]); |
| | | } |
| | | |
| | | $total = $query->countResults(); |
| | | |
| | | $members = $query |
| | | ->orderBy('is_primary', 'DESC') |
| | | ->orderBy('is_primary') |
| | | ->orderBy('created_at', 'ASC') |
| | | ->limit($perPage, $offset) |
| | | ->getResults(ARRAY_A); |
| | |
| | | $status = $request->get_param('status'); |
| | | $page = (int)$request->get_param('page'); |
| | | |
| | | $cacheKey = $this->cache->generateKey(compact('termID', 'status', 'page')); |
| | | |
| | | return $this->cache->remember($cacheKey, function() use ($termID, $status, $page) { |
| | | return $this->cache->remember(['id'=>$termID, 'status' => $status,'page' => $page], function() use ($termID, $status, $page) { |
| | | $perPage = 20; |
| | | $offset = ($page - 1) * $perPage; |
| | | |
| | |
| | | $total = $query->countResults(); |
| | | |
| | | $requests = $query |
| | | ->orderBy('created_date', 'DESC') |
| | | ->orderBy('created_date') |
| | | ->limit($perPage, $offset) |
| | | ->getResults(ARRAY_A); |
| | | |
| | |
| | | |
| | | return $this->error('Failed to create request'); |
| | | } catch (Exception $e) { |
| | | $this->logError('createRequest', [ |
| | | 'error' => $e->getMessage(), |
| | | $this->logError($e->getMessage(),'createRequest', [ |
| | | 'user_id' => $userID, |
| | | 'term_id' => $termID |
| | | ]); |
| | |
| | | } |
| | | }); |
| | | |
| | | // Notify user |
| | | NotificationManager::getInstance()->addNotification( |
| | | $requestData['user_id'], |
| | | 'request_' . $status, |
| | | [ |
| | | 'term_id' => $requestData['term_id'], |
| | | 'taxonomy' => $this->taxonomy, |
| | | 'notes' => $notes |
| | | ] |
| | | ); |
| | | // Notify user TODO |
| | | // NotificationManager::getInstance()->notify( |
| | | // $requestData['user_id'], |
| | | // 'request_' . $status, |
| | | // [ |
| | | // 'term_id' => $requestData['term_id'], |
| | | // 'taxonomy' => $this->taxonomy, |
| | | // 'notes' => $notes |
| | | // ] |
| | | // ); |
| | | |
| | | $this->cache->flush(); |
| | | |
| | | return $this->success(['message' => 'Request ' . $status]); |
| | | |
| | | } catch (Exception $e) { |
| | | $this->logError('processRequest', [ |
| | | 'error' => $e->getMessage(), |
| | | $this->logError($e->getMessage(), 'processRequest', [ |
| | | 'request_id' => $requestID, |
| | | 'action' => $action |
| | | ]); |
| | |
| | | return $this->error('Failed to update permissions'); |
| | | |
| | | } catch (Exception $e) { |
| | | $this->logError('updatePermissions', [ |
| | | 'error' => $e->getMessage(), |
| | | $this->logError($e->getMessage(),'updatePermissions', [ |
| | | 'term_id' => $termID, |
| | | 'target_user' => $targetUserID, |
| | | 'role' => $role |
| | |
| | | |
| | | $term = get_term($termID, jvbCheckBase($this->taxonomy)); |
| | | |
| | | foreach ($managers as $managerID) { |
| | | NotificationManager::getInstance()->addNotification( |
| | | $managerID, |
| | | $notificationType, |
| | | [ |
| | | 'user_id' => $actingUserID, |
| | | 'term_id' => $termID, |
| | | 'term_name' => $term->name ?? 'Unknown', |
| | | 'taxonomy' => $this->taxonomy |
| | | ] |
| | | ); |
| | | } |
| | | //TODO |
| | | // foreach ($managers as $managerID) { |
| | | // NotificationManager::getInstance()->addNotification( |
| | | // $managerID, |
| | | // $notificationType, |
| | | // [ |
| | | // 'user_id' => $actingUserID, |
| | | // 'term_id' => $termID, |
| | | // 'term_name' => $term->name ?? 'Unknown', |
| | | // 'taxonomy' => $this->taxonomy |
| | | // ] |
| | | // ); |
| | | // } |
| | | } |
| | | } |