Example #1
0
 /**
  * Convert the diffs to row format.
  *
  * @return array
  */
 protected function convertDiffsToRows()
 {
     // return early if nothing found
     if (empty($this->diffs)) {
         return array();
     }
     // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
     $skipped = array('id');
     foreach ($this->diffs as $diff) {
         $table = $diff['table'];
         if (empty($metadata[$table])) {
             list($metadata[$table]['titles'], $metadata[$table]['values']) = $this->differ->titlesAndValuesForTable($table, $diff['log_date']);
         }
         $values = CRM_Utils_Array::value('values', $metadata[$diff['table']], array());
         $titles = $metadata[$diff['table']]['titles'];
         $field = $diff['field'];
         $from = $diff['from'];
         $to = $diff['to'];
         if ($this->raw) {
             $field = "{$table}.{$field}";
         } else {
             if (in_array($field, $skipped)) {
                 continue;
             }
             // $differ filters out === values; for presentation hide changes like 42 → '42'
             if ($from == $to) {
                 continue;
             }
             // special-case for multiple values. Also works for CRM-7251: preferred_communication_method
             if (substr($from, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR && substr($from, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR || substr($to, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR && substr($to, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) {
                 $froms = $tos = array();
                 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($from, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
                     $froms[] = CRM_Utils_Array::value($val, $values[$field]);
                 }
                 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($to, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
                     $tos[] = CRM_Utils_Array::value($val, $values[$field]);
                 }
                 $from = implode(', ', array_filter($froms));
                 $to = implode(', ', array_filter($tos));
             }
             if (isset($values[$field][$from])) {
                 $from = $values[$field][$from];
             }
             if (isset($values[$field][$to])) {
                 $to = $values[$field][$to];
             }
             if (isset($titles[$field])) {
                 $field = $titles[$field];
             }
             if ($diff['action'] == 'Insert') {
                 $from = '';
             }
             if ($diff['action'] == 'Delete') {
                 $to = '';
             }
         }
         $rows[] = array('field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to);
     }
     return $rows;
 }
 protected function diffsInTable($table)
 {
     $rows = array();
     $differ = new CRM_Logging_Differ($this->log_conn_id, $this->log_date);
     $diffs = $differ->diffsInTable($table, $this->cid);
     // return early if nothing found
     if (empty($diffs)) {
         return $rows;
     }
     list($titles, $values) = $differ->titlesAndValuesForTable($table);
     // populate $rows with only the differences between $changed and $original (skipping certain columns and NULL ↔ empty changes unless raw requested)
     $skipped = array('contact_id', 'entity_id', 'id');
     foreach ($diffs as $diff) {
         $field = $diff['field'];
         $from = $diff['from'];
         $to = $diff['to'];
         if ($this->raw) {
             $field = "{$table}.{$field}";
         } else {
             if (in_array($field, $skipped)) {
                 continue;
             }
             // $differ filters out === values; for presentation hide changes like 42 → '42'
             if ($from == $to) {
                 continue;
             }
             // only in PHP: '0' == false and null == false but '0' != null
             if ($from == FALSE and $to == FALSE) {
                 continue;
             }
             // CRM-7251: special-case preferred_communication_method
             if ($field == 'preferred_communication_method') {
                 $froms = array();
                 $tos = array();
                 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $from) as $val) {
                     $froms[] = CRM_Utils_Array::value($val, $values[$field]);
                 }
                 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $to) as $val) {
                     $tos[] = CRM_Utils_Array::value($val, $values[$field]);
                 }
                 $from = implode(', ', array_filter($froms));
                 $to = implode(', ', array_filter($tos));
             }
             if (isset($values[$field][$from])) {
                 $from = $values[$field][$from];
             }
             if (isset($values[$field][$to])) {
                 $to = $values[$field][$to];
             }
             if (isset($titles[$field])) {
                 $field = $titles[$field];
             }
             if ($diff['action'] == 'Insert') {
                 $from = '';
             }
             if ($diff['action'] == 'Delete') {
                 $to = '';
             }
         }
         $rows[] = array('field' => $field . " (id: {$diff['id']})", 'from' => $from, 'to' => $to);
     }
     return $rows;
 }