Newer
Older
<?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);
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);
}
/**
* 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') {

bgm
committed
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') {

bgm
committed
CRM_Noteaccess_Form_Note::postProcess($form);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* 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;
}
}
/**
* 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']);
}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* 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') {
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]);
}
}
}
}
}