123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
-
- namespace Muzich\CoreBundle\Extension;
-
- use Symfony\Bundle\FrameworkBundle\Translation\Translator;
- use Muzich\CoreBundle\Entity\Event;
- use Symfony\Component\Form\FormView;
-
- class MyTwigExtension extends \Twig_Extension {
-
- private $translator;
-
- public function __construct(Translator $translator)
- {
- $this->translator = $translator;
- }
-
- 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')
- );
- }
-
- public function getFunctions() {
- return array(
- 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'),
- 'event_const' => new \Twig_Filter_Method($this, 'event_const'),
- 'css_list_length_class' => new \Twig_Filter_Method($this, 'getCssLengthClassForList')
- );
- }
-
- public function format_score($score)
- {
- return number_format($score, 0, '.', ' ');
- }
-
- 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->getChildren() as $form_children)
- {
- $form_children_vars = $form_children->getVars();
- $count_error += count($form_children_vars['errors']);
- }
- if ($count_error)
- {
- return true;
- }
- return false;
- }
-
- }
|