/** * Create, update, and delete entities declared by an active module * * @param $module string * @param $todos array $name => array() */ public function reconcileEnabledModule(CRM_Core_Module $module, $todos) { $dao = new CRM_Core_DAO_Managed(); $dao->module = $module->name; $dao->find(); while ($dao->fetch()) { if ($todos[$dao->name]) { // update existing entity; remove from $todos $defaults = array('id' => $dao->entity_id, 'is_active' => 1); // FIXME: test whether is_active is valid $params = array_merge($defaults, $todos[$dao->name]['params']); $result = civicrm_api($dao->entity_type, 'create', $params); if ($result['is_error']) { $this->onApiError($params, $result); } unset($todos[$dao->name]); } else { // remove stale entity; not in $todos $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'))); } } // create new entities from leftover $todos foreach ($todos as $name => $todo) { $result = civicrm_api($todo['entity'], 'create', $todo['params']); if ($result['is_error']) { $this->onApiError($todo['params'], $result); } $dao = new CRM_Core_DAO_Managed(); $dao->module = $todo['module']; $dao->name = $todo['name']; $dao->entity_type = $todo['entity']; $dao->entity_id = $result['id']; $dao->save(); } }
/** * Create a new entity * * @param array $todo entity specification (per hook_civicrm_managedEntities) */ public function insertNewEntity($todo) { $result = civicrm_api($todo['entity'], 'create', $todo['params']); if ($result['is_error']) { $this->onApiError($todo['entity'], 'create', $todo['params'], $result); } $dao = new CRM_Core_DAO_Managed(); $dao->module = $todo['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(); }