* deactivated, and deleted in tandem with their modules.
*/
class CRM_Core_ManagedEntities {
+
+ public static function getCleanupOptions() {
+ return array(
+ 'always' => ts('Always'),
+ 'never' => ts('Never'),
+ 'unused' => ts('If Unused'),
+ );
+ }
+
/**
* @var array($status => array($name => CRM_Core_Module))
*/
$dao->name = $todo['name'];
$dao->entity_type = $todo['entity'];
$dao->entity_id = $result['id'];
+ $dao->cleanup = CRM_Utils_Array::value('cleanup', $todo);
$dao->save();
}
$this->onApiError($params, $result);
}
}
+
+ if (isset($todo['cleanup'])) {
+ $dao->cleanup = $todo['cleanup'];
+ $dao->update();
+ }
}
/**
* @param CRM_Core_DAO_Managed $dao
*/
public function removeStaleEntity($dao) {
- $params = array(
- 'version' => 3,
- 'id' => $dao->entity_id,
- );
- $result = civicrm_api($dao->entity_type, 'delete', $params);
- if ($result['is_error']) {
- $this->onApiError($params, $result);
- }
+ $policy = empty($dao->cleanup) ? 'always' : $dao->cleanup;
+ $doDelete = ($policy == 'always');
- CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
- 1 => array($dao->id, 'Integer')
- ));
+ if ($doDelete) {
+ $params = array(
+ 'version' => 3,
+ 'id' => $dao->entity_id,
+ );
+ $result = civicrm_api($dao->entity_type, 'delete', $params);
+ if ($result['is_error']) {
+ $this->onApiError($params, $result);
+ }
+
+ CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
+ 1 => array($dao->id, 'Integer')
+ ));
+ }
}
/**
SET civicrm_navigation.url = CONCAT(SUBSTRING(url FROM 1 FOR LOCATE('&', url) - 1), '?', SUBSTRING(url FROM LOCATE('&', url) + 1))
WHERE civicrm_navigation.url LIKE "%&%" AND civicrm_navigation.url NOT LIKE "%?%";
+-- CRM-14478 Add a "cleanup" policy for managed entities
+ALTER TABLE `civicrm_managed`
+ADD COLUMN `cleanup` varchar(32) COMMENT 'Policy on when to cleanup entity (always, never, unused)';
+
-- CRM-14639
SELECT @option_grant_status := id FROM civicrm_option_group WHERE name = 'grant_status';
$this->assertEquals($foo['id'], $foo2['id']);
}
+ /**
+ * Set up an active module with one managed-entity using the
+ * policy "cleanup=>never". When the managed-entity goes away,
+ * ensure that the policy is followed (ie the entity is not
+ * deleted).
+ */
+ function testRemoveDeclaration_CleanupNever() {
+ $decls = array();
+
+ // create first managed entity ('foo')
+ $decls[] = array_merge($this->fixtures['com.example.one-foo'], array(
+ 'cleanup' => 'never'
+ ));
+ $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+ $me->reconcile();
+ $foo = $me->get('com.example.one', 'foo');
+ $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
+ $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
+
+ // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
+ $decls = array();
+ $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+ $me->reconcile();
+ $foo2 = $me->get('com.example.one', 'foo');
+ $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
+ $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
+ $this->assertEquals($foo['id'], $foo2['id']);
+ }
+
/**
* Setup an active module with a malformed entity declaration
*/
<comment>Foreign key to the referenced item.</comment>
<add>4.2</add>
</field>
+ <field>
+ <name>cleanup</name>
+ <type>varchar</type>
+ <length>32</length>
+ <comment>Policy on when to cleanup entity (always, never, unused)</comment>
+ <pseudoconstant>
+ <callback>CRM_Core_ManagedEntities::getCleanupOptions</callback>
+ </pseudoconstant>
+ <html>
+ <type>Select</type>
+ </html>
+ <add>4.5</add>
+ </field>
<index>
<name>UI_managed_entity</name>
<fieldName>entity_type</fieldName>