Example #1
0
 function init()
 {
     $this->head_items->add_stylesheet(REASON_ADMIN_CSS_DIRECTORY . 'archive.css');
     $this->current = new entity($this->admin_page->id);
     $this->_current_user = new entity($this->admin_page->user_id);
     $this->admin_page->title = 'History of "' . $this->current->get_value('name') . '"';
     $this->ignore_fields = array('id', 'last_edited_by', 'last_modified', 'creation_date', 'type', 'created_by', 'new', 'state');
     // get archive relationship id
     $this->rel_id = reason_get_archive_relationship_id($this->admin_page->type_id);
     $es = new entity_selector();
     $es->add_type($this->admin_page->type_id);
     $es->add_right_relationship($this->admin_page->id, $this->rel_id);
     $es->set_order('last_modified DESC, entity.id DESC');
     $archived = $es->run_one(false, 'Archived');
     $this->_locks[$this->current->id()] = array();
     // No problem replacing current entity with itself!
     foreach ($archived as $archive_id => $archive_entity) {
         $this->_locks[$archive_id] = $this->_get_archive_lock_info($archive_entity);
     }
     $history_top = array($this->current->id() => $this->current);
     $this->history = $history_top + $archived;
 }
	/**
	 * Updates entity with id = $id with the values of the array $values
	 *
	 * reason_update_entity() provides an easier-to-use interface 
	 * where the updates do not need to be organized by table.
	 *
	 * @todo figure out how to refresh the entity data cache on update
	 *
	 * @param integer $id ID of entity to update
	 * @param integer $user_id Reason ID of user making changes
	 * @param array $updates array of tables with values being array of key-val pairs
	 * @param boolean $archive boolean that determines whether this update will be archived
	 * @return boolean true => entity has changed,  false => entity has not changed
	 */
	function update_entity( $id, $user_id, $raw_updates = array(), $archive = true )
	{
		$original = new entity($id, false);
		if (reason_is_entity($original, true)) // must be an entity of some type.
		{
			if (isset($raw_updates['entity']['type']) && ($raw_updates['entity']['type'] != $original->get_value('type')))
			{
				trigger_error('update_entity cannot be used to change the type of an entity.');
			}
			foreach( $raw_updates AS $table => $raw_fields )
			{
				$updates[$table] = reason_sanitize_values($original->get_value('type'), $raw_fields);
				if (empty($changed))
				{
					foreach ($updates[$table] as $k => $v) // see if this represents a change.
					{
						if ($v != $original->get_value($k))
						{
							$changed = true;
							break;
						}
					}
				}
			}
			if (!empty($changed)) // some change took place
			{
				if (!isset($updates['entity']['last_edited_by'])) $updates['entity']['last_edited_by'] = $user_id;
				if (!isset($updates['entity']['last_modified'])) $updates['entity']['last_modified'] = get_mysql_datetime();
				foreach  ($updates as $table => $fields)
				{
					$GLOBALS['sqler']->update_one( $table, $fields, $id );
				}
				if (!empty($archive))
				{
					$archived_id = duplicate_entity( $original, false, true, array( 'state' => 'Archived' ) );
					$rel_id = reason_get_archive_relationship_id($original->get_value('type'));
					create_relationship( $id, $archived_id, $rel_id );
				}
				$updated_entity = new entity($id, false);
				$updated_entity->get_values();
				// If the unique_name changes on the updated entity, or a uniquely named entity is deleted or undeleted, lets update the unique name cache
				if ($updated_entity->get_value('unique_name') != $original->get_value('unique_name') ||
					($original->get_value('state') != 'Deleted' && $updated_entity->get_value('state') == 'Deleted' && $original->get_value('unique_name')) ||
					($original->get_value('state') == 'Deleted' && $updated_entity->get_value('state') != 'Deleted' && $updated_entity->get_value('unique_name')))
				{
					reason_refresh_unique_names();
				}
				return true;
			}
		}
		else
		{
			trigger_error('update_entity requires a valid entity id (was given ' . $id . ').');
		}
		return false;
	}
Example #3
0
 function _get_archived_item_count($id, $type_id)
 {
     $rel_id = reason_get_archive_relationship_id($type_id);
     $es = new entity_selector();
     $es->add_type($type_id);
     $es->add_right_relationship($id, $rel_id);
     $es->limit_tables();
     $es->limit_fields();
     $archived = $es->run_one(false, 'Archived', 'show_archived error in CM');
     return count($archived);
 }
 /**
  * Get the archived entities that represent the state of the item at
  * the beginning of a given start date and at the end of a given end date
  *
  * @param object $item reason entity
  * @param string $start_date
  * @param string $end_date
  * @return array form: array('start'=>entity,'end'=>entity)
  */
 function _get_archived_entities($item, $start_date, $end_date)
 {
     //echo $start_date.'.'.$end_date;
     $ret = array('start' => NULL, 'end' => NULL);
     $es = new entity_selector();
     $es->add_type($item->get_value('type'));
     $es->add_right_relationship($item->id(), reason_get_archive_relationship_id($item->get_value('type')));
     $es->add_relation('entity.last_modified < "' . addslashes($start_date) . '"');
     $es->set_order('entity.last_modified DESC');
     $es->set_num(1);
     $starts = $es->run_one(false, 'Archived');
     if (!empty($starts)) {
         $ret['start'] = current($starts);
     }
     $es = new entity_selector();
     $es->add_type($item->get_value('type'));
     $es->add_right_relationship($item->id(), reason_get_archive_relationship_id($item->get_value('type')));
     $es->add_relation('entity.last_modified <= "' . addslashes($end_date) . ' 23:59:59"');
     $es->set_order('entity.last_modified DESC');
     $es->set_num(1);
     $ends = $es->run_one(false, 'Archived');
     if (!empty($ends)) {
         $ret['end'] = current($ends);
     }
     return $ret;
 }