예제 #1
0
 /**
  * Get the differences between the actual data and the old data of one item.
  *
  * The function will inspect and collect all the fields that have other value than before.
  *
  * For add action, return all the values.
  * For edit action, return only the fields that have changes.
  * For delete action, return all the values.
  *
  * @param Phprojekt_Item_Abstract $object The item object.
  * @param string                  $action Action (edit/add/delete).
  *
  * @return array The array with 'oldValue' and 'newValue'.
  */
 private function _getDifferences(Phprojekt_Item_Abstract $object, $action)
 {
     $getter = 'getShortFieldDefinition';
     $order = Phprojekt_ModelInformation_Default::ORDERING_FORM;
     if (method_exists($object->getInformation(), $getter)) {
         $fields = call_user_func(array($object->getInformation(), $getter), $order);
     } else {
         $fields = $object->getInformation()->getFieldDefinition($order);
     }
     $clone = clone $object;
     $clone->find($object->id);
     $differences = array();
     if ($action == 'edit') {
         foreach ($fields as $value) {
             $fieldName = $value['key'];
             if ($value['type'] == 'textarea') {
                 $objectFieldName = str_replace("\n", "", strip_tags($object->{$fieldName}));
                 $cloneFieldName = str_replace("\n", "", strip_tags($clone->{$fieldName}));
             } else {
                 $objectFieldName = $object->{$fieldName};
                 $cloneFieldName = $clone->{$fieldName};
             }
             if ($objectFieldName != $cloneFieldName) {
                 $oldValue = $this->_convertDateTimes($clone->{$fieldName}, $value['type'], 'userToUtc');
                 $newValue = $this->_convertDateTimes($object->{$fieldName}, $value['type'], 'userToUtc');
                 $differences[$fieldName] = array('oldValue' => $oldValue, 'newValue' => $newValue);
             }
         }
     } else {
         if ($action == 'add') {
             foreach ($fields as $value) {
                 $fieldName = $value['key'];
                 if ($value['type'] == 'textarea') {
                     $objectFieldName = str_replace("\n", "", strip_tags($object->{$fieldName}));
                 } else {
                     $objectFieldName = $object->{$fieldName};
                 }
                 if (!empty($objectFieldName)) {
                     $newValue = $this->_convertDateTimes($object->{$fieldName}, $value['type'], 'userToUtc');
                     $differences[$fieldName] = array('oldValue' => '', 'newValue' => $object->{$fieldName});
                 }
             }
         } else {
             if ($action == 'delete') {
                 foreach ($fields as $value) {
                     $fieldName = $value['key'];
                     $oldValue = $this->_convertDateTimes($clone->{$fieldName}, $value['type'], 'userToUtc');
                     $differences[$fieldName] = array('oldValue' => $clone->{$fieldName}, 'newValue' => '');
                 }
             }
         }
     }
     return $differences;
 }
예제 #2
0
 /**
  * Returns the last changes, if there are any, for a specific module and item id.
  *
  * The result data is used by Mail_Notification class, when telling the users related
  * to an item that it has been modified.
  *
  * @param Phprojekt_Item_Abstract $object The item object
  *
  * @return array Array with 'userId', 'moduleId', 'itemId', 'field', 'label',
  *                          'oldValue', 'newValue', 'action' and 'datetime'.
  */
 public function getLastHistoryData($object)
 {
     $result = array();
     $moduleId = Phprojekt_Module::getId($object->getModelName());
     $itemId = $object->id;
     $where = sprintf('module_id = %d AND item_id = %d', (int) $moduleId, (int) $itemId);
     $fields = array();
     $fieldDefinition = $object->getInformation()->getFieldDefinition();
     foreach ($fieldDefinition as $field) {
         $fields[$field['key']] = $field;
     }
     $datetime = null;
     $action = null;
     $history = $this->fetchAll($where, 'id DESC');
     $stop = false;
     foreach ($history as $row) {
         if (!$stop) {
             if (null === $datetime) {
                 $datetime = $row->datetime;
                 $action = $row->action;
             }
             if ($action == $row->action) {
                 $diff = abs(strtotime($datetime) - strtotime($row->datetime));
                 if ($diff < 1) {
                     $result[] = array('userId' => $row->userId, 'moduleId' => $row->moduleId, 'itemId' => $row->itemId, 'field' => $row->field, 'label' => isset($fields[$row->field]) ? $fields[$row->field]['label'] : $row->field, 'oldValue' => $row->oldValue, 'newValue' => $row->newValue, 'action' => $row->action, 'datetime' => $row->datetime);
                 } else {
                     $stop = true;
                     break;
                 }
             } else {
                 $stop = true;
                 break;
             }
         }
     }
     return array_reverse($result);
 }