| 1: | <?php |
| 2: | namespace TIC\DormBundle\Traits; |
| 3: | |
| 4: | use Doctrine\ORM\EntityRepository; |
| 5: | use Doctrine\ORM\EntityManagerInterface; |
| 6: | use Symfony\Component\HttpFoundation\Request; |
| 7: | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 8: | |
| 9: | use TIC\CoreBundle\Util\ConvertHelper; |
| 10: | use TIC\CoreBundle\Util\ObjectHelper; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | trait ControllerItemable |
| 16: | { |
| 17: | use \TIC\DormBundle\Traits\ManagerTrait; |
| 18: | use \TIC\CoreBundle\Traits\ControllerRouting; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | protected function saveItem(object $item, ?bool $redirect = null, ?string $alert = null): ?RedirectResponse |
| 31: | { |
| 32: | $error = $this->saveEntity($item); |
| 33: | |
| 34: | |
| 35: | if (null === $error) { |
| 36: | $message = ($alert === null) ? \sprintf('alert.%s.save.success', $this->ctxMesg) : $alert; |
| 37: | $this->addFlash('success', $message); |
| 38: | } else { |
| 39: | $message = ($alert === null) ? \sprintf('alert.%s.save.failed', $this->ctxMesg) : $error; |
| 40: | $this->addFlash('danger', $message); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | if ($redirect === false) return null; |
| 45: | return ($redirect) ? $this->redirectItem($item) : $this->redirectReferer(); |
| 46: | } |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | protected function deleteItem(object $item, ?bool $redirect = null, ?string $alert = null): ?RedirectResponse |
| 59: | { |
| 60: | $error = $this->deleteEntity($item); |
| 61: | |
| 62: | |
| 63: | if (null === $error) { |
| 64: | $message = ($alert === null) ? \sprintf('alert.%s.delete.success', $this->ctxMesg) : $alert; |
| 65: | $this->addFlash('success', $message); |
| 66: | } else { |
| 67: | $message = ($alert === null) ? \sprintf('alert.%s.delete.failed', $this->ctxMesg) : $error; |
| 68: | $this->addFlash('danger', $message); |
| 69: | } |
| 70: | |
| 71: | |
| 72: | if ($redirect === false) return null; |
| 73: | if (null === $error) return $this->redirectList(); |
| 74: | return ($redirect) ? $this->redirectItem($item) : $this->redirectReferer(); |
| 75: | } |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | protected function harddeleteItem(object $item, ?bool $redirect = null, ?string $alert = null): ?RedirectResponse |
| 88: | { |
| 89: | $error = $this->harddeleteEntity($item); |
| 90: | |
| 91: | |
| 92: | if (null === $error) { |
| 93: | $message = ($alert === null) ? \sprintf('alert.%s.delete.success', $this->ctxMesg) : $alert; |
| 94: | $this->addFlash('success', $message); |
| 95: | } else { |
| 96: | $message = ($alert === null) ? \sprintf('alert.%s.delete.failed', $this->ctxMesg) : $error; |
| 97: | $this->addFlash('danger', $message); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | if ($redirect === false) return null; |
| 102: | if (null === $error) return $this->redirectList(); |
| 103: | return ($redirect) ? $this->redirectItem($item) : $this->redirectReferer(); |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | protected function changeItem(object $item, string $field, mixed $value, ?bool $redirect = null, ?bool $no_alert = false): ?RedirectResponse |
| 117: | { |
| 118: | |
| 119: | try { |
| 120: | $change = ObjectHelper::changeField($item, $field, $value); |
| 121: | } catch (\Exception $e) { |
| 122: | return $this->alert('danger', ['title'=>"error.item.no_property", 'text'=>$e->getMessage()], false); |
| 123: | } |
| 124: | |
| 125: | |
| 126: | if ($change) $error = $this->saveEntity($item); |
| 127: | |
| 128: | |
| 129: | if (! $no_alert) { |
| 130: | if (! $change) |
| 131: | $this->addFlash('warning', \sprintf('alert.%s.change.already', $this->ctxMesg)); |
| 132: | elseif ($error === null) |
| 133: | $this->addFlash('success', \sprintf('alert.%s.change.success', $this->ctxMesg)); |
| 134: | else |
| 135: | $this->addFlash('danger', ['title'=>\sprintf('alert.%s.change.failed', $this->ctxMesg), 'text'=>$error]); |
| 136: | } |
| 137: | |
| 138: | |
| 139: | if ($redirect === false) return null; |
| 140: | return ($redirect) ? $this->redirectItem($item) : $this->redirectReferer(); |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | protected function toggleItem(object $item, string $field, mixed $value, ?bool $redirect = null, ?bool $no_alert = false): ?RedirectResponse |
| 154: | { |
| 155: | |
| 156: | try { |
| 157: | $value = ConvertHelper::boolean($value); |
| 158: | $state = ObjectHelper::toggleField($item, $field, $value); |
| 159: | } catch (\Exception $e) { |
| 160: | return $this->alert('danger', ['title'=>"error.item.no_property", 'text'=>$e->getMessage()], false); |
| 161: | } |
| 162: | |
| 163: | |
| 164: | if ($value === null) $value = $state; |
| 165: | if ($state !== null) $error = $this->saveEntity($item); |
| 166: | |
| 167: | |
| 168: | if (! $no_alert) { |
| 169: | $action = ($value) ? "enable" : "disable"; |
| 170: | if ($state === null) |
| 171: | $this->addFlash('warning', \sprintf('alert.%s.%s.already', $this->ctxMesg, $action)); |
| 172: | elseif ($error === null) |
| 173: | $this->addFlash('success', \sprintf('alert.%s.%s.success', $this->ctxMesg, $action)); |
| 174: | else |
| 175: | $this->addFlash('danger', ['title'=>\sprintf('alert.%s.%s.failed', $this->ctxMesg, $action), 'text'=>$error]); |
| 176: | } |
| 177: | |
| 178: | |
| 179: | if ($redirect === false) return null; |
| 180: | return ($redirect) ? $this->redirectItem($item) : $this->redirectReferer(); |
| 181: | } |
| 182: | |
| 183: | } |
| 184: | |