throw new RuntimeException("Failed to determine contact ID");
}
- //we need to get existing dashlets, so we know when to update or insert
- $contactDashlets = self::getContactDashlets(TRUE, $contactID);
-
$dashletIDs = array();
if (is_array($columns)) {
foreach ($columns as $colNo => $dashlets) {
$weight = 1;
foreach ($dashlets as $dashletID => $isMinimized) {
$isMinimized = (int) $isMinimized;
- if (in_array($dashletID, $contactDashlets)) {
- $query = " UPDATE civicrm_dashboard_contact
- SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
- WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
- }
- else {
- $query = " INSERT INTO civicrm_dashboard_contact
- ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
- VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
- }
+ $dashletID = (int) $dashletID;
+ $query = "INSERT INTO civicrm_dashboard_contact
+ (weight, is_minimized, column_no, is_active, dashboard_id, contact_id)
+ VALUES({$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID})
+ ON DUPLICATE KEY UPDATE weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1";
// fire update query for each column
- $dao = CRM_Core_DAO::executeQuery($query);
+ CRM_Core_DAO::executeQuery($query);
$dashletIDs[] = $dashletID;
$weight++;
}
}
- if (!empty($dashletIDs)) {
- // we need to disable widget that removed
- $updateQuery = " UPDATE civicrm_dashboard_contact
- SET is_active = 0
- WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
- }
- else {
- // this means all widgets are disabled
- $updateQuery = " UPDATE civicrm_dashboard_contact
- SET is_active = 0
- WHERE contact_id = {$contactID}";
- }
+ // Disable inactive widgets
+ $dashletClause = $dashletIDs ? "dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
+ $updateQuery = "UPDATE civicrm_dashboard_contact
+ SET is_active = 0
+ WHERE $dashletClause AND contact_id = {$contactID}";
CRM_Core_DAO::executeQuery($updateQuery);
}
$this->addTask(ts('Alter index and type for image URL'), 'alterIndexAndTypeForImageURL');
}
+ /**
+ * Upgrade function.
+ *
+ * @param string $rev
+ */
+ public function upgrade_4_7_11($rev) {
+ $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+ $this->addTask('Dashboard schema updates', 'dashboardSchemaUpdate');
+ }
+
/*
* Important! All upgrade functions MUST call the 'runSql' task.
* Uncomment and use the following template for a new upgrade version
return TRUE;
}
+ /**
+ * CRM-17663 - Dashboard schema changes
+ *
+ * @param \CRM_Queue_TaskContext $ctx
+ *
+ * @return bool
+ */
+ public function dashboardSchemaUpdate(CRM_Queue_TaskContext $ctx) {
+ if (!CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_dashboard_contact', 'index_dashboard_id_contact_id')) {
+ // Delete any stray duplicate rows and add unique index to prevent new dupes and enable INSERT/UPDATE combo query
+ CRM_Core_DAO::executeQuery('DELETE c1 FROM civicrm_dashboard_contact c1, civicrm_dashboard_contact c2 WHERE c1.contact_id = c2.contact_id AND c1.dashboard_id = c2.dashboard_id AND c1.id > c2.id');
+ CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_dashboard_contact ADD UNIQUE INDEX index_dashboard_id_contact_id (dashboard_id, contact_id);');
+ }
+ }
+
/**
* CRM-19100 - Alter Index and Type for Image URL
* @return bool