Edit via SFTP
  1. <?php namespace ferret;
  2. /*
  3.  FOLDER DOC: https://htyp.org/Ferreteria/v0.4/file/menu/items
  4. */
  5. /*::::
  6.   PURPOSE: base class for menu-links which are usage-aware
  7.   Base class does not define how usage is determined.
  8.   It is also still PASSIVE - status checks happen when rendering.
  9.   ADDS: usage-awareness
  10.   HISTORY:
  11.   2017-02-08 Can't see any reason for ActionClass methods to be defined here; moving them to Dropin Link class
  12.   (in dropin.php).
  13.   2017-03-26 Figuring authorization at RunCalculations time now, instead of at Render time, because in the latter case
  14.   we don't have the list of missing permits at render time (when we might want to display them).
  15. */
  16. abstract class cMenuLink extends \fcNavLink {
  17. #use ftLinkKey;
  18.  
  19. // ++ SETUP ++ //
  20.  
  21. /*----
  22.   CONSTRUCTOR INPUT ARRAY:
  23.   array['base'] = base URL to prefix all output URLs
  24.   array['name'] = value of unique key
  25.   array['value'] = (optional) text to display
  26.   array['popup'] = (optional) hover-over text
  27.   */
  28.  
  29. const PERM_NONE = '';
  30. protected function SetupDefaults() : void {
  31. parent::SetupDefaults();
  32. // this class is security-aware, but assumes permission by default:
  33. $this->SetRequiredPrivilege(self::PERM_NONE); // total lack of permit is adequate
  34. }
  35.  
  36. //++fields++//
  37.  
  38. private $sPageTitle = NULL;
  39. public function SetPageTitle(string $s) { $this->sPageTitle = $s; }
  40. protected function HasPageTitle() : bool { return !is_null($this->sPageTitle); }
  41. protected function GetPageTitle() : string { return $this->sPageTitle; }
  42.  
  43. private $sReqPriv = '';
  44. public function SetRequiredPrivilege(string $sPerm) { $this->sReqPriv = $sPerm; }
  45. protected function GetRequiredPrivilege() : string { return $this->sReqPriv; }
  46.  
  47. protected function MakeURL_fromPath(string $sPath) : string {
  48. $osBase = $this->BasePath();
  49. $wpBase = $osBase->GetIt();
  50. if (strlen($wpBase) == 0) {
  51. $wpBase .= \ferret\globals\cAbstract::Me()->WebPath_Separator();
  52. }
  53. return $wpBase.$sPath;
  54. }
  55.  
  56. //--fields--//
  57.  
  58. // -- SETUP -- //
  59. // ++ CALCULATIONS ++ //
  60.  
  61. /*----
  62.   NEW
  63.   NOTES:
  64.   2017-01-01 Deleting unauthorized nodes ensures that nothing further happens if not authorized.
  65.   More practically, it also means that the folder doesn't need to do a count of "permitted" nodes
  66.   in order to decide whether or not to display itself; it can assume any remaining nodes are permitted.
  67.   If we later decide NOT to delete unauthorized nodes, then we need
  68.   (a) some way to prevent them from being selected
  69.   (b) some reasonably efficient way for the parent-folder to count how many are permitted
  70.   2017-02-10 Came really close to needing to hide the "users" node from unauthorized users while still allowing
  71.   them to access their own profile through it, but decided that "my profile" should be a "do:" command instead.
  72.   If we (later) want admins to be able to modify other users' profiles without logging in as them, we can add
  73.   an extra user ID parameter. No changes made here.
  74.   */
  75. protected function FigureIfAuthorized() : bool {
  76. $sPerm = $this->GetRequiredPrivilege();
  77. $ok = FALSE;
  78. $oApp = \fcApp::Me();
  79. if ($sPerm == self::PERM_NONE) {
  80. // in this context, NULL permission means everyone is authorized
  81. $ok = TRUE;
  82. } elseif ($oApp->UserIsLoggedIn()) {
  83. if ($oApp->UserStatus()->Record()->GetIt()->CanDo($sPerm)) {
  84. // this item has been authorized for access
  85. $ok = TRUE; // node has been authorized
  86. }
  87. }
  88. if (!$ok) {
  89. // We used to make sure the node has a parent before deleting it, but menu entries should never be the node-tree root.
  90. // And actually, root nodes should respond sensibly to this request anyway.
  91. $this->DeleteMe();
  92. }
  93. return $ok;
  94. }
  95.  
  96. // -- CALCULATIONS -- //
  97. // ++ INPUT-TO-OUTPUT STATES ++ //
  98.  
  99. private $isActive;
  100. protected function SetIsActive(bool $b) { $this->isActive = $b; }
  101. protected function GetIsActive() : bool { return $this->isActive; }
  102. /*----
  103.   PURPOSE: This is essentially a stub routine for descendents to override
  104. if they want to impose conditions.
  105.   */
  106. protected function GetShouldDisplay() : bool { return TRUE; }
  107.  
  108. // -- INPUT-TO-OUTPUT STATES -- //
  109. }
  110.