Ferreteria/v2/class/fcMenuLink

From Woozle Writes Code
< Ferreteria‎ | v2‎ | class
Jump to navigation Jump to search

Template:Page/code/class

/*::::
  PURPOSE: base class for menu-links which are usage-aware
    Base class does not define how usage is determined.
    It is also still PASSIVE - status checks happen when rendering.
  ADDS: usage-awareness
  HISTORY:
    2017-02-08 Can't see any reason for ActionClass methods to be defined here; moving them to Dropin Link class
      (in dropin.php).
    2017-03-26 Figuring authorization at RunCalculations time now, instead of at Render time, because in the latter case
      we don't have the list of missing permits at render time (when we might want to display them).
*/
abstract class fcMenuLink extends fcNavLink {

    // ++ SETUP ++ //

    public function __construct(string $sKeyValue,$sText=NULL,$sPopup=NULL) {
	$this->SetKeyValue($sKeyValue);
	$this->SetLinkText($sText);
	$this->SetPopup($sPopup);
	$this->SetupDefaults();		// ALWAYS CALL THIS from constructor
    }
    protected function SetupDefaults() {
	$this->SetRequiredPrivilege(NULL);	// this class is security-aware, but assumes permission by default
    }

      //++fields++//
    
    private $sPageTitle;
    public function SetPageTitle($s) {
	$this->sPageTitle = $s;
    }
    protected function HasPageTitle() {
	return !empty($this->sPageTitle);
    }
    protected function GetPageTitle() {
	return $this->sPageTitle;
    }
    private $sReqPriv;
    public function SetRequiredPrivilege($sPerm) {
	$this->sReqPriv = $sPerm;
    }
    protected function GetRequiredPrivilege() {
	return $this->sReqPriv;
    }
    private $sBasePath;
    public function SetBasePath($s) {
	$this->sBasePath = $s;
    }
    protected function GetBasePath() {
	return $this->sBasePath;
    }
    protected function HasBasePath() {
	return isset($this->sBasePath);
    }
    protected function GetBasePath_toUse() {
	if ($this->HasBasePath()) {
	    return $this->GetBasePath();
	} else {
	    return fcApp::Me()->GetKioskObject()->GetPagePath();
	}
    }
    protected function MakeURL_fromPath($sPath) {
	return $this->GetBasePath_toUse().$sPath;
    }

      //--fields--//

    // -- SETUP -- //
    // ++ EVENTS ++ //
/*
    protected function OnRunCalculations(){
	parent::OnRunCalculations();
	$this->SetIsAuthorized($this->FigureIfAuthorized());
    } */
    /*----
      NOTE: This has to intercept Render() rather than RenderContent() because fcNavBase::RenderContent()
	is only called if GetShouldDisplay() is true, and we might want to render the Page content to which
	this menu item is attached even if the menu item itself should not be rendered.
    */ /* moving to dropin.php
    public function Render() {
	if ($this->GetIsSelected() && $this->GetIsAuthorized()) {
	    //$this->DoSelect();
	    $this->PageRender();
	}
	return parent::Render();
    }
    protected function OnEventAfter($nEvent){
	$this->PageEvent($nEvent);
    }
*/
    // -- EVENTS -- //
    // ++ STATUS ++ //
    
      //++stored values++//

    // NEW
    private $sKeyValue;
    protected function SetKeyValue($s) {
	$this->sKeyValue = $s;
    }
    protected function GetKeyValue() {
	return $this->sKeyValue;
    }
    // NEW
    private $sKeyName;
    protected function SetKeyName($s) {
	$this->sKeyName = $s;
    }
    protected function GetKeyName() {
	return $this->sKeyName;
    }
    /*
    private $isAuth;
    protected function SetIsAuthorized($b) {
	$this->isAuth = $b;
    }
    protected function GetIsAuthorized() {
	return $this->isAuth;
    }*/
    
    // -- STATUS -- //
    // ++ CALCULATIONS ++ //

    /*----
      NEW
      NOTES:
	2017-01-01 Deleting unauthorized nodes ensures that nothing further happens if not authorized.
		More practically, it also means that the folder doesn't need to do a count of "permitted" nodes
		in order to decide whether or not to display itself; it can assume any remaining nodes are permitted.
		If we later decide NOT to delete unauthorized nodes, then we need
			(a) some way to prevent them from being selected
			(b) some reasonably efficient way for the parent-folder to count how many are permitted
	2017-02-10 Came really close to needing to hide the "users" node from unauthorized users while still allowing
	  them to access their own profile through it, but decided that "my profile" should be a "do:" command instead.
	  If we (later) want admins to be able to modify other users' profiles without logging in as them, we can add
	  an extra user ID parameter. No changes made here.
    */
    protected function FigureIfAuthorized() {
	$sPerm = $this->GetRequiredPrivilege();
	$ok = FALSE;
	if (is_null($sPerm)) {
	    // in this context, NULL permission means everyone is authorized
	    $ok = TRUE;
	} elseif (fcApp::Me()->UserIsLoggedIn()) {
	    if (fcApp::Me()->GetUserRecord()->CanDo($sPerm)) {
		// this item has been authorized for access
		$ok = TRUE;	// node has been authorized
	    }
	}
	if (!$ok) {
	    // We used to make sure the node has a parent before deleting it, but menu entries should never be the node-tree root.
	    // And actually, root nodes should respond sensibly to this request anyway.
	    $this->DeleteMe();
	}
	return $ok;
    }
    
    // -- CALCULATIONS -- //
    // ++ INPUT-TO-OUTPUT STATES ++ //
    
    private $isActive;
    protected function SetIsActive($b) {
	$this->isActive = $b;
    }
    protected function GetIsActive() {
	return $this->isActive;
    }
    /*----
      PURPOSE: This is essentially a stub routine for descendents to override
	if they want to impose conditions.
    */
    protected function GetShouldDisplay() {
	return TRUE;
    }

    // -- INPUT-TO-OUTPUT STATES -- //
    // ++ FRAMEWORK ++ //
    
    protected function GetKioskObject() {
	return fcApp::Me()->GetKioskObject();
    }
}