/**
  * Create/update json file Etag with the tag value.
  *
  * @param string $tag
  * @param string $id
  * @param string $lang
  * @param array  $params
  * 
  * @return boolean true if the tag have been insert corectly in the json file.
  * @access public
  * @author Etienne de Longeaux <*****@*****.**>
  * @since  2014-04-03
  */
 public function setJsonFileEtag($tag, $id, $lang, $params = null)
 {
     $result = false;
     // we set the time
     $now = $this->setTimestampNow();
     // we set the Etag.
     $this->createEtag($tag, $id, $lang, $params);
     // we set the path
     $path = $this->container->getParameter("pi_app_admin.cache_dir.etag");
     // we set the file name
     if (isset($params['page-url']) && !empty($params['page-url']) && $tag == "page") {
         // if the page is sluggify
         if ($this->isSluggifyPage()) {
             $path_json_file_tmp = $this->createJsonFileName('page-sluggify-tmp', $this->Etag, $lang);
             if (!file_exists($path_json_file_tmp)) {
                 $result = PiFileManager::save($path_json_file_tmp, $now . '|' . $this->Etag . '|' . $params['page-url'] . "\n", 0777, LOCK_EX);
                 // we add new Etag in the sluggify file.
                 $path_json_file_sluggify = $this->createJsonFileName('page-sluggify', $id, $lang);
                 $result = PiFileManager::save($path_json_file_sluggify, $now . '|' . $this->Etag . '|' . $params['page-url'] . "\n", 0777, FILE_APPEND);
             }
             // if the page has queries
         } elseif ($this->isQueryStringPage()) {
             $path_json_file_tmp = $this->createJsonFileName('page-history-tmp', $this->Etag, $lang);
             if (!file_exists($path_json_file_tmp)) {
                 $result = PiFileManager::save($path_json_file_tmp, $now . '|' . $this->Etag . '|' . $params['page-url'] . "\n", 0777, LOCK_EX);
                 // we add new Etag in the history.
                 $path_json_file_history = $this->createJsonFileName('page-history', $id, $lang);
                 $result = PiFileManager::save($path_json_file_history, $now . '|' . $this->Etag . '|' . $params['page-url'] . "\n", 0777, FILE_APPEND);
             }
         } else {
             $path_json_file = $this->createJsonFileName('page', $id, $lang);
             if (!file_exists($path_json_file)) {
                 $result = PiFileManager::save($path_json_file, $now . '|' . $this->Etag . "\n", 0777, LOCK_EX);
             }
         }
     } elseif (isset($params['esi-url']) && !empty($params['esi-url']) && $tag == "esi") {
         $path_json_file_tmp = $this->createJsonFileName('esi-tmp', $params['esi-url'], $lang);
         if (!file_exists($path_json_file_tmp)) {
             $result = PiFileManager::save($path_json_file_tmp, $now . '|' . $params['esi-url'] . "\n", 0777, LOCK_EX);
             // we add new ESI tag in the file.
             $path_json_file = $this->createJsonFileName('esi', $id, $lang);
             $result = PiFileManager::save($path_json_file, $now . '|' . $params['esi-url'] . "\n", 0777, FILE_APPEND);
         }
     } elseif (isset($params['widget-id']) && !empty($params['widget-id'])) {
         if (isset($params['widget-sluggify-url'])) {
             $path_json_file_tmp = $this->createJsonFileName('widget-history-tmp', $this->Etag, $lang);
             if (!file_exists($path_json_file_tmp)) {
                 $result = PiFileManager::save($path_json_file_tmp, $now . '|' . $this->Etag . "\n", 0777, LOCK_EX);
                 // we add new Etag in the history.
                 $path_json_file_history = $this->createJsonFileName('widget-history', $params['widget-id'], $lang);
                 $result = PiFileManager::save($path_json_file_history, $now . '|' . $this->Etag . "\n", 0777, FILE_APPEND);
             }
         } else {
             $path_json_file = $this->createJsonFileName('widget', $params['widget-id'], $lang);
             $result = PiFileManager::save($path_json_file, $now . '|' . $this->Etag . "\n", 0777, LOCK_EX);
         }
     } else {
         $path_json_file_tmp = $this->createJsonFileName('default-tmp', $this->Etag, $lang);
         if (!file_exists($path_json_file_tmp)) {
             $result = PiFileManager::save($path_json_file_tmp, $now . '|' . $this->Etag . "\n", 0777, LOCK_EX);
             $path_json_file = $this->createJsonFileName('default', $tag, $lang);
             $result = PiFileManager::save($path_json_file, $now . '|' . $this->Etag . "\n", 0777, FILE_APPEND);
         }
     }
     return $result;
 }
 /**
  * Delete the cache of all elements of an entity (Page, TranslationPages, widgets)
  *
  * @param object $entity Page or TranslationPage entity
  * @param string $type   ['persist', 'update', 'remove']
  * 
  * @return string
  * @access public
  * @author Etienne de Longeaux <*****@*****.**>
  * @since  2014-06-07
  */
 public function cachePage($entity, $type)
 {
     if ($entity instanceof TranslationPage) {
         $entity = $entity->getPage();
     }
     if ($entity instanceof Page) {
         $path_page_json_file = $this->createJsonFileName('page-json', $entity->getRouteName());
         if (in_array($type, array('persist', 'update'))) {
             $reports = serialize($entity);
             $result = \Sfynx\ToolBundle\Util\PiFileManager::save($path_page_json_file, $reports, 0777, LOCK_EX);
         } elseif ($type == 'remove') {
             if (file_exists($path_page_json_file)) {
                 unlink($path_page_json_file);
             }
         }
     }
 }