translator = $translator; $this->params = $params; } public function getFilters() { return array( 'var_dump' => new \Twig_Filter_Function('var_dump'), 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'), 'date_epurate' => new \Twig_Filter_Method($this, 'date_epurate'), 'form_has_errors' => new \Twig_Filter_Method($this, 'form_has_errors'), 'format_score' => new \Twig_Filter_Method($this, 'format_score'), 'can_autoplay' => new \Twig_Filter_Method($this, 'can_autoplay') ); } public function getFunctions() { return array( 'date_or_relative_date' => new \Twig_Function_Method($this, 'date_or_relative_date'), 'event_const' => new \Twig_Function_Method($this, 'event_const'), 'css_list_length_class' => new \Twig_Function_Method($this, 'getCssLengthClassForList') ); } public function format_score($score) { return number_format($score, 0, '.', ' '); } public function can_autoplay($element) { if (in_array($element->getType(), $this->params['autoplay_sites_enabled'])) { return true; } return false; } protected function datetime2timestamp($string) { list($date, $time) = explode(' ', $string); list($year, $month, $day) = explode('-', $date); list($hour, $minute, $second) = explode(':', $time); $timestamp = mktime($hour, $minute, $second, $month, $day, $year); return $timestamp; } protected function translate_date_relative($tr, $type, $x) { if ($x != 1) { return $this->translator->trans( $tr.'x_'.$type, array('%x%' => $x), 'messages' ); } return $this->translator->trans( $tr.'one_'.$type, array(), 'messages' ); } public function date_or_relative_date($sentence, $context = "default") { $iTimeDifference = time() - $this->datetime2timestamp($sentence); if( $iTimeDifference<0 ) { return $this->translator->trans('date.instant', array(), 'userui');; } $iSeconds = $iTimeDifference ; $iMinutes = round( $iTimeDifference/60 ); $iHours = round( $iTimeDifference/3600 ); $iDays = round( $iTimeDifference/86400 ); $iWeeks = round( $iTimeDifference/604800 ); $iMonths = round( $iTimeDifference/2419200 ); $iYears = round( $iTimeDifference/29030400 ); $tr = 'date_since.'.$context.'.'; if( $iSeconds<60 ) { return $this->translator->trans('date_since.'.$context.'.less_min', array(), 'messages'); } elseif( $iMinutes<60 ) { return $this->translate_date_relative($tr, 'min', $iMinutes); } elseif( $iHours<24 ) { return $this->translate_date_relative($tr, 'hour', $iHours); } elseif( $iDays<7 ) { return $this->translate_date_relative($tr, 'day', $iDays); } elseif( $iWeeks <4 ) { return $this->translate_date_relative($tr, 'week', $iWeeks); } elseif( $iMonths<12 ) { return $this->translate_date_relative($tr, 'month', $iMonths); } else { return $this->translate_date_relative($tr, 'year', $iYears); } } public function getName() { return 'my_twig_extension'; } public function date_epurate($date) { $date = str_replace(' ', '', $date); $date = str_replace('-', '', $date); $date = str_replace(':', '', $date); $date = str_replace('.', '', $date); return $date; } public function event_const($const_name) { switch ($const_name) { case 'TYPE_COMMENT_ADDED_ELEMENT': return Event::TYPE_COMMENT_ADDED_ELEMENT; break; case 'TYPE_FAV_ADDED_ELEMENT': return Event::TYPE_FAV_ADDED_ELEMENT; break; case 'TYPE_USER_FOLLOW': return Event::TYPE_USER_FOLLOW; break; case 'TYPE_TAGS_PROPOSED': return Event::TYPE_TAGS_PROPOSED; break; default: throw new \Exception('Constante non géré dans MyTwigExtension::event_const'); break; } return null; } /** * Cette fonction retourne une classe CSS (string) permettant de donner * de l'importance a des élements de liste en foncyion de leur position dans * la liste. * * @param int $position * @param int $count * @return string */ public function getCssLengthClassForList($position, $count) { // On établie 3 type de taille // grand, moyen, standart // chacun correspondant a 1/3 if ($position <= $count/3) { return 'list_length_big'; } elseif ($position <= ($count/3)*2) { return 'list_length_medium'; } else { return 'list_length_default'; } } public function form_has_errors(FormView $form) { $form_vars = $form->getVars(); $count_error = count($form_vars['errors']); foreach ($form as $form_children) { $form_children_vars = $form_children->getVars(); $count_error += count($form_children_vars['errors']); } if ($count_error) { return true; } return false; } }