Example #1
0
        public ArticleWorkflowState GetWorkFlowState(Guid articleId)
        {
            var item = _sitecoreMasterService.GetItem <Item>(articleId);

            if (item?.State?.GetWorkflowState() == null)
            {
                return(null);
            }
            var currentState = item.State.GetWorkflowState();

            Sitecore.Workflows.IWorkflow workflow = item.State.GetWorkflow();
            var workFlowState = new ArticleWorkflowState();

            workFlowState.IsFinal     = currentState.FinalState;
            workFlowState.DisplayName = currentState.DisplayName;
            var commands = new List <ArticleWorkflowCommand>();

            foreach (Sitecore.Workflows.WorkflowCommand command in workflow.GetCommands(item))
            {
                var wfCommand = new ArticleWorkflowCommand();
                wfCommand.DisplayName = command.DisplayName;
                wfCommand.StringID    = command.CommandID;
                ICommand commandItem   = _sitecoreMasterService.GetItem <ICommand>(new Guid(command.CommandID));
                IState   nextStateItem = _sitecoreMasterService.GetItem <IState>(commandItem.Next_State);

                if (nextStateItem != null)
                {
                    wfCommand.SendsToFinal     = nextStateItem.Final;
                    wfCommand.GlobalNotifyList = new List <StaffStruct>();
                    foreach (var staff in nextStateItem.Staffs)
                    {
                        var staffItem = _sitecoreMasterService.GetItem <IStaff_Item>(staff._Id);
                        if (staffItem.Inactive)
                        {
                            continue;
                        }
                        var staffMember = new StaffStruct
                        {
                            ID   = staffItem._Id,
                            Name = staffItem.Last_Name + " , " + staffItem.First_Name
                        };
                        //   staffMember.Publications = staff  //TODO :Check if this field if we need this field.
                        wfCommand.GlobalNotifyList.Add(staffMember);
                    }
                    commands.Add(wfCommand);
                }
            }
            workFlowState.Commands = commands;

            return(workFlowState);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="i"></param>
        /// <param name="commandID">Optional: if included, the command will be executed</param>
        /// <returns>The workflow state of the item (after workflow command has executed, if given one)</returns>
        public ArticleWorkflowState ExecuteCommandAndGetWorkflowState(Item i, string commandID)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                IWorkflow workflow = i.State.GetWorkflow();

                if (workflow == null)
                {
                    //uh oh
                    return(new ArticleWorkflowState());
                }

                if (commandID != null)
                {
                    //var oldWorkflow = workflow.WorkflowID;
                    // This line will cause the workflow field to be set to null... sometimes
                    workflow.Execute(commandID, i, "comments", false);
                    //var info = new WorkflowInfo(oldWorkflow, i.Fields[Sitecore.FieldIDs.WorkflowState].Value);
                    //i.Database.DataManager.SetWorkflowInfo(i, info);
                    //i.Fields[Sitecore.FieldIDs.Workflow].Value = oldWorkflow;
                }
                var state = new ArticleWorkflowState();

                var currentState = workflow.GetState(i);
                state.DisplayName = currentState.DisplayName;
                state.IsFinal     = currentState.FinalState;

                var commands = new List <ArticleWorkflowCommand>();
                foreach (Sitecore.Workflows.WorkflowCommand command in workflow.GetCommands(i))
                {
                    var wfCommand = new PluginModels.ArticleWorkflowCommand();
                    wfCommand.DisplayName = command.DisplayName;
                    wfCommand.StringID    = command.CommandID;

                    ICommand commandItem = _sitecoreMasterService.GetItem <ICommand>(new Guid(command.CommandID));

                    IState stateItem = _sitecoreMasterService.GetItem <IState>(commandItem.Next_State);

                    if (stateItem == null)
                    {
                        //_logger.Warn("WorkflowController.ExecuteCommandAndGetWorkflowState: Next state for command [" + command.CommandID + "] is null!");
                    }
                    else
                    {
                        wfCommand.SendsToFinal     = stateItem.Final;
                        wfCommand.GlobalNotifyList = new List <StaffStruct>();

                        foreach (var x in stateItem.Staffs)
                        {
                            var staffItem = _sitecoreMasterService.GetItem <IStaff_Item>(x._Id);
                            if (staffItem.Inactive)
                            {
                                continue;
                            }

                            var staffMember = new StaffStruct {
                                ID = staffItem._Id
                            };
                            //staffMember.Name = x.GetFullName();
                            //staffMember.Publications = x.Publications.ListItems.Select(p => p.ID.ToGuid()).ToArray();
                            wfCommand.GlobalNotifyList.Add(staffMember);
                        }

                        commands.Add(wfCommand);
                    }
                }

                state.Commands = commands;

                return(state);
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        public void SendNotification(ArticleStruct articleStruct, WorkflowInfo oldWorkflow)
        {
            Logger.SitecoreInfo("EmailUtil.SendNotification");
            if (articleStruct.Publication == Guid.NewGuid())
            {
                return;
            }
            var siteConfigItem = _service.GetItem <ISite_Config>(articleStruct.Publication);

            if (siteConfigItem == null)
            {
                return;
            }
            var fromEmail    = siteConfigItem.From_Email_Address;
            var title        = siteConfigItem.Email_Title;
            var replyToEmail = Sitecore.Context.User.Profile.Email;

            if (string.IsNullOrEmpty(fromEmail) || string.IsNullOrEmpty(replyToEmail))
            {
                return;
            }
            var isAuthorInSenderList = false;

            var notificationList = !articleStruct.ArticleSpecificNotifications.Any() ? new List <StaffStruct>() :
                                   articleStruct.ArticleSpecificNotifications;

            Logger.SitecoreInfo($"EmailUtil.SendNotification: Notification List - {string.Join(",", notificationList.Select(a => GetStaffEmail(a.ID)))}");

            //IIPP-1092
            try
            {
                var stateItem =
                    _service.GetItem <Informa.Models.Informa.Models.sitecore.templates.System.Workflow.ICommand>(
                        articleStruct.CommandID.ToString());
                if (stateItem != null)
                {
                    var workflowitem =
                        _service.GetItem <Informa.Models.Informa.Models.sitecore.templates.System.Workflow.IState>(stateItem.Next_State);
                    var workflowBasednotificationList = workflowitem.Staffs;
                    foreach (var eachUser in workflowBasednotificationList)
                    {
                        try
                        {
                            var toSender = new StaffStruct {
                                ID = eachUser._Id
                            };
                            notificationList.Add(toSender);
                        }
                        catch (Exception ex)
                        {
                            Logger.SitecoreError($"EmailUtil.SendNotification: {ex}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.SitecoreError($"EmailUtil.SendNotification: Failed to find the workflow item. {ex}");
            }

            var emailBody = CreateBody(articleStruct, title, siteConfigItem.Publication_Name, oldWorkflow);

            foreach (var eachEmail in notificationList)
            {
                var staffEmail = GetStaffEmail(eachEmail.ID);
                if (string.IsNullOrEmpty(staffEmail))
                {
                    continue;
                }
                Email email = new Email
                {
                    To         = staffEmail,
                    Subject    = title,
                    From       = fromEmail,
                    Body       = emailBody,
                    IsBodyHtml = true
                };

                Logger.SitecoreInfo($"EmailUtil.SendNotification: notifying - {staffEmail}");

                EmailSender.SendWorkflowNotification(email, replyToEmail);
                if (replyToEmail == staffEmail)
                {
                    isAuthorInSenderList = true;
                }
            }

            if (isAuthorInSenderList)
            {
                return;
            }
            //Emailing the Content Author
            Email contentAuthorEmail = new Email
            {
                To         = replyToEmail,
                Subject    = title,
                From       = fromEmail,
                Body       = emailBody,
                IsBodyHtml = true
            };

            Logger.SitecoreInfo($"EmailUtil.SendNotification: sending author - {contentAuthorEmail}");

            EmailSender.SendWorkflowNotification(contentAuthorEmail, replyToEmail);
        }