/**
  * Executes view action : displays the user profile
  */
 public function executeView()
 {
     $id = $this->getRequestParameter('id');
     parent::executeView();
     $hasPublicProfile = UserPrivateData::hasPublicProfile($id);
     if (!$this->getUser()->isConnected() && !$hasPublicProfile) {
         // page owner has not allowed anonymous users to access his personal page
         $this->setTemplate('login');
     } else {
         if (!$this->document->isArchive() && $this->document['redirects_to'] == NULL) {
             if ($hasPublicProfile) {
                 $this->getResponse()->addMeta('robots', 'index, follow');
             } else {
                 $this->getResponse()->addMeta('robots', 'noindex, nofollow');
             }
             // get associated outings
             $associated_outings = array();
             $nb_outings = count(Association::countAllLinked(array($id), 'uo'));
             $nb_outings_limit = 0;
             if ($nb_outings > 0) {
                 $outing_params = array('users' => $id);
                 $nb_outings_limit = sfConfig::get('app_users_outings_limit');
                 $associated_outings = Outing::listLatest($nb_outings_limit, array(), array(), array(), $outing_params, false, false);
                 $associated_outings = Language::getTheBest($associated_outings, 'Outing');
             }
             $this->nb_outings = $nb_outings;
             $this->nb_outings_limit = $nb_outings_limit;
             $this->associated_outings = $associated_outings;
             $forum_nickname = Punbb::getNickname($id);
             $this->forum_nickname = $forum_nickname[0]['username'];
             // check if user is forum and / or topoguide moderator
             $this->forum_moderator = UserPrivateData::isForumModerator($id);
             $user_permissions = Doctrine_Query::create()->from('User.groups.permissions, User.permissions')->where('User.id = ?', $id)->execute(array(), Doctrine::FETCH_ARRAY);
             $topoguide_moderator = false;
             $moderator_credential = sfConfig::get('app_credentials_moderator');
             foreach ($user_permissions[0]['groups'] as $group) {
                 foreach ($group['permissions'] as $permission) {
                     if ($permission['name'] == $moderator_credential) {
                         $topoguide_moderator = true;
                         break 2;
                     }
                 }
             }
             foreach ($user_permissions[0]['permissions'] as $permission) {
                 if ($permission['name'] == $moderator_credential) {
                     $topoguide_moderator = true;
                     break;
                 }
             }
             $this->topoguide_moderator = $topoguide_moderator;
         } else {
             // only moderators and user itself should see archive versions of user docs
             $this->filterAuthorizedPeople($id);
         }
     }
 }
 /**
  * Executes view action.
  */
 public function executeView()
 {
     parent::executeView();
     // redefine page title: prepend date
     sfLoader::loadHelpers(array('Date'));
     $title = $this->document->get('name') . ', ' . format_date($this->document->get('date'), 'D') . ' :: ' . $this->__('outing');
     $this->setPageTitle($title);
     if (!$this->document->isArchive() && $this->document['redirects_to'] == NULL) {
         $user = $this->getUser();
         $prefered_cultures = $user->getCulturesForDocuments();
         $associated_routes = Route::getAssociatedRoutesData($this->associated_docs, $this->__(' :') . ' ');
         // determines outing max elevation using routes max elevations if it is not set
         $max_elevation = $this->document->getMaxElevation();
         if ($max_elevation == NULL || $max_elevation == 0) {
             $outing_max_elevation = 0;
             foreach ($associated_routes as $route) {
                 $route_max_elevation = $route['max_elevation'] instanceof Doctrine_Null ? 0 : $route['max_elevation'];
                 if ($route_max_elevation > $outing_max_elevation) {
                     $outing_max_elevation = $route_max_elevation;
                 }
             }
             if ($outing_max_elevation > 0) {
                 $this->document->setMaxElevation($outing_max_elevation);
             }
         }
         // the routes/sites linked to the outing will be used to retrieve 2-hops docs like summits,
         // huts, parkings....
         // However, we need some special handling with raid routes (more than 2 days)
         $parent_ids = $other_ids = $other_routes = $default_ids = array();
         $associated_summits = array();
         $associated_huts = array();
         $associated_parkings = array();
         if (count($associated_routes)) {
             $associated_routes = c2cTools::sortArray($associated_routes, 'duration');
             foreach ($associated_routes as $route) {
                 // pour les docs de 2?me niveau, on retient uniquement les itin?raires de 1 ou 2 jours
                 if (!$route['duration'] instanceof Doctrine_Null) {
                     if ($route['duration'] <= 4) {
                         $parent_ids[] = $route['id'];
                     } else {
                         $other_routes[$route['id']] = $route['duration'];
                     }
                 } else {
                     $default_ids[] = $route['id'];
                 }
             }
             // s'il n'y a pas d'itin?raire de 1 ou 2 jours, on utilise les itin?raires qui ont la plus petite dur?e
             // s'il n'y en a pas non plus, on utilise les itin?raire dont la dur?e est non renseign?e
             if (!count($parent_ids)) {
                 if (count($other_routes) > 1) {
                     asort($other_routes);
                     $min_duration = $other_routes . reset();
                     foreach ($other_routes as $id => $duration) {
                         if ($duration == $min_duration) {
                             $other_ids[] = $id;
                         }
                     }
                 } elseif (count($other_routes)) {
                     $other_ids[] = key($other_routes);
                 } else {
                     $other_ids = $default_ids;
                 }
                 $parent_ids = $other_ids;
             }
         }
         if (count($this->associated_sites)) {
             $associated_sites = $this->associated_sites;
             foreach ($associated_sites as $site) {
                 $parent_ids[] = $site['id'];
             }
         }
         // now retrieve the associated docs (summits, huts, parkings)
         if (count($parent_ids)) {
             $associated_route_docs = Association::findLinkedDocsWithBestName($parent_ids, $prefered_cultures, array('sr', 'hr', 'pr', 'pt'), false, false);
             if (count($associated_route_docs)) {
                 $associated_route_docs = c2cTools::sortArray($associated_route_docs, 'elevation');
                 $associated_summits = array_filter($associated_route_docs, array('c2cTools', 'is_summit'));
                 $associated_huts = array_filter($associated_route_docs, array('c2cTools', 'is_hut'));
                 $associated_parkings = Parking::getAssociatedParkingsData(array_filter($associated_route_docs, array('c2cTools', 'is_parking')));
                 if (count($associated_summits) && count($associated_huts)) {
                     $summit_ids = $summit_hut_tmp = $summit_hut_ids = array();
                     foreach ($associated_summits as $summit) {
                         $summit_ids[] = $summit['id'];
                     }
                     $summit_hut_tmp = Association::countAllLinked($summit_ids, 'sh');
                     if (count($summit_hut_tmp)) {
                         foreach ($summit_hut_tmp as $hut) {
                             $summit_hut_ids[] = $hut['main_id'];
                         }
                         foreach ($associated_summits as $key => $summit) {
                             if (in_array($summit['id'], $summit_hut_ids)) {
                                 unset($associated_summits[$key]);
                             }
                         }
                     }
                 }
             }
         }
         $this->associated_summits = $associated_summits;
         $this->associated_huts = $associated_huts;
         $this->associated_parkings = $associated_parkings;
         $this->associated_routes = $associated_routes;
         // associated users
         $associated_users = array_filter($this->associated_docs, array('c2cTools', 'is_user'));
         if (count($associated_users) >= 2) {
             // Set outing creator at first in the users list, and sort other users by name
             $creator_id = $this->document->getCreatorId();
             $creator = array();
             $associated_users_2 = array();
             foreach ($associated_users as $key => $user) {
                 if ($user['id'] == $creator_id) {
                     $creator[$key] = $user;
                 } else {
                     $associated_users_2[$key] = $user;
                 }
             }
             if (count($associated_users_2) >= 2) {
                 $associated_users_2 = c2cTools::sortArrayByName($associated_users_2);
             }
             $associated_users = array_merge($creator, $associated_users_2);
         }
         $this->associated_users = $associated_users;
         // related portals
         $related_portals = array();
         $activities = $this->document->get('activities');
         $outing_with_public_transportation = $this->document->get('outing_with_public_transportation');
         if (!$outing_with_public_transportation instanceof Doctrine_Null && $outing_with_public_transportation) {
             $related_portals[] = 'cda';
         }
         Portal::getRelatedPortals($related_portals, $this->associated_areas, $associated_routes, $activities);
         $this->related_portals = $related_portals;
         $description = array($title, $this->getActivitiesList(), $this->getAreasList());
         $this->getResponse()->addMeta('description', implode(' - ', $description));
     } else {
         // only moderators and associated users should see archive versions of outings
         $this->filterAuthorizedPeople($this->getRequestParameter('id'));
     }
 }
 /**
  * Executes view action.
  */
 public function executeView()
 {
     parent::executeView();
     if (!$this->document->isArchive() && $this->document['redirects_to'] == NULL) {
         $user = $this->getUser();
         $prefered_cultures = $user->getCulturesForDocuments();
         $current_doc_id = $this->getRequestParameter('id');
         $is_gite_camping = $this->document->get('shelter_type') == 5 || $this->document->get('shelter_type') == 6;
         // retrieve 2-hops parkings
         $associated_parkings = c2cTools::sortArray(array_filter($this->associated_docs, array('c2cTools', 'is_parking')), 'elevation');
         if (count($associated_parkings)) {
             $associated_parkings = Association::createHierarchyWithBestName($associated_parkings, $prefered_cultures, array('type' => 'pp'));
             $associated_parkings = Parking::getAssociatedParkingsData($associated_parkings);
         }
         $this->associated_parkings = $associated_parkings;
         $associated_routes = array_filter($this->associated_docs, array('c2cTools', 'is_route'));
         // associated summits
         $associated_summits = c2cTools::sortArray(array_filter($this->associated_docs, array('c2cTools', 'is_summit')), 'name');
         $this->associated_summits = $associated_summits;
         $summit_ids = $summit_routes_tmp = $summit_routes_ids = array();
         if (count($associated_summits)) {
             foreach ($associated_summits as $summit) {
                 $summit_ids[] = $summit['id'];
             }
             $summit_routes_tmp = Association::countAllLinked($summit_ids, 'sr');
             foreach ($summit_routes_tmp as $route) {
                 $summit_routes_ids[] = $route['linked_id'];
             }
         }
         // for a gite, routes  and sites are not directly linked. We retrieve the routes linked to the linked parkings
         if ($is_gite_camping) {
             $parking_ids = array();
             foreach ($associated_parkings as $parking) {
                 $parking_ids[] = $parking['id'];
             }
             $associated_parking_docs = Association::findLinkedDocsWithBestName($parking_ids, $prefered_cultures, array('pr', 'pt'), false, true);
             $associated_routes = array_filter($associated_parking_docs, array('c2cTools', 'is_route'));
             $associated_parking_sites = c2cTools::sortArrayByName(array_filter($associated_parking_docs, array('c2cTools', 'is_site')));
             $this->associated_sites = array_merge($this->associated_sites, $associated_parking_sites);
             // associated sites should be empty!! Else it violates moderation rules
             $this->ids = implode('-', $parking_ids);
         } else {
             $associated_routes = array_filter($this->associated_docs, array('c2cTools', 'is_route'));
             $this->associated_sites = c2cTools::sortArrayByName(array_filter($this->associated_docs, array('c2cTools', 'is_site')));
             $this->ids = $current_doc_id;
         }
         // get additional data for routes
         $associated_routes = Route::getAssociatedRoutesData($associated_routes, $this->__(' :') . ' ', reset($summit_ids));
         // these are the routes where the hut act as a summit
         // they are displayed in a specific section
         $summit_ids = $summit_routes_tmp = $summit_routes_ids = $associated_summit_routes = array();
         if (count($associated_summits)) {
             foreach ($associated_summits as $summit) {
                 $summit_ids[] = $summit['id'];
             }
             $summit_routes_tmp = Association::countAllLinked($summit_ids, 'sr');
             foreach ($summit_routes_tmp as $route) {
                 $summit_routes_ids[] = $route['linked_id'];
             }
         }
         if (count($summit_routes_ids)) {
             foreach ($associated_routes as $key => $route) {
                 if (in_array($route['id'], $summit_routes_ids)) {
                     $associated_summit_routes[$key] = $route;
                     unset($associated_routes[$key]);
                 }
             }
         }
         $this->associated_routes = $associated_routes;
         $this->associated_summit_routes = $associated_summit_routes;
         // We retrieve both the books directly linked
         $associated_books = c2cTools::sortArrayByName(array_filter($this->associated_docs, array('c2cTools', 'is_book')));
         // AND the books linked to linked routes
         // FIXME we should probably also do this with linked sites
         $route_ids = array();
         $associated_routes_books = array();
         if (count($associated_routes)) {
             foreach ($associated_routes as $route) {
                 if ($route['duration'] instanceof Doctrine_Null || $route['duration'] <= 4) {
                     $route_ids[] = $route['id'];
                 }
             }
             if (count($route_ids)) {
                 $book_ids = array();
                 foreach ($associated_books as $book) {
                     $book_ids[] = $book['id'];
                 }
                 $associated_route_docs = Association::findLinkedDocsWithBestName($route_ids, $prefered_cultures, array('br'), false, false, $book_ids);
                 if (count($associated_route_docs)) {
                     $associated_route_docs = c2cTools::sortArray($associated_route_docs, 'name');
                     $associated_routes_books = array_filter($associated_route_docs, array('c2cTools', 'is_book'));
                     foreach ($associated_routes_books as $key => $book) {
                         $associated_routes_books[$key]['parent_id'] = true;
                     }
                 }
             }
         }
         $associated_books = array_merge($associated_books, $associated_routes_books);
         if (count($associated_books)) {
             $associated_books = Book::getAssociatedBooksData($associated_books);
         }
         $this->associated_books = $associated_books;
         // get associated outings
         $latest_outings = array();
         $nb_outings = 0;
         if (!$is_gite_camping && count($associated_routes) || $is_gite_camping && count($parking_ids)) {
             if (!$is_gite_camping) {
                 $outing_params = array('huts' => $current_doc_id);
             } else {
                 $outing_params = array('parkings' => $this->ids);
             }
             $nb_outings = sfConfig::get('app_nb_linked_outings_docs');
             $latest_outings = Outing::listLatest($nb_outings + 1, array(), array(), array(), $outing_params, false);
             $latest_outings = Language::getTheBest($latest_outings, 'Outing');
         }
         $this->latest_outings = $latest_outings;
         $this->nb_outings = $nb_outings;
         // possibly related portals
         $related_portals = array();
         Portal::getRelatedPortals($related_portals, $this->associated_areas, $associated_routes);
         $this->related_portals = $related_portals;
         $cab = count($associated_books);
         $this->section_list = array('books' => $cab != 0, 'map' => (bool) $this->document->get('geom_wkt'));
         $hut_type_list = sfConfig::get('mod_huts_shelter_types_list');
         $hut_type_index = $this->document->get('shelter_type');
         $hut_type = $this->__($hut_type_list[$hut_type_index]);
         $doc_name = $this->document->get('name');
         $title = $doc_name;
         if ($this->document->isArchive()) {
             $version = $this->getRequestParameter('version');
             $title .= ' :: ' . $this->__('revision') . ' ' . $version;
         }
         $title .= ' :: ' . $hut_type;
         $this->setPageTitle($title);
         $description = array($hut_type . ' :: ' . $doc_name, $this->getActivitiesList(), $this->getAreasList());
         $this->getResponse()->addMeta('description', implode(' - ', $description));
     }
 }
