UserModel::clearSectionNavigationPreference PHP Method

clearSectionNavigationPreference() public method

Also optionally resets the section dashboard landing page, which may be desirable if a user no longer has permission to access pages in that section.
public clearSectionNavigationPreference ( string $url = '', string $userID = '', boolean $resetSectionPreference = true )
$url string The url to search the user navigation preferences for, defaults to the request
$userID string The ID of the user to clear the preferences for, defaults to the sessioned user
$resetSectionPreference boolean Whether to reset the dashboard section landing page
    public function clearSectionNavigationPreference($url = '', $userID = '', $resetSectionPreference = true)
    {
        if (!$userID) {
            $userID = Gdn::session()->UserID;
        }
        if ($url == '') {
            $url = Gdn::request()->url();
        }
        $user = $this->getID($userID);
        $preferences = val('Preferences', $user, []);
        $landingPages = val('DashboardNav.SectionLandingPages', $preferences, []);
        // Run through the user's saved landing page per section and if the url matches the passed url,
        // remove that preference.
        foreach ($landingPages as $section => $landingPage) {
            $url = strtolower(trim($url, '/'));
            $landingPage = strtolower(trim($landingPage, '/'));
            if ($url == $landingPage || stringEndsWith($url, $landingPage)) {
                unset($landingPages[$section]);
            }
        }
        $this->savePreference($userID, 'DashboardNav.SectionLandingPages', $landingPages);
        if ($resetSectionPreference) {
            $this->savePreference($userID, 'DashboardNav.DashboardLandingPage', '');
        }
    }

Usage Example

Example #1
0
 /**
  * Clear user navigation preferences if we can't find the explicit method on the controller.
  *
  * @param Gdn_Controller $sender
  * @param array $args Event arguments. We can expect a 'PathArgs' key here.
  */
 public function gdn_dispatcher_methodNotFound_handler($sender, $args)
 {
     // If PathArgs is empty, the user hit the root, and we assume they want the index.
     // If not, they got redirected to the root because their controller method was not
     // found. We should clear the user prefs in that case.
     if (!empty($args['PathArgs'])) {
         if (Gdn::session()->isValid()) {
             $uri = Gdn::request()->getRequestArguments('server')['REQUEST_URI'];
             try {
                 $userModel = new UserModel();
                 $userModel->clearSectionNavigationPreference($uri);
             } catch (Exception $ex) {
                 // Nothing
             }
         }
     }
 }
All Usage Examples Of UserModel::clearSectionNavigationPreference
UserModel