3

I have a custom element type with its own table 'pluginname_entries'. I am trying to find a way to "dummy proof" changing the plugins status. So I came up, aka stole from the Events element type, with the idea of clicking a button that triggers a controller action - similar to the following example to delete my element types entry:

actionDelete()

/**
 * Deletes an entry.
 */
public function actionDelete()
{
    $this->requirePostRequest();

    $entryId = craft()->request->getRequiredPost('entryId');

    if (craft()->elements->deleteElementById($entryId))
    {
        craft()->userSession->setNotice(Craft::t('Entry deleted.'));
        $this->redirectToPostedUrl();
    }
    else
    {
        craft()->userSession->setError(Craft::t('Couldn’t delete entry.'));
    }
}

I love the concept or a quick way to delete or update the entry, more specifically the entry custom status, with a simple click of a button. So I came up with this:

actionDeny()

/**
 * Changes an entry to denied.
 */
public function actionDeny()
{
    $this->requirePostRequest();

    $status = PluginStatus::Denied;

    $entryId = craft()->request->getRequiredPost('entryId');

    $query = craft()->db->createCommand()
        ->select('id, status')
        ->from('entries')
        ->where(array(
            'id' => $entryId
        ))->queryRow();

    if ($query == null) {
        craft()->userSession->setError(Craft::t('Unable to deny the entry.'));
    }
    else {
        craft()->userSession->setNotice(Craft::t('The entry was denied.'));
    }

}

That works as intended, notifying the user but of course doesn't actually update the status column to the new value, is there a better way to handle this? Maybe more of a "Craft" way?

5

You are currently performing a SELECTinstead of an UPDATE so no data will be updated.

I'm guessing that entryId is the primary key of the record you are trying to update? If so, I would suggest you find the record to make sure it exists, update its attributes (status) and then save it.

// PluginController
public function actionDeny()
{
    $this->requirePostRequest();

    $status  = PluginStatus::Denied;
    $entryId = craft()->request->getRequiredPost('entryId');

    if (craft()->pluginName->updateStatus($entryId, $status)
    {
        craft()->userSession->setError(Craft::t('Unable to deny the entry.'));
    }
    else
    {
        craft()->userSession->setNotice(Craft::t('The entry was denied.'));
    }
}

// PluginService
public function updateStatus($id, $status)
{
    $record = PluginRecord::model()->findById($id);

    if ($record)
    {
        $record->setAttribute('status', $status);

        return $record->save();
    }
    else
    {
        throw new Exception(Craft:t('No record was found with id {id}', array('id' => $id));
    }
}

Unless I'm missing something, that should be about all you need.

@selvinortiz

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.