'fas fa-tasks', 'admin' => 'fas fa-wrench', 'calendar' => 'far fa-calendar-alt', 'customer' => 'fas fa-users', 'copy' => 'far fa-copy', 'create' => 'far fa-plus-square', 'dashboard' => 'fas fa-tachometer-alt', 'delete' => 'far fa-trash-alt', 'download' => 'fas fa-download', 'duration' => 'far fa-hourglass', 'edit' => 'far fa-edit', 'filter' => 'fas fa-filter', 'help' => 'far fa-question-circle', 'invoice' => 'fas fa-file-invoice', 'list' => 'fas fa-list', 'logout' => 'fas fa-sign-out-alt', 'manual' => 'fas fa-book', 'money' => 'far fa-money-bill-alt', 'print' => 'fas fa-print', 'project' => 'fas fa-project-diagram', 'repeat' => 'fas fa-redo-alt', 'start' => 'fas fa-play-circle', 'start-small' => 'fas fa-play-circle', 'stop' => 'fas fa-stop', 'stop-small' => 'far fa-stop-circle', 'timesheet' => 'far fa-clock', 'trash' => 'far fa-trash-alt', 'user' => 'fas fa-user', 'visibility' => 'far fa-eye', 'settings' => 'fas fa-wrench', ]; /** * @param RequestStack $requestStack * @param array $languages */ public function __construct(RequestStack $requestStack, array $languages) { $this->requestStack = $requestStack; $this->locales = $languages; $this->durationFormatter = new Duration(); } /** * {@inheritdoc} */ public function getFilters() { return [ new TwigFilter('duration', [$this, 'duration']), new TwigFilter('money', [$this, 'money']), new TwigFilter('currency', [$this, 'currency']), new TwigFilter('country', [$this, 'country']), new TwigFilter('icon', [$this, 'icon']), ]; } /** * {@inheritdoc} */ public function getFunctions() { return [ new \Twig_SimpleFunction('locales', [$this, 'getLocales']), new \Twig_SimpleFunction('is_visible_column', [$this, 'isColumnVisible']), ]; } /** * This is only for datatables, do not use it outside this context. * * @param string $dataTable * @param string $column * @param string $size * @return bool */ public function isColumnVisible(string $dataTable, string $column, string $size) { // name handling is spread between here and datatables.html.twig (data_table_column_modal) $dataTable = $dataTable . '_visibility' . $size; if (!isset($this->cookies[$dataTable])) { $visibility = false; if ($this->requestStack->getCurrentRequest()->cookies->has($dataTable)) { $visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($dataTable), true); } $this->cookies[$dataTable] = $visibility; } $values = $this->cookies[$dataTable]; if (empty($values) || !is_array($values)) { return true; } if (isset($values[$column]) && $values[$column] === false) { return false; } return true; } /** * Transforms seconds into a duration string. * * @param int|Timesheet $duration * @param string $format * @return string */ public function duration($duration, $format = null) { $seconds = $duration; if ($duration instanceof Timesheet) { $seconds = $duration->getDuration(); if (null === $duration->getEnd()) { $seconds = time() - $duration->getBegin()->getTimestamp(); } } $locale = $this->getLocale(); switch ($format) { case 'full': $format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration'] : null; break; case null: case 'short': $format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration_short'] : null; break; } if (null === $format) { $format = '%h:%m h'; } return $this->durationFormatter->format($seconds, $format); } /** * @param string $currency * @return string */ public function currency($currency) { return Intl::getCurrencyBundle()->getCurrencySymbol($currency); } /** * @param string $country * @return string */ public function country($country) { return Intl::getRegionBundle()->getCountryName($country); } /** * @param string $name * @param string $default * @return string */ public function icon($name, $default = '') { return self::$icons[$name] ?? $default; } /** * @param float $amount * @param string $currency * @return string */ public function money($amount, $currency = null) { $locale = $this->getLocale(); if ($this->locale !== $locale) { $this->locale = $locale; $this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL); } $fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency); $amount = round($amount, $fractionDigits); $result = $this->numberFormatter->format($amount); if (null !== $currency) { $result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency, $locale); } return $result; } /** * @return string */ protected function getLocale() { return $this->requestStack->getCurrentRequest()->getLocale(); } /** * Takes the list of codes of the locales (languages) enabled in the * application and returns an array with the name of each locale written * in its own language (e.g. English, Français, Español, etc.) * * @return array */ public function getLocales() { $locales = []; foreach (array_keys($this->locales) as $locale) { $locales[] = ['code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale)]; } return $locales; } }