Example #4
0
 public static function fetchAdditionalFields($objects, $add_fields = true, $add_images_count = false)
 {
     if (!count($objects)) {
         return array();
     }
     $ids = array();
     $q = array();
     $out = array();
     // build ids list
     foreach ($objects as $object) {
         $ids[] = $object['id'];
         $q[] = '?';
     }
     if ($add_fields) {
         // db request fetching array with all requested fields
         $results = Doctrine_Query::create()->select('m.activities, m.date, m.geom_wkt, v.version, hm.user_id, u.topo_name')->from('Outing m')->leftJoin('m.versions v')->leftJoin('v.history_metadata hm')->leftJoin('hm.user_private_data u')->where('m.id IN ( ' . implode(', ', $q) . ' )', $ids)->addWhere('v.version = 1')->orderBy('m.date DESC')->execute(array(), Doctrine::FETCH_ARRAY);
         // merge array 'results' into array '$objects' on the basis of same 'id' key
         foreach ($objects as $object) {
             $id = $object['id'];
             foreach ($results as $result) {
                 if ($result['id'] == $id) {
                     $out[] = array_merge($object, $result);
                 }
             }
         }
     } else {
         $out = $objects;
     }
     if ($add_images_count) {
         $image_links = Association::countAllLinked($ids, 'oi');
         $image_counts = array();
         foreach ($image_links as $image_link) {
             $main_id = $image_link['main_id'];
             if (isset($image_counts[$main_id])) {
                 $image_counts[$main_id]++;
             } else {
                 $image_counts[$main_id] = 1;
             }
         }
         foreach ($out as &$outing) {
             if (isset($image_counts[$outing['id']])) {
                 $outing['nb_images'] = $image_counts[$outing['id']];
             }
         }
     }
     return $out;
 }
 public static function countAssociatedDocuments(&$docs, $type, $is_main)
 {
     if (count($docs) == 0) {
         return;
     }
     $doc_ids = array();
     foreach ($docs as $key => $doc) {
         $doc_ids[] = $doc['id'];
     }
     if ($is_main) {
         $associations = Association::countAllLinked($doc_ids, $type);
     } else {
         $associations = Association::countAllMain($doc_ids, $type);
     }
     if (count($associations) == 0) {
         return;
     }
     $linked_doc_count = array();
     foreach ($associations as $assoc) {
         if ($is_main) {
             $linked_doc_count[] = $assoc['main_id'];
         } else {
             $linked_doc_count[] = $assoc['linked_id'];
         }
     }
     $linked_doc_count = array_count_values($linked_doc_count);
     foreach ($docs as $key => $doc) {
         $doc_id = $doc['id'];
         if (isset($linked_doc_count[$doc_id])) {
             $docs[$key]['nb_linked_docs'] = $linked_doc_count[$doc_id];
         } else {
             $docs[$key]['nb_linked_docs'] = 0;
         }
     }
 }
 public static function parseListItems($array, $modelName, $count_images = true)
 {
     $langs = sfContext::getInstance()->getUser()->getPreferedLanguageList();
     $parsed_array = self::getTheBest($array, $modelName, $langs);
     // once we have selected the best lang to display this item name,
     // we build search string for comments (eg: 333_fr)
     $_str = array();
     // extract independant country list
     $countries = array();
     foreach ($parsed_array as $key => $item) {
         // build ids strings to get nb of comments
         $_str[$item['id']] = $item['id'] . '_' . $item[$modelName . 'I18n'][0]['culture'];
         // extract best name for associated regions
         if (isset($item['geoassociations'])) {
             $geo_associations = $item['geoassociations'];
             $parsed_array[$key]['geoassociations'] = self::getTheBest($geo_associations, 'Area', $langs, 'linked_id');
             if (count($geo_associations) > 1) {
                 foreach ($geo_associations as $geo_association) {
                     if ($geo_association['type'] == 'dc') {
                         $countries[$geo_association['linked_id']] = true;
                     }
                 }
             }
         }
     }
     // if all docs are in the same country, country data are removed from list
     if (count($countries) == 1) {
         $country_id = key($countries);
         foreach ($parsed_array as $key => $item) {
             if (isset($item['geoassociations'])) {
                 if (isset($item['geoassociations'][$country_id])) {
                     unset($parsed_array[$key]['geoassociations'][$country_id]);
                 }
             }
         }
     }
     // get nb of comments for all items
     $pun_msgs = PunbbComm::retrieveNbComments($_str);
     // merge this info into $parsed_array
     foreach ($pun_msgs as $pun_msg) {
         $id = substr($pun_msg['subject'], 0, strpos($pun_msg['subject'], '_'));
         foreach ($parsed_array as $key => $item) {
             if ($key == $id) {
                 $parsed_array[$key]['nb_comments'] = $pun_msg['nb_comments'];
             }
         }
     }
     if ($count_images && $modelName != 'Image') {
         // Count all images linked
         $image_links = Association::countAllLinked(array_keys($_str), c2cTools::Model2Letter($modelName) . 'i');
         // merge this info into $parsed_array
         foreach ($image_links as $image_link) {
             $main_id = $image_link['main_id'];
             if (isset($parsed_array[$main_id]['nb_images'])) {
                 $parsed_array[$main_id]['nb_images']++;
             } else {
                 $parsed_array[$main_id]['nb_images'] = 1;
             }
         }
     }
     if ($modelName == 'Route') {
         // find highest associated summit
         foreach ($parsed_array as $key => $item) {
             $parsed_array[$key]['associations'] = self::getTheHighest($item['associations'], 'Summit');
             // once this is done, find his best name
             $parsed_array[$key]['associations'][0]['Summit'] = self::getTheBest($parsed_array[$key]['associations'][0]['Summit'], 'Summit', $langs, '', false);
         }
     }
     return $parsed_array;
 }