Deprecates the CRM_Core_BAO_Note::del() function and refactors out all references to it.
Related to work on dev/core#2757
$session->pushUserContext($url);
if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
- CRM_Core_BAO_Note::del($this->_id);
+ $this->delete();
CRM_Utils_System::redirect($url);
}
}
/**
- * Delete the note object from the db.
+ * Delete the note object from the db and set a status msg.
*/
public function delete() {
- CRM_Core_BAO_Note::del($this->_id);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $this->_id]);
+ $status = ts('Selected Note has been deleted successfully.');
+ CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
}
/**
$note = CRM_Core_BAO_Note::getNote($id, 'civicrm_contribution');
$noteId = key($note);
if ($noteId) {
- CRM_Core_BAO_Note::del($noteId, FALSE);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $noteId]);
}
$dao = new CRM_Contribute_DAO_Contribution();
*/
public static function processNote($params, $contactID, $contributionID, $contributionNoteID = NULL) {
if (CRM_Utils_System::isNull($params['note']) && $contributionNoteID) {
- CRM_Core_BAO_Note::del($contributionNoteID);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $contributionNoteID]);
+ $status = ts('Selected Note has been deleted successfully.');
+ CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
return;
}
//process note
/**
* BAO object for crm_note table.
*/
-class CRM_Core_BAO_Note extends CRM_Core_DAO_Note {
+class CRM_Core_BAO_Note extends CRM_Core_DAO_Note implements \Civi\Test\HookInterface {
use CRM_Core_DynamicFKAccessTrait;
/**
return $notes;
}
+ /**
+ * Event fired prior to modifying a Note.
+ * @param \Civi\Core\Event\PreEvent $event
+ */
+ public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
+ if ($event->action === 'delete' && $event->id) {
+ // When deleting a note, also delete child notes
+ // This causes recursion as this hook is called again while deleting child notes,
+ // So the children of children, etc. will also be deleted.
+ foreach (self::getDescendentIds($event->id) as $child) {
+ self::deleteRecord(['id' => $child]);
+ }
+ }
+ }
+
/**
* Delete the notes.
*
* @param int $id
- * Note id.
- * @param bool $showStatus
- * Do we need to set status or not.
*
- * @return int|null
- * no of deleted notes on success, null otherwise
+ * @deprecated
+ * @return int
*/
- public static function del($id, $showStatus = TRUE) {
- $return = NULL;
- $recent = array($id);
- $note = new CRM_Core_DAO_Note();
- $note->id = $id;
- $note->find();
- $note->fetch();
- if ($note->entity_table == 'civicrm_note') {
- $status = ts('Selected Comment has been deleted successfully.');
- }
- else {
- $status = ts('Selected Note has been deleted successfully.');
- }
-
- // Delete all descendents of this Note
- foreach (self::getDescendentIds($id) as $childId) {
- $childNote = new CRM_Core_DAO_Note();
- $childNote->id = $childId;
- $childNote->delete();
- $recent[] = $childId;
- }
-
- $return = $note->delete();
- if ($showStatus) {
- CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
- }
+ public static function del($id) {
+ // CRM_Core_Error::deprecatedFunctionWarning('deleteRecord');
+ self::deleteRecord(['id' => $id]);
- // delete the recently created Note
- foreach ($recent as $recentId) {
- $noteRecent = array(
- 'id' => $recentId,
- 'type' => 'Note',
- );
- CRM_Utils_Recent::del($noteRecent);
- }
- return $return;
+ return 1;
}
/**
}
/**
- * Given a note id, get a list of the ids of all notes that are descendents of that note
+ * Get direct children of given parentId note
*
* @param int $parentId
- * Id of the given note.
- * @param array $ids
- * (reference) one-dimensional array to store found descendent ids.
*
* @return array
- * One-dimensional array containing ids of all desendent notes
+ * One-dimensional array containing ids of child notes
*/
- public static function getDescendentIds($parentId, &$ids = []) {
- // get direct children of given parentId note
+ public static function getDescendentIds($parentId) {
+ $ids = [];
$note = new CRM_Core_DAO_Note();
$note->entity_table = 'civicrm_note';
$note->entity_id = $parentId;
$note->find();
while ($note->fetch()) {
- // foreach child, add to ids list, and recurse
$ids[] = $note->id;
- self::getDescendentIds($note->id, $ids);
}
return $ids;
}
$contactNoteId = CRM_Core_DAO::executeQuery($contactQuery, $params);
while ($contactNoteId->fetch()) {
- self::del($contactNoteId->id, FALSE);
+ self::deleteRecord(['id' => $contactNoteId->id]);
}
}
CRM_Core_BAO_Note::add($noteParams, $noteIDs);
}
elseif ($noteId && $hasNoteField) {
- CRM_Core_BAO_Note::del($noteId, FALSE);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $noteId]);
}
}
$note = CRM_Core_BAO_Note::getNote($id, 'civicrm_participant');
$noteId = key($note);
if ($noteId) {
- CRM_Core_BAO_Note::del($noteId, FALSE);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $noteId]);
}
$participant->delete();
}
if ($this->_action & CRM_Core_Action::DELETE) {
- CRM_Core_BAO_Note::del($this->_id);
+ CRM_Core_BAO_Note::deleteRecord(['id' => $this->_id]);
+ $status = ts('Selected Note has been deleted successfully.');
+ CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
return;
}
* @return array
*/
function civicrm_api3_note_delete($params) {
- $result = new CRM_Core_BAO_Note();
- return $result->del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting Note');
+ $result = CRM_Core_BAO_Note::deleteRecord($params);
+ return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting Note');
}
/**
--- /dev/null
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+namespace api\v4\Entity;
+
+use api\v4\UnitTestCase;
+use Civi\Api4\Note;
+use Civi\Test\TransactionalInterface;
+
+/**
+ * @group headless
+ */
+class NoteTest extends UnitTestCase implements TransactionalInterface {
+
+ public function testDeleteWithChildren() {
+ $c1 = $this->createEntity(['type' => 'Individual']);
+
+ $text = uniqid(__FUNCTION__, TRUE);
+
+ // Create 2 top-level notes.
+ $notes = Note::save(FALSE)
+ ->setRecords([['note' => $text], ['note' => $text]])
+ ->setDefaults([
+ 'entity_id' => $c1['id'],
+ 'entity_table' => 'civicrm_contact',
+ ])->execute();
+
+ // Add 2 children of the first note.
+ $children = Note::save(FALSE)
+ ->setRecords([['note' => $text], ['note' => $text]])
+ ->setDefaults([
+ 'entity_id' => $notes->first()['id'],
+ 'entity_table' => 'civicrm_note',
+ ])->execute();
+
+ // Add 2 children of the first child.
+ $grandChildren = Note::save(FALSE)
+ ->setRecords([['note' => $text], ['note' => $text]])
+ ->setDefaults([
+ 'entity_id' => $children->first()['id'],
+ 'entity_table' => 'civicrm_note',
+ ])->execute();
+
+ // We just created 2 top-level notes and 4 children. Ensure we have a total of 6.
+ $existing = Note::get(FALSE)
+ ->addWhere('note', '=', $text)
+ ->execute();
+ $this->assertCount(6, $existing);
+
+ // Delete parent
+ Note::delete(FALSE)
+ ->addWhere('id', '=', $notes->first()['id'])
+ ->execute();
+
+ // Should have deleted 1 parent + 4 child-notes, for a new total of 1 remaining.
+ $existing = Note::get(FALSE)
+ ->addWhere('note', '=', $text)
+ ->execute();
+ $this->assertCount(1, $existing);
+ }
+
+}