Skip to content

Instantly share code, notes, and snippets.

@cesarockstar1985
Created May 10, 2023 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cesarockstar1985/67ebf74d8edabde0a2d2bd77148d7124 to your computer and use it in GitHub Desktop.
Save cesarockstar1985/67ebf74d8edabde0a2d2bd77148d7124 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Event\EventInterface;
use CakeDC\Todo\Controller\TodosController;
/**
* Applications Controller
*
* @property \App\Model\Table\BondsTable $Bonds
* @property \App\Model\Table\BondTypesTable $BondTypes
* @property \SuretyApi\Controller\Component\TaskStorageComponent $TaskStorage
*/
class AppTodosController extends TodosController
{
/**
* initialize
*
* @return void
*/
public function initialize(): void
{
parent::initialize();
$this->Todos = $this->fetchTable('CakeDC.Todos');
$this->FollowUps = $this->fetchTable('FollowUps');
}
/**
* beforeFilter
*
* @param mixed $event event
* @return void
*/
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$this->FormProtection->setConfig('unlockedActions', [
'disableFollowUp',
]);
}
/**
* disableFollowUpAjax
*
* @param mixed $followUpId follow up id
* @return void
*/
public function disableFollowUpAjax($followUpId)
{
$this->disableFollowUp($followUpId);
if (!$this->getRequest()->is('ajax')) {
return $this->redirect($this->getRequest()->referer(true));
}
}
/**
* disableFollowUp
*
* @param mixed $followUpId
* @param mixed $fromController if request is sent from controller
* @return void
*/
private function disableFollowUp($followUpId, $fromController = false)
{
if ($this->getRequest()->is('post') || $fromController) {
$jsonResponse = ['success' => 'true'];
$error = false;
$followUp = $this->FollowUps->find()->where(['id' => $followUpId])->first();
if (!$followUp || $followUp->enabled == 0) {
$error = true;
} else {
$followUp->enabled = 0;
}
if ($error || !$this->FollowUps->save($followUp)) {
$jsonResponse['success'] = 'false';
} else {
$jsonResponse['followUp'] = $followUp;
}
$this->set('jsonResponse', $jsonResponse);
$this->viewBuilder()->setOption('serialize', ['jsonResponse']);
}
}
/**
* edit
*
* @param mixed $id todo id
* @return void
*/
public function edit($id = null)
{
$this->disableFollowUp($id, true);
}
/**
* add
*
* @param mixed $model todo model
* @param mixed $foreignKey foreign key
* @return void
*/
public function add(string $model, string $foreignKey)
{
$this->disableFollowUp($foreignKey, true);
}
/**
* delete
*
* @param mixed $id todo id
* @return void
*/
public function delete($id = null)
{
$this->disableFollowUp($id, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment