/**
  * This returns the workflow requests outstanding for this user.
  * It does one query against draft for change requests, and another
  * request against live for the deletion requests (which are not in draft
  * any more), and merges the result sets together.
  */
 function sourceRecords($params)
 {
     increase_time_limit_to(120);
     $currentStage = Versioned::current_stage();
     $changes = WorkflowTwoStepRequest::get_by_author('WorkflowPublicationRequest', Member::currentUser(), array('AwaitingApproval'));
     if ($changes) {
         foreach ($changes as $change) {
             $change->RequestType = "Publish";
         }
     }
     Versioned::reading_stage(Versioned::get_live_stage());
     $deletions = WorkflowTwoStepRequest::get_by_author('WorkflowDeletionRequest', Member::currentUser(), array('AwaitingApproval'));
     if ($deletions) {
         foreach ($deletions as $deletion) {
             $deletion->RequestType = "Deletion";
         }
     }
     if ($changes && $deletions) {
         $changes->merge($deletions);
     } else {
         if ($deletions) {
             $changes = $deletions;
         }
     }
     return $changes;
 }
 function sourceRecords($params, $sort, $limit)
 {
     increase_time_limit_to(120);
     $res = WorkflowTwoStepRequest::get_by_publisher('WorkflowDeletionRequest', Member::currentUser(), array('AwaitingApproval'));
     $doSet = new DataObjectSet();
     if ($res) {
         foreach ($res as $result) {
             if (!$result->canApprove()) {
                 continue;
             }
             if ($wf = $result->openWorkflowRequest()) {
                 $result->WFAuthorTitle = $wf->Author()->Title;
                 $result->WFAuthorID = $wf->AuthorID;
                 $result->WFRequestedWhen = $wf->Created;
                 $result->WFApproverID = $wf->ApproverID;
                 $result->WFPublisherID = $wf->PublisherID;
                 $result->BacklinkCount = $result->BackLinkTracking()->Count();
                 $doSet->push($result);
             }
         }
     }
     if ($sort) {
         $parts = explode(' ', $sort);
         $field = $parts[0];
         $direction = $parts[1];
         if ($field == 'AbsoluteLink') {
             $sort = 'URLSegment ' . $direction;
         }
         if ($field == 'Subsite.Title') {
             $sort = 'SubsiteID ' . $direction;
         }
         $doSet->sort($sort);
     }
     if ($limit && $limit['limit']) {
         return $doSet->getRange($limit['start'], $limit['limit']);
     } else {
         return $doSet;
     }
 }
 function sourceRecords($params, $sort, $limit)
 {
     increase_time_limit_to(120);
     $res = WorkflowTwoStepRequest::get_by_publisher('WorkflowPublicationRequest', Member::currentUser(), array('AwaitingApproval'));
     $doSet = new DataObjectSet();
     if ($res) {
         foreach ($res as $result) {
             if (!$result->canPublish()) {
                 continue;
             }
             if ($wf = $result->openWorkflowRequest()) {
                 if (ClassInfo::exists('Subsite')) {
                     $result->SubsiteTitle = $result->Subsite()->Title;
                 }
                 $result->RequestedAt = $wf->Created;
                 $result->WFAuthorTitle = $wf->Author()->Title;
                 $result->HasEmbargo = $wf->getEmbargoDate() ? date('j M Y g:ia', strtotime($wf->getEmbargoDate())) : 'no';
                 $doSet->push($result);
             }
         }
     }
     if ($sort) {
         $parts = explode(' ', $sort);
         $field = $parts[0];
         $direction = $parts[1];
         if ($field == 'AbsoluteLink') {
             $sort = 'URLSegment ' . $direction;
         }
         if ($field == 'Subsite.Title') {
             $sort = 'SubsiteID ' . $direction;
         }
         $doSet->sort($sort);
     }
     if ($limit && $limit['limit']) {
         return $doSet->getRange($limit['start'], $limit['limit']);
     } else {
         return $doSet;
     }
 }
 function testWorkflowPublicationApprovalTransition()
 {
     WorkflowRequest::$enable_all_alerts = true;
     $page = $this->objFromFixture('SiteTree', 'custompublisherpage');
     $custompublisher = $this->objFromFixture('Member', 'custompublisher');
     $customauthor = $this->objFromFixture('Member', 'customauthor');
     // awaiting approval
     $this->session()->inst_set('loggedInAs', $customauthor->ID);
     $request1 = $page->openOrNewWorkflowRequest('WorkflowPublicationRequest');
     $this->assertNotNull($request1);
     $this->assertEquals($request1->AuthorID, $customauthor->ID, "Logged-in member is set as the author of the request");
     $this->assertEquals($request1->Status, 'AwaitingApproval', "Request is set to AwaitingApproval after requestPublication() is called");
     $this->assertContains('cms_cancel', array_flip($request1->WorkflowActions()), "Author can cancel own request");
     $this->assertNotContains('cms_deny', array_flip($request1->WorkflowActions()), "Author cant deny own request");
     $this->session()->inst_set('loggedInAs', $custompublisher->ID);
     $this->assertContains('cms_cancel', array_flip($request1->WorkflowActions()), "Publisher can cancel requests");
     $request1->requestedit('Please make some changes');
     $request1->comment('Here are the changes I would like you to do:');
     $this->session()->inst_set('loggedInAs', $customauthor->ID);
     $this->assertContains('cms_requestpublication', array_flip($request1->WorkflowActions()), "Author can resubmit after request edit");
     $request1->request('Resubmit after edits');
     $this->session()->inst_set('loggedInAs', $custompublisher->ID);
     WorkflowRequest::$allow_deny = true;
     $this->assertContains('cms_deny', array_flip($request1->WorkflowActions()), "Publisher can deny requests when WorkflowRequest::allow_deny is true");
     WorkflowRequest::$allow_deny = false;
     $this->assertNotContains('cms_deny', array_flip($request1->WorkflowActions()), "Publisher can't deny requests when WorkflowRequest::allow_deny is false");
     WorkflowRequest::$allow_deny = true;
     $request1->approve('Looks good');
     $this->assertEquals($request1->Status, 'Completed', "Request is set to Completed after page is approved");
     $this->assertEquals($request1->PublisherID, $custompublisher->ID, "Currently logged-in user is set as the Publisher for this request");
     $this->session()->inst_set('loggedInAs', null);
     // Make sure the requests can be picked up by the get by author and get by publisher functions
     $this->assertContains($request1->Page()->ID, WorkflowTwoStepRequest::get_by_publisher('WorkflowPublicationRequest', $custompublisher, array('Completed'))->column('ID'));
     $this->assertContains($request1->Page()->ID, WorkflowTwoStepRequest::get_by_author('WorkflowPublicationRequest', $customauthor, array('Completed'))->column('ID'));
     WorkflowRequest::$enable_all_alerts = false;
 }