Posted By

stolkchris on 08/31/10


Tagged

php controller cakephp model dynamic quick app models actions


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

rascoop


CakePHP - Quick record actions


 / Published in: PHP
 

This snippet can be called in every controller, all you need to do is to specify the form name, the model name and the data.

  1. function quick_actions($form, $model, $data) {
  2. $action = $this->data[$form]['actions'];
  3. App::Import('Model', $model);
  4. $this->DynamicModel = new $form;
  5. if ($action == 'delete') {
  6. foreach ($this->data[$form]['id'] as $key => $value) {
  7. if ($value != 0) {
  8. $this->DynamicModel->delete($key);
  9. $this->Session->setFlash('Items permanent verwijderd', 'notifications/tray_top');
  10. $return = true;
  11. }
  12. }
  13. }
  14. if ($action == 'return') {
  15. foreach ($this->data[$form]['id'] as $key => $value) {
  16. if ($value != 0) {
  17. if ($this->DynamicModel->updateAll(array($form.'.deleted'=>'0'),array($form.'.id'=>$key))) {
  18. $this->Session->setFlash('Items teruggezet', 'notifications/tray_top');
  19. $return = true;
  20. }
  21. }
  22. }
  23. }
  24. if ($action == 'remove') {
  25. foreach ($this->data[$form]['id'] as $key => $value) {
  26. if ($value != 0) {
  27. if ($this->DynamicModel->updateAll(array($form.'.deleted'=>'1'),array($form.'.id'=>$key))) {
  28. $this->Session->setFlash('Items verwijderd naar prullenbak', 'notifications/tray_top');
  29. $return = true;
  30. }
  31. }
  32. }
  33. }
  34. if ($action == 'publish') {
  35. foreach ($this->data[$form]['id'] as $key => $value) {
  36. if ($value != 0) {
  37. if ($this->DynamicModel->updateAll(array($form.'.published'=>'1'),array($form.'.id'=>$key))) {
  38. $this->Session->setFlash('Items gepubliceerd', 'notifications/tray_top');
  39. $return = true;
  40. }
  41. }
  42. }
  43. }
  44. if ($action == 'unpublish') {
  45. foreach ($this->data[$form]['id'] as $key => $value) {
  46. if ($value != 0) {
  47. if($this->DynamicModel->updateAll(array($form.'.published'=>'0'),array($form.'.id'=>$key))){
  48. $this->Session->setFlash('Items geweigerd', 'notifications/tray_top');
  49. $return = true;
  50. }
  51. }
  52. }
  53. }
  54. if (isset($return)) {
  55. $this->redirect($this->referer());
  56. } elseif (!isset($return)) {
  57. $this->Session->setFlash('Er is een fout opgetreden, probeer opnieuw','notifications/error');
  58. $this->redirect($this->referer());
  59. }
  60. }

Report this snippet  

You need to login to post a comment.