src/AdminBundle/Entity/Settings.php line 259

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Eventviva\ImageResize;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. /**
  7. * Settings
  8. */
  9. #[ORM\Table(name: '`settings`')]
  10. #[ORM\UniqueConstraint(name: 'key_type', columns: ['key', 'type'])]
  11. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\SettingsRepository::class)]
  12. #[ORM\HasLifecycleCallbacks]
  13. class Settings extends BaseEntity
  14. {
  15. const CURRENCY_SYMBOL = [
  16. 'GBP' => '£',
  17. 'EUR' => '€',
  18. 'USD' => '$',
  19. ];
  20. const TYPE_FINANCIAL = 1;
  21. const TYPE_AREA = 2;
  22. const TYPE_CAR = 3;
  23. const TYPE_BOOKING = 4;
  24. const TYPE_ACCOUNTS = 5;
  25. const TYPE_API_DRIVER = 6;
  26. const TYPE_API_CLIENT = 7;
  27. const TYPE_GLOBAL = 9;
  28. const TYPE_JOBS_PLAN = 10;
  29. public static $typeList = [
  30. self::TYPE_FINANCIAL => 'Financial',
  31. self::TYPE_AREA => 'Area',
  32. self::TYPE_BOOKING => 'Booking',
  33. self::TYPE_ACCOUNTS => 'Accounts',
  34. self::TYPE_API_DRIVER => 'API Driver',
  35. self::TYPE_API_CLIENT => 'API Client',
  36. self::TYPE_GLOBAL => 'Global',
  37. self::TYPE_JOBS_PLAN => 'Jobs Plan',
  38. ];
  39. public const KEY_BOOKING_DISTANCE_PERCENTAGE_CHARGE = 'booking_distance_percentage_charge';
  40. public const KEY_BOOKING_TIME_EXTRA_CHARGE = 'automatic_extra_charge_min_time';
  41. public const KEY_CLIENT_ACTIVE_EXTRA_CHARGE_EXTRA_DISTANCE = 'client_active_extra_charge_extra_distance';
  42. public const KEY_CLIENT_ACTIVE_EXTRA_CHARGE_NOTIFICATION_AMOUNT = 'client_active_extra_charge_notification_amount';
  43. const FILE_FOLDER = 'upload/settings/';
  44. public const VALUE_CALCULATE_QUOTE_TYPE_OVER_ALL = 'over all';
  45. public const VALUE_CALCULATE_QUOTE_TYPE_ADDED = 'added';
  46. /**
  47. * @var integer
  48. */
  49. #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
  50. #[ORM\Id]
  51. #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  52. protected $id;
  53. /**
  54. * @var string
  55. */
  56. #[ORM\Column(name: '`key`', type: 'string', length: 255, nullable: false)]
  57. private $key;
  58. /**
  59. * @var string
  60. */
  61. #[ORM\Column(name: '`value`', type: 'text', nullable: true)]
  62. private $value;
  63. /**
  64. * @var string
  65. */
  66. #[ORM\Column(name: 'description', type: 'text', nullable: true)]
  67. private $description;
  68. /**
  69. * @var integer
  70. */
  71. #[ORM\Column(name: '`type`', type: 'smallint', length: 20, nullable: false)]
  72. private $type;
  73. /**
  74. * @var integer
  75. */
  76. #[ORM\Column(name: 'tags', type: 'string', length: 255, nullable: true)]
  77. private $tags;
  78. /**
  79. * @var string
  80. */
  81. #[ORM\Column(name: 'end_date', type: 'datetime', nullable: true)]
  82. private $endDate;
  83. /**
  84. * @var string
  85. */
  86. #[ORM\Column(name: 'start_date', type: 'datetime', nullable: true)]
  87. private $startDate;
  88. /**
  89. * @var string
  90. */
  91. #[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
  92. private $picture;
  93. private $pictureFile;
  94. public $symbol;
  95. public function __construct($key = null, $value = null, $type = null)
  96. {
  97. if ($key) {
  98. $this->setKey($key);
  99. }
  100. if ($value) {
  101. $this->setValue($value);
  102. }
  103. if ($type) {
  104. $this->setType($type);
  105. }
  106. }
  107. /**
  108. * @return int
  109. */
  110. public function getId()
  111. {
  112. return $this->id;
  113. }
  114. /**
  115. * @param int $id
  116. */
  117. public function setId($id)
  118. {
  119. $this->id = $id;
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getKey()
  125. {
  126. return $this->key;
  127. }
  128. /**
  129. * @param string $key
  130. * @return Settings
  131. */
  132. public function setKey(string $key): Settings
  133. {
  134. $this->key = $key;
  135. return $this;
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getValue()
  141. {
  142. return $this->value;
  143. }
  144. /**
  145. * @param string $value
  146. * @return Settings
  147. */
  148. public function setValue($value): Settings
  149. {
  150. $this->value = $value;
  151. return $this;
  152. }
  153. /**
  154. * @return int
  155. */
  156. public function getType()
  157. {
  158. return $this->type;
  159. }
  160. /**
  161. * @param int $type
  162. * @return Settings
  163. */
  164. public function setType(int $type): Settings
  165. {
  166. $this->type = $type;
  167. return $this;
  168. }
  169. public function __toString()
  170. {
  171. return $this->getId() ? $this->getId() . '' : 'n\a';
  172. }
  173. /**
  174. * @return string
  175. */
  176. public function getStartDate()
  177. {
  178. return $this->startDate;
  179. }
  180. /**
  181. * @param string $startDate
  182. */
  183. public function setStartDate($startDate)
  184. {
  185. $this->startDate = $startDate;
  186. }
  187. /**
  188. * @return string
  189. */
  190. public function getEndDate()
  191. {
  192. return $this->endDate;
  193. }
  194. /**
  195. * @param string $endDate
  196. */
  197. public function setEndDate($endDate)
  198. {
  199. $this->endDate = $endDate;
  200. }
  201. /**
  202. * Set picture
  203. *
  204. * @param string $picture
  205. *
  206. * @return Settings
  207. */
  208. public function setPicture($picture)
  209. {
  210. $this->picture = $picture;
  211. return $this;
  212. }
  213. public function getPicture()
  214. {
  215. return $this->picture;
  216. }
  217. /**
  218. * @param UploadedFile $picture
  219. */
  220. public function setPictureFile(UploadedFile $pictureFile = null)
  221. {
  222. $this->pictureFile = $pictureFile;
  223. }
  224. /**
  225. * @return UploadedFile
  226. */
  227. public function getPictureFile()
  228. {
  229. return $this->pictureFile;
  230. }
  231. public function getPicturePath()
  232. {
  233. if (!$this->picture) {
  234. return;
  235. }
  236. return $this->getUploadFilePath() . $this->picture;
  237. }
  238. public function uploadPicture()
  239. {
  240. if (null === $this->getPictureFile()) {
  241. return;
  242. }
  243. $this->removePictureFile();
  244. $filename = sha1(uniqid(mt_rand(), true));
  245. $this->picture = $filename . '.' . $this->getPictureFile()->guessExtension();
  246. header("Content-type: image/jpeg");
  247. $image = new ImageResize($this->getPictureFile());
  248. $image->resizeToWidth(1980);
  249. $image->save($this->getUploadFilePath() . '/' . $this->picture);
  250. }
  251. public function getUploadFilePath()
  252. {
  253. if (!is_dir(self::FILE_FOLDER)) {
  254. mkdir(self::FILE_FOLDER, 0777, true);
  255. }
  256. return self::FILE_FOLDER;
  257. }
  258. public function removePictureFile()
  259. {
  260. if ($pictureFile = $this->getPicturePath()) {
  261. @unlink($pictureFile);
  262. }
  263. }
  264. public function getImageSizes()
  265. {
  266. if (!$this->picture) {
  267. return;
  268. }
  269. $sizes = getimagesize($this->getUploadFilePath() . $this->picture);
  270. $res = [];
  271. if (!empty($sizes)) {
  272. $res['width'] = $sizes[0];
  273. $res['height'] = $sizes[1];
  274. }
  275. return $res;
  276. }
  277. #[ORM\PreUpdate]
  278. #[ORM\PrePersist]
  279. public function lifecycleFileUpload()
  280. {
  281. $this->uploadPicture();
  282. }
  283. #[ORM\PostRemove]
  284. public function removeUpload()
  285. {
  286. $this->removePictureFile();
  287. }
  288. /**
  289. * @return array
  290. */
  291. public function getTags()
  292. {
  293. if (strpos($this->tags, ',') !== false) {
  294. return explode(',', $this->tags);
  295. } else {
  296. return [$this->tags];
  297. }
  298. }
  299. /**
  300. * @param array $tags
  301. */
  302. public function setTags($tags)
  303. {
  304. $this->tags = implode(',', $tags);
  305. }
  306. /**
  307. * @return string
  308. */
  309. public function getDescription()
  310. {
  311. return $this->description;
  312. }
  313. /**
  314. * @param string $description
  315. */
  316. public function setDescription($description)
  317. {
  318. $this->description = $description;
  319. }
  320. }