$params['image_URL'] = '';
}
- $contactType = CRM_Contact_BAO_ContactType::add($params);
+ $contactType = CRM_Contact_BAO_ContactType::writeRecord($params);
CRM_Core_Session::setStatus(ts("The Contact Type '%1' has been saved.",
[1 => $contactType->label]
), ts('Saved'), 'success');
* @param \Civi\Core\Event\PostEvent $event
*/
public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
+ /** @var CRM_Contact_DAO_ContactType $contactType */
+ $contactType = $event->object;
+ if ($event->action === 'edit' && $contactType->find(TRUE)) {
+ // Update navigation menu for contact type
+ $navigation = [
+ 'label' => ts("New %1", [1 => $contactType->label]),
+ 'name' => "New {$contactType->name}",
+ 'is_active' => $contactType->is_active,
+ ];
+ civicrm_api4('Navigation', 'save', [
+ 'checkPermissions' => FALSE,
+ 'records' => [$navigation],
+ 'match' => ['name'],
+ ]);
+ }
+ if ($event->action === 'create' && $contactType->find(TRUE)) {
+ $name = self::getBasicType($contactType->name);
+ if (!$name) {
+ return;
+ }
+ $navigation = [
+ 'label' => ts("New %1", [1 => $contactType->label]),
+ 'name' => "New {$contactType->name}",
+ 'url' => "civicrm/contact/add?ct=$name&cst={$contactType->name}&reset=1",
+ 'permission' => 'add contacts',
+ 'parent_id:name' => "New $name",
+ 'is_active' => $contactType->is_active,
+ ];
+ civicrm_api4('Navigation', 'save', [
+ 'checkPermissions' => FALSE,
+ 'records' => [$navigation],
+ 'match' => ['name'],
+ ]);
+ }
if ($event->action === 'delete') {
$sep = CRM_Core_DAO::VALUE_SEPARATOR;
- $subType = "$sep{$event->object->name}$sep";
+ $subType = "$sep{$contactType->name}$sep";
// For contacts with just the one sub-type, set to null
$sql = "
UPDATE civicrm_contact SET contact_sub_type = NULL
// remove navigation entry which was auto-created when this sub-type was added
\Civi\Api4\Navigation::delete(FALSE)
- ->addWhere('name', '=', "New {$event->object->name}")
+ ->addWhere('name', '=', "New {$contactType->name}")
->addWhere('url', 'LIKE', 'civicrm/contact/add%')
// Overide the default which limits to a single domain
->addWhere('domain_id', '>', 0)
->execute();
-
- Civi::cache('contactTypes')->clear();
}
+ Civi::cache('contactTypes')->clear();
}
/**
- * Add or update Contact SubTypes.
- *
- * @param array $params
- * An assoc array of name/value pairs.
- *
- * @return CRM_Contact_DAO_ContactType|NULL
+ * @deprecated
+ * @return CRM_Contact_DAO_ContactType
* @throws \CRM_Core_Exception
*/
public static function add($params) {
-
- // label or name
- if (empty($params['id']) && empty($params['label'])) {
- // @todo consider throwing exception instead.
- return NULL;
- }
- if (empty($params['id']) && empty($params['name'])) {
- $params['name'] = ucfirst(CRM_Utils_String::munge($params['label']));
- }
- if (!empty($params['parent_id']) &&
- !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id'])
- ) {
- return NULL;
- }
-
- $contactType = new CRM_Contact_DAO_ContactType();
- $contactType->copyValues($params);
- $contactType->id = $params['id'] ?? NULL;
-
- $contactType->save();
- if ($contactType->find(TRUE)) {
- $contactName = $contactType->name;
- $contact = ucfirst($contactType->label);
- $active = $contactType->is_active;
- }
-
- if (!empty($params['id'])) {
- $newParams = [
- 'label' => ts("New %1", [1 => $contact]),
- 'is_active' => $contactType->is_active,
- ];
- CRM_Core_BAO_Navigation::processUpdate(['name' => "New $contactName"], $newParams);
- }
- else {
- $name = self::getBasicType($contactName);
- if (!$name) {
- return NULL;
- }
- $value = ['name' => "New $name"];
- CRM_Core_BAO_Navigation::retrieve($value, $navinfo);
- $navigation = [
- 'label' => ts("New %1", [1 => $contact]),
- 'name' => "New $contactName",
- 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1",
- 'permission' => 'add contacts',
- 'parent_id' => $navinfo['id'],
- 'is_active' => $active,
- ];
- CRM_Core_BAO_Navigation::add($navigation);
- }
- CRM_Core_BAO_Navigation::resetNavigation();
- Civi::cache('contactTypes')->clear();
-
- return $contactType;
+ CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
+ return self::writeRecord($params);
}
/**
public $id;
/**
- * @var CRM_Core_DAO
+ * @var \CRM_Core_DAO
*/
public $object;
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->student = $params['name'];
$parents = 'indivi_parent' . substr(sha1(rand()), 0, 7);
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->parent = $params['name'];
$organizations = 'org_sponsor' . substr(sha1(rand()), 0, 7);
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->sponsor = $params['name'];
$this->individualParams = [
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->student = $params['name'];
$params = [
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->parent = $params['name'];
$params = [
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->sponsor = $params['name'];
$params = [
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->team = $params['name'];
}
'parent_id' => 1,
'is_active' => 1,
];
- $result = CRM_Contact_BAO_ContactType::add($params);
+ $result = CRM_Contact_BAO_ContactType::writeRecord($params);
$this->assertEquals($result->label, $params['label']);
$this->assertEquals($result->name, $params['name']);
$this->assertEquals($result->parent_id, $params['parent_id']);
'parent_id' => 2,
'is_active' => 0,
];
- $result = CRM_Contact_BAO_ContactType::add($params);
+ $result = CRM_Contact_BAO_ContactType::writeRecord($params);
$this->assertEquals($result->label, $params['label']);
$this->assertEquals($result->name, $params['name']);
$this->assertEquals($result->parent_id, $params['parent_id']);
CRM_Contact_BAO_ContactType::deleteRecord(['id' => $result->id]);
}
- /**
- * Test add() with invalid data
- */
- public function testAddInvalid1() {
-
- // parent id does not exist in db
- $params = [
- 'label' => 'subType',
- 'name' => 'subType',
- // non existent
- 'parent_id' => 100,
- 'is_active' => 1,
- ];
- $result = CRM_Contact_BAO_ContactType::add($params);
- $this->assertEquals($result, NULL);
- }
-
- public function testAddInvalid2() {
-
- // params does not have name and label keys
- $params = [
- 'parent_id' => 1,
- 'is_active' => 1,
- ];
- $result = CRM_Contact_BAO_ContactType::add($params);
- $this->assertEquals($result, NULL);
- }
-
- public function testAddInvalid3() {
-
- // params does not have parent_id
- $params = [
- 'label' => 'subType',
- 'name' => 'subType',
- 'is_active' => 1,
- ];
- $result = CRM_Contact_BAO_ContactType::add($params);
- $this->assertEquals($result, NULL);
- }
-
/**
* Test del() with valid data.
*/
'parent_id' => 1,
'is_active' => 1,
];
- $subtype = CRM_Contact_BAO_ContactType::add($params);
+ $subtype = CRM_Contact_BAO_ContactType::writeRecord($params);
$result = CRM_Contact_BAO_ContactType::subTypes();
$this->assertEquals(TRUE, in_array($subtype->name, $result, TRUE));
$this->callAPISuccess('ContactType', 'delete', ['id' => $subtype->id]);
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->student = $params['name'];
$params = [
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->parent = $params['name'];
$params = [
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->sponsor = $params['name'];
//create contacts
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->subTypeIndividual = $params['name'];
$params = [
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->subTypeOrganization = $params['name'];
$params = [
'parent_id' => 2,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$this->subTypeHousehold = $params['name'];
}
'parent_id' => 1,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$subtype = $params['name'];
// check for Type:Individual subype:sub_individual
'parent_id' => 3,
'is_active' => 1,
];
- CRM_Contact_BAO_ContactType::add($params);
+ CRM_Contact_BAO_ContactType::writeRecord($params);
$subtype = $params['name'];
// check for Type:Organization subype:sub_organization
// Menu item should have been auto-created
$this->assertCount(1, Navigation::get(FALSE)->addWhere('name', '=', 'New Tester')->execute());
+ ContactType::update(FALSE)
+ ->addWhere('name', '=', 'Tester')
+ ->addValue('label', 'Tested')
+ ->execute();
+
+ // Menu item should have been updated
+ $nav = Navigation::get(FALSE)->addWhere('name', '=', 'New Tester')->execute()->single();
+ $this->assertEquals('New Tested', $nav['label']);
+
ContactType::delete(FALSE)
->addWhere('name', '=', 'Tester')
->execute();