-- fix for last modified. Now set on parent folders as well

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_SetLastModified') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_SetLastModified
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_SetLastModified
 @PortalId Int,
 @EntryId Int,
 @LastModified DATETIME = NULL
AS

BEGIN
 DECLARE @entrypath VARCHAR(4000)
 SET @entrypath = (SELECT [Path] 
   FROM {databaseOwner}{objectQualifier}DMX_Entries
   WHERE EntryId = @EntryId);
 IF @LastModified IS NULL
  SET @LastModified = GETDATE();
 DISABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
 UPDATE {databaseOwner}{objectQualifier}DMX_Entries
 SET LastModified=@LastModified
 WHERE PortalId=@PortalId
 AND @entrypath LIKE [Path]+'%';
 ENABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
END

GO

-- repair recycle bin
UPDATE {databaseOwner}{objectQualifier}DMX_Extensions
SET NodePattern='ent(\d+)|recyclebin'
WHERE ExtensionKey= 'Contents.Collections'
GO

-- improvement for notification adding
IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_AddNotification') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_AddNotification
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddNotification
	@UserId Int,	
	@LogId Int,
	@PortalId Int,
	@Sent DateTime,
	@Template NVarChar (100)
AS

IF NOT EXISTS (SELECT * FROM {databaseOwner}{objectQualifier}DMX_Notifications
 WHERE UserId=@UserId AND LogId=@LogId AND PortalId=@PortalId AND Template=@Template)
INSERT INTO {databaseOwner}{objectQualifier}DMX_Notifications (
	[UserId],
	[LogId],
	PortalId,
	Sent,
	Template
) VALUES (
	@UserId,
	@LogId,
	@PortalId,
	@Sent,
	@Template
)

GO

