Skip to content

Commit

Permalink
convert Workflow_SubmissionModel into element type
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jun 26, 2016
1 parent abfc32a commit 00ecab7
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 52 deletions.
7 changes: 4 additions & 3 deletions workflow/WorkflowPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function getSettingsUrl()
public function registerCpRoutes()
{
return array(
'workflow' => array('action' => 'workflow/index'),
'workflow/settings' => array('action' => 'workflow/settings'),
);
}
Expand Down Expand Up @@ -88,8 +87,10 @@ public function onBeforeInstall()

public function init()
{
// Add template to the sidebar for maximum trendyness
craft()->workflow->renderEntrySidebar();
if (craft()->request->isCpRequest()) {
// Add template to the sidebar for maximum trendyness
craft()->workflow->renderEntrySidebar();
}
}

public function registerEmailMessages()
Expand Down
14 changes: 3 additions & 11 deletions workflow/controllers/WorkflowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ class WorkflowController extends BaseController
// Control Panel
//

public function actionIndex()
{
$submissions = craft()->workflow_submissions->getAll();

$this->renderTemplate('workflow/index', array(
'submissions' => $submissions,
));
}

public function actionSettings()
{
$settings = craft()->workflow->getSettings();
Expand All @@ -36,9 +27,10 @@ public function actionSendForSubmission()
$user = craft()->userSession->getUser();

$model = new Workflow_SubmissionModel();
$model->elementId = craft()->request->getParam('entryId');
$model->ownerId = craft()->request->getParam('entryId');
$model->draftId = craft()->request->getParam('draftId');
$model->editorId = $user->id;
$model->status = Workflow_SubmissionModel::PENDING;
$model->dateApproved = null;

if (craft()->workflow_submissions->save($model)) {
Expand All @@ -57,7 +49,7 @@ public function actionApproveSubmission()

$submissionId = craft()->request->getParam('submissionId');
$model = craft()->workflow_submissions->getById($submissionId);
$model->approved = true;
$model->status = Workflow_SubmissionModel::APPROVED;
$model->publisherId = $user->id;
$model->dateApproved = new DateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Craft;

class Workflow_SubmissionsStatusElementAction extends BaseElementAction
{
// Public Methods
// =========================================================================

public function getTriggerHtml()
{
return craft()->templates->render('workflow/_elementactions/status');
}

public function performAction(ElementCriteriaModel $criteria)
{
$status = $this->getParams()->status;

// Figure out which element IDs we need to update
$elementIds = $criteria->ids();

// Update their statuses
craft()->db->createCommand()->update(
'workflow_submissions',
array('status' => $status),
array('in', 'id', $elementIds)
);

// Clear their template caches
craft()->templateCache->deleteCachesByElementId($elementIds);

// Fire an 'onSetStatus' event
$this->onSetStatus(new Event($this, array(
'criteria' => $criteria,
'elementIds' => $elementIds,
'status' => $status,
)));

$this->setMessage(Craft::t('Statuses updated.'));

return true;
}

public function onSetStatus(Event $event)
{
$this->raiseEvent('onSetStatus', $event);
}


// Protected Methods
// =========================================================================

protected function defineParams()
{
return array(
'status' => array(AttributeType::Enum, 'values' => array(
Workflow_SubmissionsModel::APPROVED,
Workflow_SubmissionsModel::PENDING,
), 'required' => true)
);
}
}
160 changes: 160 additions & 0 deletions workflow/elementtypes/Workflow_SubmissionElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
namespace Craft;

class Workflow_SubmissionElementType extends BaseElementType
{
// Public Methods
// =========================================================================

public function getName()
{
return Craft::t('Workflow Submission');
}

public function hasContent()
{
return false;
}

public function hasTitles()
{
return false;
}

public function hasStatuses()
{
return true;
}

public function getStatuses()
{
return array(
Workflow_SubmissionModel::APPROVED => Craft::t('Approved'),
Workflow_SubmissionModel::PENDING => Craft::t('Pending'),
);
}

public function getSources($context = null)
{
$settings = craft()->workflow->getSettings();

$sources = array(
'*' => array(
'label' => Craft::t('All Submissions'),
),
);

$submissions = craft()->workflow_submissions->getAll();

foreach ($submissions as $submission) {
$elementType = craft()->elements->getElementType($submission->owner->elementType);
$key = 'elements:'.$elementType->classHandle;

$sources[$key] = array('heading' => $elementType->name);

$sources[$key.':all'] = array(
'label' => Craft::t('All ' . $elementType->name),
'criteria' => array('elementType' => $submission->owner->elementType),
);
}

return $sources;
}

public function populateElementModel($row)
{
return Workflow_SubmissionModel::populateModel($row);
}

public function defineTableAttributes($source = null)
{
return array(
'id' => Craft::t('Entry'),
'editor' => Craft::t('Editor'),
'dateCreated' => Craft::t('Date Submitted'),
'publisher' => Craft::t('Publisher'),
'dateApproved' => Craft::t('Date Approved'),
);
}

public function defineSortableAttributes()
{
return array(
'id' => Craft::t('Entry'),
'editor' => Craft::t('Editor'),
'dateCreated' => Craft::t('Date Submitted'),
'publisher' => Craft::t('Publisher'),
'dateApproved' => Craft::t('Date Approved'),
);
}

public function getTableAttributeHtml(BaseElementModel $element, $attribute)
{
switch ($attribute) {
case 'publisher':
case 'editor': {
if ($element->$attribute) {
return "<a href='" . $element->$attribute->cpEditUrl . "'>" . $element->$attribute . "</a>";
}
}
default: {
return parent::getTableAttributeHtml($element, $attribute);
}
}
}

public function defineCriteriaAttributes()
{
return array(
'ownerId' => array(AttributeType::Number),
'draftId' => array(AttributeType::Number),
'editorId' => array(AttributeType::Number),
'publisherId' => array(AttributeType::Number),
'status' => array(AttributeType::String, 'default' => Workflow_SubmissionModel::PENDING),
'order' => array(AttributeType::String, 'default' => 'dateCreated asc'),
);
}

public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
{
$query
->addSelect('
workflow_submissions.ownerId,
workflow_submissions.draftId,
workflow_submissions.editorId,
workflow_submissions.publisherId,
workflow_submissions.status
')
->join('workflow_submissions workflow_submissions', 'workflow_submissions.id = elements.id');

if ($criteria->ownerId) {
$query->andWhere(DbHelper::parseParam('workflow_submissions.ownerId', $criteria->ownerId, $query->params));
}

if ($criteria->draftId) {
$query->andWhere(DbHelper::parseParam('workflow_submissions.draftId', $criteria->draftId, $query->params));
}

if ($criteria->editorId) {
$query->andWhere(DbHelper::parseParam('workflow_submissions.editorId', $criteria->editorId, $query->params));
}

if ($criteria->publisherId) {
$query->andWhere(DbHelper::parseParam('workflow_submissions.publisherId', $criteria->publisherId, $query->params));
}

if ($criteria->status) {
$query->andWhere(DbHelper::parseParam('workflow_submissions.status', $criteria->status, $query->params));
}

if ($criteria->dateCreated) {
$query->andWhere(DbHelper::parseDateParam('workflow_submissions.dateCreated', $criteria->dateCreated, $query->params));
}
}

public function getAvailableActions($source = null)
{
return array('Workflow_SubmissionsStatus');
}

}
47 changes: 39 additions & 8 deletions workflow/models/Workflow_SubmissionModel.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
<?php
namespace Craft;

class Workflow_SubmissionModel extends BaseModel
class Workflow_SubmissionModel extends BaseElementModel
{
// Properties
// =========================================================================

protected $elementType = 'Workflow_Submission';

const APPROVED = 'approved';
const PENDING = 'pending';


// Public Methods
// =========================================================================

public function getElement()
public function __toString()
{
if ($this->ownerId) {
return $this->owner->title;
} else {
return '';
}
}

public function getStatus()
{
return $this->status;
}

public function getCpEditUrl()
{
return UrlHelper::getCpUrl('workflow/edit/' . $this->id);
}

public function getOwner()
{
if ($this->elementId) {
return craft()->entries->getEntryById($this->elementId);
if ($this->ownerId) {
return craft()->entries->getEntryById($this->ownerId);
}
}

Expand All @@ -33,17 +61,20 @@ public function getPublisher()

protected function defineAttributes()
{
return array(
return array_merge(parent::defineAttributes(), array(
'id' => array(AttributeType::Number),
'elementId' => array(AttributeType::Number),
'ownerId' => array(AttributeType::Number),
'draftId' => array(AttributeType::Number),
'editorId' => array(AttributeType::Number),
'publisherId' => array(AttributeType::Number),
'approved' => array(AttributeType::Bool),
'status' => array(AttributeType::Enum, 'values' => array(
Workflow_SubmissionModel::APPROVED,
Workflow_SubmissionModel::PENDING,
)),
'dateApproved' => array(AttributeType::DateTime),
'dateCreated' => array(AttributeType::DateTime),
'dateUpdated' => array(AttributeType::DateTime),
);
));
}

}
14 changes: 12 additions & 2 deletions workflow/records/Workflow_SubmissionRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ public function getTableName()
public function defineRelations()
{
return array(
'element' => array(static::BELONGS_TO, 'ElementRecord', 'required' => true, 'onDelete' => static::CASCADE),
'owner' => array(static::BELONGS_TO, 'ElementRecord', 'required' => true, 'onDelete' => static::CASCADE),
'draft' => array(static::BELONGS_TO, 'EntryDraftRecord', 'required' => false, 'onDelete' => static::CASCADE),
'editor' => array(static::BELONGS_TO, 'UserRecord', 'required' => false, 'onDelete' => static::CASCADE),
'publisher' => array(static::BELONGS_TO, 'UserRecord', 'required' => false, 'onDelete' => static::CASCADE),
);
}

public function scopes()
{
return array(
'ordered' => array('order' => 'dateCreated'),
);
}


// Protected Methods
// =========================================================================

protected function defineAttributes()
{
return array(
'approved' => array(AttributeType::Bool),
'status' => array(AttributeType::Enum, 'values' => array(
Workflow_SubmissionModel::APPROVED,
Workflow_SubmissionModel::PENDING,
)),
'dateApproved' => array(AttributeType::DateTime),
);
}
Expand Down
Loading

0 comments on commit 00ecab7

Please sign in to comment.