Skip to content
Snippets Groups Projects
noteaccess.php 4.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • bgm's avatar
    bgm committed
    <?php
    
    require_once 'noteaccess.civix.php';
    // phpcs:disable
    use CRM_Noteaccess_ExtensionUtil as E;
    // phpcs:enable
    
    /**
     * Implements hook_civicrm_config().
     *
     * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
     */
    function noteaccess_civicrm_config(&$config) {
      _noteaccess_civix_civicrm_config($config);
    
    bgm's avatar
    bgm committed
    
      if (isset(Civi::$statics[__FUNCTION__])) { return; }
      Civi::$statics[__FUNCTION__] = 1;
    
      // Run with a high priority, to run before other export hooks which may halt execution
      Civi::dispatcher()->addListener('hook_civicrm_export', '_noteaccess_civicrm_export', 500);
    
    bgm's avatar
    bgm committed
    }
    
    /**
     * Implements hook_civicrm_install().
     *
     * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
     */
    function noteaccess_civicrm_install(): void {
      _noteaccess_civix_civicrm_install();
    }
    
    /**
     * Implements hook_civicrm_enable().
     *
     * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
     */
    function noteaccess_civicrm_enable(): void {
      _noteaccess_civix_civicrm_enable();
    }
    
    /**
     * Implements hook_civicrm_buildForm().
     *
     * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm
     */
    function noteaccess_civicrm_buildForm($formName, &$form): void {
      if ($formName == 'CRM_Note_Form_Note') {
    
        CRM_Noteaccess_Form_Note::buildForm($form);
    
      }
    }
    
    /**
     * Implements hook_civicrm_postProcess().
     *
     * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postProcess
     */
    function noteaccess_civicrm_postProcess($formName, &$form): void {
      if ($formName == 'CRM_Note_Form_Note') {
    
        CRM_Noteaccess_Form_Note::postProcess($form);
    
    bgm's avatar
    bgm committed
      }
    }
    
    
    /**
     * Apply the note privacy conditions when a list of people are specified
     *
     * Implements hook_civicrm_notePrivacy()
     */
    function noteaccess_civicrm_notePrivacy(&$note) {
      // Find the option_value from this extension
      $optionValue = \Civi\Api4\OptionValue::get(TRUE)
        ->addWhere('option_group_id:name', '=', 'note_privacy')
        ->addWhere('name', '=', 'specific_contacts')
        ->execute()
        ->first();
    
      if ($note['privacy'] == $optionValue['value']) {
        $userID = CRM_Core_Session::getLoggedInContactID();
    
        // Always allow the note author
        if ($note['contact_id'] == $userID) {
          $note['notePrivacy_hidden'] = FALSE;
          return;
        }
    
        // Check the allowed contacts
        $allowed = \Civi\Api4\NoteAccess::get(FALSE)
          ->addSelect('contact_id')
          ->addWhere('note_id', '=', $note['id'])
          ->execute()
          ->indexBy('contact_id');
    
        if (isset($allowed[$userID])) {
          $note['notePrivacy_hidden'] = FALSE;
          return;
        }
    
        $note['notePrivacy_hidden'] = TRUE;
      }
    }
    
    
    bgm's avatar
    bgm committed
    /**
     * Hide the notes from contact exports.
     * Workaround because notePrivacy does not work.
     *
     * Implements hook_civicrm_export() using Symfony hooks
     */
    function _noteaccess_civicrm_export($event) {
      // Hides the column title
      $key = array_search(ts('Note(s)'), $event->headerRows);
      if ($key !== FALSE) {
        unset($event->headerRows[$key]);
      }
    
      // Unset the data rows
      unset($event->sqlColumns['notes']);
    }
    
    
    /**
     * Recalculate the count of notes, to include the hidden ones
     *
     * Implements hook_civicrm_tabset
     */
    function noteaccess_civicrm_tabset($tabsetName, &$tabs, $context) {
      if ($tabsetName != 'civicrm/contact/view') {
        return;
      }
    
      $contact_id = $context['contact_id'];
    
      // @todo Add a setting for this behaviour?
      foreach ($tabs as $key => $tab) {
        if ($tab['id'] == 'note') {
          $notes = \Civi\Api4\Note::get(TRUE)
            ->selectRowCount()
            ->addSelect('id')
            ->addWhere('entity_table', '=', 'civicrm_contact')
            ->addWhere('entity_id', '=', $contact_id)
            ->execute();
    
          $tabs[$key]['count'] = $notes->rowCount;
        }
      }
    }
    
    /**
     * Display a warning about hidden notes, so that people know who to contact
     *
     * Implements hook_civicrm_pageRun()
     */
    function noteaccess_civicrm_pageRun(&$page) {
      $pageName = get_class($page);
    
      if ($pageName == 'CRM_Contact_Page_View_Note') {
    
    bgm's avatar
    bgm committed
        CRM_Noteaccess_Page_Note::pageRun($page);
      }
    }
    
    /**
     * On the Contact Notes tab, hide the edit/delete links if the note is restricted
     */
    
    function noteaccess_civicrm_links(string $op, $objectName, $objectID, &$links, &$mask, &$values) {
    
      if ($objectName == 'Note') {
        if (!CRM_Noteaccess_Utils::userCanEditNote($objectID)) {
          foreach ($links as $key => $link) {
            if ($link['bit'] == CRM_Core_Action::DELETE || $link['bit'] == CRM_Core_Action::UPDATE) {
              unset($links[$key]);
            }
          }
        }
      }
    }