public function executeView()
 {
     sfLoader::loadHelpers(array('General', 'MetaLink'));
     parent::executeView();
     // we get the user (id + name) who first uploaded this picture:
     $this->creator = $this->document->getCreator();
     $this->image_type = $this->document['image_type'];
     if (!$this->document->isArchive() && $this->document['redirects_to'] == NULL) {
         // here, we add the summit name to route names :
         $associated_routes = array_filter($this->associated_docs, array('c2cTools', 'is_route'));
         $associated_routes = Route::addBestSummitName($associated_routes, $this->__(' :') . ' ');
         $associated_docs = array_filter($this->associated_docs, array('c2cTools', 'is_not_route'));
         $associated_docs = array_filter($associated_docs, array('c2cTools', 'is_not_image'));
         $associated_docs = array_merge($associated_docs, $associated_routes);
         // sort by document type, name
         if (!empty($associated_docs)) {
             foreach ($associated_docs as $key => $row) {
                 $module[$key] = $row['module'];
                 $name[$key] = remove_accents($row['name']);
             }
             array_multisort($module, SORT_STRING, $name, SORT_STRING, $associated_docs);
         }
         $this->associated_documents = $associated_docs;
         // add linked docs areas (except users and images)
         $parent_ids = array();
         $associated_areas = array();
         foreach ($this->associated_docs as $doc) {
             if (!in_array($doc['module'], array('images', 'users'))) {
                 $parent_ids[] = $doc['id'];
             }
         }
         if (count($parent_ids)) {
             $prefered_cultures = $this->getUser()->getCulturesForDocuments();
             $associated_docs_areas = GeoAssociation::findAreasWithBestName($parent_ids, $prefered_cultures);
             $associated_areas = $this->associated_areas;
             $areas_ids = array();
             foreach ($associated_areas as $area) {
                 $areas_ids[] = $area['id'];
             }
             foreach ($associated_docs_areas as $area) {
                 if (!in_array($area['id'], $areas_ids)) {
                     $associated_areas[] = $area;
                 }
             }
         }
         $this->associated_areas = $associated_areas;
         $related_portals = array();
         $activities = $this->document->get('activities');
         if (in_array(5, $activities)) {
             $related_portals[] = 'ice';
         }
         Portal::getLocalPortals($related_portals, $associated_areas);
         $this->related_portals = $related_portals;
         // link for facebook
         list($image_name, $image_ext) = Images::getFileNameParts($this->document['filename']);
         $image_url = DIRECTORY_SEPARATOR . sfConfig::get('app_upload_dir') . DIRECTORY_SEPARATOR . sfConfig::get('app_images_directory_name') . DIRECTORY_SEPARATOR . $image_name . 'SI' . $image_ext;
         addMetaLink('image_src', $image_url);
     }
 }
 /**
  * Executes Feed action
  * NB: cannot be cached ...
  */
 public function executeFeed()
 {
     $feed = new sfGeoRssFeed();
     $lang = $this->getRequestParameter('lang');
     $model = $this->model_class;
     $module = $this->getModuleName();
     $id = $this->getRequestParameter('id');
     $mode = $this->getRequestParameter('mode');
     if ($lang) {
         $languages = sfConfig::get('app_languages_c2c');
     }
     switch ($mode) {
         case 'editions':
             $description = $this->__($lang ? "Latest %1% editions in %2%" : "Latest %1% editions", array('%1%' => $this->__($module), '%2%' => $lang ? $this->__($languages[$lang]) : null));
             $title = $description;
             // TODO Provide better description, different from title
             $link = "@module_whatsnew?module={$module}";
             // TODO maybe we should propose lang filters for whatsnew url?
             $self_link = $lang ? "@feed_lang?module={$module}&lang={$lang}" : "@feed?module={$module}";
             break;
         case 'creations':
             $description = $this->__($lang ? "Latest %1% creations in %2%" : "Latest %1% creations", array('%1%' => $this->__($module), '%2%' => $lang ? $this->__($languages[$lang]) : null));
             $title = $description;
             // TODO Provide better description, different from title
             $link = "@default_index?module={$module}";
             // TODO can we redirect to list with lang filter once it is done?
             $self_link = $lang ? "@creations_feed_lang?module={$module}&lang={$lang}" : "@creations_feed?module={$module}";
             break;
         default:
             // editions of a specific document
             // check that document $id exists in lang $lang, and retrieve its name.
             if (!($document = DocumentI18n::findName($id, $lang, $this->model_class))) {
                 $this->setNotFoundAndRedirect();
             }
             $name = $document->get('name');
             $description = $this->__("Latest editions for \"%1%\" - %2%", array('%1%' => $name, '%2%' => $this->__($languages[$lang])));
             $title = $description;
             // TODO Provide better description, different from title
             $link = "@document_history?module={$module}&id={$id}&lang={$lang}";
             $self_link = "@document_feed?module={$module}&id={$id}&lang={$lang}";
             break;
     }
     $feed->setTitle($title);
     $feed->setLink($link);
     $feed->setFeedUrl($self_link);
     $feed->setDescription($description);
     $feed->setLanguage($lang);
     $feed->setAuthorName('Camptocamp.org');
     $max_number = sfConfig::get('app_feeds_items_limit');
     if ($module != 'documents' || !empty($id)) {
         //usage: listRecent($model, $limit, $user_id = null, $lang = null, $doc_id = null, $mode = 'editions')
         $items = Document::listRecent($this->model_class, $max_number, null, $lang, $id, $mode);
         sfLoader::loadHelpers(array('General', 'SmartFormat'));
         $model_i18n = $model . 'I18n';
         // Add best summit name for routes
         foreach ($items as $key => $item) {
             $items[$key]['module'] = $item[$model]['module'];
             $items[$key]['id'] = $item['document_id'];
             $items[$key]['name'] = $item[$model_i18n]['name'];
             $items[$key]['search_name'] = $item[$model_i18n]['search_name'];
         }
         $routes = Route::addBestSummitName(array_filter($items, array('c2cTools', 'is_route')), $this->__(' :') . ' ');
         foreach ($routes as $key => $route) {
             $items[$key] = $route;
         }
         foreach ($items as $item) {
             $item_id = $item['document_id'];
             $new = $item['version'];
             $module_name = $item[$model]['module'];
             $name = $item['name'];
             $doc_lang = $item['culture'];
             $feedItemTitle = $name . ($mode != 'creations' ? " - r{$new}" . ($lang ? '' : "/{$doc_lang}") : '');
             $feedItem = new sfGeoFeedItem();
             $feedItem->setTitle($feedItemTitle);
             if ($mode == 'creations') {
                 if ($module_name == 'users') {
                     $feedItem->setLink("@document_by_id_lang?module={$module_name}&id={$item_id}&lang={$doc_lang}");
                 } else {
                     $feedItem->setLink("@document_by_id_lang_slug?module={$module_name}&id={$item_id}&lang={$doc_lang}&slug=" . make_slug($item['name']));
                 }
             } else {
                 $feedItem->setLink("@document_by_id_lang_version?module={$module_name}&id={$item_id}&lang={$doc_lang}&version={$new}");
             }
             $feedItem->setAuthorName($item['history_metadata']['user_private_data']['topo_name']);
             //$feedItem->setAuthorEmail($item['history_metadata']['user_private_data']['email']);
             $feedItem->setPubdate(strtotime($item['created_at']));
             $feedItem->setUniqueId("{$item_id}-{$doc_lang}-{$new}");
             $feedItem->setLongitude($item[$model]['lon']);
             $feedItem->setLatitude($item[$model]['lat']);
             $comment = smart_format($item['history_metadata']['comment']);
             $feedItem->setDescription($comment);
             if ($mode != 'creations') {
                 $link = $new > 1 ? ' - ' . link_to(__('View difference between version %1% and %2%', array('%1%' => $new - 1, '%2%' => $new)), "@document_diff?module={$module_name}&id={$item_id}&lang={$doc_lang}&new={$new}&old=" . ($new - 1), array('absolute' => true)) : '';
                 $feedItem->setContent($comment . $link);
             }
             $feed->addItem($feedItem);
         }
     }
     $this->feed = $feed;
     $this->setTemplate('../../documents/templates/feed');
 }
 /**
  * returns a list of associated routes
  */
 public function executeGetroutes()
 {
     $id = $this->getRequestParameter('summit_id');
     $div_name = $this->getRequestParameter('div_name');
     $div_prefix = $this->getRequestParameter('div_prefix', '');
     $div_id = $div_prefix . $div_name;
     $user = $this->getUser();
     $user_id = $user->getId();
     // if id = 0 or no provided
     if (!$id) {
         return $this->ajax_feedback('Missing id parameter');
     }
     // if session is time-over
     if (!$user_id) {
         return $this->ajax_feedback('Your session is over. Please login again.');
     }
     $summit = Document::find('Summit', $id, array('id', 'elevation'));
     if (!$summit) {
         return $this->ajax_feedback('Summit not found');
     }
     $sub_summits = Summit::getSubSummits($id);
     $ids = array($id);
     foreach ($sub_summits as $sub) {
         $ids[] = $sub['id'];
     }
     $routes = Association::findLinkedDocsWithBestName($ids, $this->getUser()->getCulturesForDocuments(), 'sr');
     $routes = Route::addBestSummitName($routes, $this->__(' :') . ' ');
     $routes = c2cTools::sortArrayByName($routes);
     if (count($routes) == 0) {
         return $this->ajax_feedback($this->__('No associated route found'));
     }
     if (!$div_id) {
         return $this->ajax_feedback('Please chose a "select" container ID in "remote_function"');
     }
     $output = $this->__('Route:') . ' <select id="' . $div_id . '" name="' . $div_name . '" onchange="C2C.getWizardRouteRatings(\'' . $div_id . '\');">';
     foreach ($routes as $route) {
         $output .= '<option value="' . $route['id'] . '">' . $route['name'] . '</option>';
     }
     $output .= '</select>';
     $output .= '<p id="wizard_' . $div_id . '_descr" class="short_descr" style="display: none">' . '<span id="' . $div_id . '_descr">' . $this->__('Short description: ') . $this->__('not available') . '</span></p>';
     return $this->renderText($output);
 }
 /**
  * Executes view action.
  */
 public function executeView()
 {
     sfLoader::loadHelpers(array('General'));
     parent::executeView();
     if (!$this->document->isArchive() && $this->document['redirects_to'] == NULL) {
         // Add the summit name to route
         $associated_routes = array_filter($this->associated_docs, array('c2cTools', 'is_route'));
         $associated_routes = Route::addBestSummitName($associated_routes, $this->__(' :') . ' ');
         // Include outings additional data
         $associated_outings = array_filter($this->associated_docs, array('c2cTools', 'is_outing'));
         $associated_outings = Outing::fetchAdditionalFields($associated_outings);
         // group all linked docs except images in a single list
         $associated_docs = array_filter($this->associated_docs, array('c2cTools', 'is_not_route'));
         $associated_docs = array_filter($associated_docs, array('c2cTools', 'is_not_outing'));
         $associated_docs = array_filter($associated_docs, array('c2cTools', 'is_not_image'));
         $associated_docs = array_merge($associated_docs, $associated_routes, $associated_outings);
         // sort by document type, name
         if (count($associated_docs)) {
             foreach ($associated_docs as $key => $row) {
                 $module[$key] = $row['module'];
                 $name[$key] = remove_accents($row['name']);
             }
             array_multisort($module, SORT_STRING, $name, SORT_STRING, $associated_docs);
         }
         $this->associated_users = array_filter($associated_docs, array('c2cTools', 'is_user'));
         $this->associated_documents = $associated_docs;
         // Retrieve linked docs areas (except users)
         $parent_ids = array();
         $associated_areas = array();
         foreach ($this->associated_docs as $doc) {
             if ($doc['module'] != 'users') {
                 $parent_ids[] = $doc['id'];
             }
         }
         if (count($parent_ids)) {
             $prefered_cultures = $this->getUser()->getCulturesForDocuments();
             $associated_areas = GeoAssociation::findAreasWithBestName($parent_ids, $prefered_cultures);
         }
         $this->associated_areas = $associated_areas;
         // retrieve related portals, depending on article category
         // Soft mobility articles => changedapproche
         // ice activity + gear, stories or expeditions => ice portal
         $related_portals = array();
         $activities = $this->document->get('activities');
         $categories = $this->document->get('categories');
         if (in_array(7, $categories)) {
             $related_portals[] = 'cda';
         }
         if (array_intersect(array(2, 3, 8), $categories) && in_array(5, $activities)) {
             $related_portals[] = 'ice';
         }
         Portal::getLocalPortals($related_portals, $associated_areas);
         $this->related_portals = $related_portals;
         sfLoader::loadHelpers(array('sfBBCode', 'SmartFormat'));
         $abstract = strip_tags(parse_links(parse_bbcode_abstract($this->document->get('abstract'))));
         $this->getResponse()->addMeta('description', $abstract);
         if (in_array(100, $categories)) {
             $this->getResponse()->addMeta('robots', 'noindex, follow');
         }
     }
 }
 public static function getLastDocs($summit_separator = ': ')
 {
     /*
     $q = Doctrine_Query::create()
                          ->select('i.name, i.search_name, i.culture, a.id, a.module')
                          ->from('DocumentVersion d')
                          ->leftJoin('d.DocumentArchive a')
                          ->leftJoin('d.DocumentI18nArchive i')
                          ->where("d.version = 1 AND a.module != 'outings' AND a.module != 'users' AND a.module != 'images'")
                          ->limit(20)
                          ->orderBy('d.created_at DESC');
     //return $q->execute(array(), Doctrine::FETCH_ARRAY); // FIXME: returns nothing!?
     $sql = $q->getSql();
     */
     // following query is ok, but the displayed name is the first name given to the document, should be the last one
     /*
             $sql = 'SELECT a2.id AS id, a2.module AS module, a3.name AS name, a3.search_name AS search_name, a3.culture AS culture ' .
                    'FROM app_documents_versions a ' .
                    'LEFT JOIN app_documents_archives a2 ON a.document_archive_id = a2.document_archive_id ' .
                    'LEFT JOIN app_documents_i18n_archives a3 ON a.document_i18n_archive_id = a3.document_i18n_archive_id ' .
                    "WHERE (a.version = 1 AND a2.module != 'outings' AND a2.module !=  'users' AND a2.module != 'images' AND a2.module != 'articles') " .
                    'ORDER BY a.created_at DESC LIMIT 20';*/
     // this one uses last document name
     $sql = 'SELECT sub.id AS id, sub.module AS module, a3.name AS name, a3.search_name AS search_name, a3.culture AS culture ' . 'FROM ' . '(SELECT a2.id AS id, a2.module AS module, a.culture AS culture FROM app_documents_versions a ' . 'LEFT JOIN app_documents_archives a2 ON a.document_archive_id = a2.document_archive_id ' . "WHERE (a.version = 1 AND a2.module NOT IN ('outings', 'users', 'images', 'articles') AND a2.redirects_to IS NULL) " . 'ORDER BY a.created_at DESC LIMIT 20) AS sub ' . 'LEFT JOIN documents_i18n a3 ON a3.id = sub.id AND sub.culture = a3.culture';
     $docs = sfDoctrine::connection()->standaloneQuery($sql)->fetchAll();
     // get summit name for routes items
     $routes = Route::addBestSummitName(array_filter($docs, array('c2cTools', 'is_route')), $summit_separator);
     foreach ($routes as $key => $route) {
         $docs[$key] = $route;
     }
     return $docs;
 }