protected function endEdit()
 {
     //Test if form is submitted or not
     if ($this->success) {
         // try to perform association with linked_doc (if pertinent)
         $summit_id = $this->getRequestParameter('summit_id');
         $id = $this->document->get('id');
         $user_id = $this->getUser()->getId();
         if ($this->new_document && $summit_id || $summit_id && !Association::find($summit_id, $id, 'sr')) {
             $sr = new Association();
             $sr->doSaveWithValues($summit_id, $id, 'sr', $user_id);
             // main, linked, type
             // clear cache of associated summit ...
             $this->clearCache('summits', $summit_id, false, 'view');
         }
         // here if we have created a new document and if $this->document->get('geom_wkt') is null, then use associated doc geom associations:
         // this allows us to filter on ranges even if no GPX is uploaded
         if ($this->new_document && $summit_id && !$this->document->get('geom_wkt')) {
             // get all associated regions (3+maps) with this summit:
             $associations = GeoAssociation::findAllAssociations($summit_id, array('dr', 'dc', 'dd', 'dv', 'dm'));
             // replicate them with route_id instead of summit_id:
             foreach ($associations as $ea) {
                 $a = new GeoAssociation();
                 $a->doSaveWithValues($id, $ea->get('linked_id'), $ea->get('type'));
             }
         }
         // if we add a route to a summit-hut, link the route to the hut
         $hut_asso = Association::findAllAssociations($summit_id, 'sh');
         if ($this->new_document && $summit_id && count($hut_asso) > 0) {
             // associate hut to summit
             $asso = new Association();
             $hut_id = $hut_asso[0]->get('linked_id');
             $asso->doSaveWithValues($hut_id, $id, 'hr', 2);
             // C2C user
             // clear cache of associated hut ...
             $this->clearCache('huts', $hut_id, false, 'view');
         }
         parent::endEdit();
         // redirect to document view
     } else {
         if ($this->link_with = $this->getRequestParameter('link')) {
             // linked_doc was already retrieved in filterAdditionalParameters
             if ($this->linked_doc) {
                 $this->linked_doc->setBestCulture($this->getUser()->getCulturesForDocuments());
             }
         } else {
             $this->linked_doc = $this->getHighestSummit();
         }
     }
 }
 protected function endEdit()
 {
     if ($this->success) {
         // before redirecting view, we check if either the name, elevation or geolocalization of the hut
         // has changed and pass on those changes to the 'ghost summit'
         $hut_doc = $this->document;
         $summit_doc = $hut_doc->getGhostSummit();
         // check wether elevation, name or geometry of the hut has changed, and
         // change accordingly the ghost summit if that's the case
         if ($summit_doc != false) {
             $geom_changed = $hut_doc->get('lat') !== $summit_doc->get('lat') || $hut_doc->get('lon') !== $summit_doc->get('lon');
             if ($hut_doc->get('elevation') !== $summit_doc->get('elevation') || $hut_doc->get('name') !== $summit_doc->get('name') || $geom_changed) {
                 c2cTools::log('Updating ghost summit of hut');
                 $id = $summit_doc->get('id');
                 $conn = sfDoctrine::Connection();
                 try {
                     $conn->beginTransaction();
                     $history_metadata = new HistoryMetadata();
                     $history_metadata->set('is_minor', false);
                     $history_metadata->set('user_id', $this->getUser()->getId());
                     $history_metadata->setComment('Synchronize summit to associated hut');
                     $history_metadata->save();
                     $summit_doc->set('name', $hut_doc->get('name'));
                     $summit_doc->set('lon', $hut_doc->get('lon'));
                     $summit_doc->set('lat', $hut_doc->get('lat'));
                     $summit_doc->set('elevation', $hut_doc->get('elevation'));
                     $summit_doc->save();
                     $conn->commit();
                     if ($geom_changed) {
                         // TODO idea here is to call the refreshGeoAssociations
                         // from summitsActions but we can't call it. In order not to
                         // change the whole mechanism for refreshAssociations, we kinda copy paste
                         // the function here, but this should be improved / factorized
                         // refer to it to understand what is done here
                         $associated_routes = Association::findAllAssociatedDocs($id, array('id', 'geom_wkt'), 'sr');
                         if (count($associated_routes)) {
                             $geoassociations = GeoAssociation::findAllAssociations($id, null, 'main');
                             foreach ($associated_routes as $route) {
                                 $i = $route['id'];
                                 if (!$route['geom_wkt']) {
                                     $nb_created = GeoAssociation::replicateGeoAssociations($geoassociations, $i, true, true);
                                     $this->clearCache('routes', $i, false, 'view');
                                     $associated_outings = Association::findAllAssociatedDocs($i, array('id', 'geom_wkt'), 'ro');
                                     if (count($associated_outings)) {
                                         $geoassociations2 = GeoAssociation::findAllAssociations($i, null, 'main');
                                         foreach ($associated_outings as $outing) {
                                             $j = $outing['id'];
                                             if (!$outing['geom_wkt']) {
                                                 $nb_created = GeoAssociation::replicateGeoAssociations($geoassociations2, $j, true, false);
                                                 c2cTools::log("created {$nb_created} geo associations for outing N° {$j}");
                                                 $this->clearCache('outings', $j, false, 'view');
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 } catch (Exception $e) {
                     $conn->rollback();
                     // TODO It is ok to signal the failure, but anyway, the hut doc has been updated
                     // so there is much room for improvement here :)
                     return $this->setErrorAndRedirect("Failed to synchronize summit", '@document_edit?module=huts&id=' . $hut_doc->getId() . '&lang=' . $hut_doc->getCulture());
                 }
             }
         }
         parent::endEdit();
         // redirect to document view
     }
 }
 protected function endEdit()
 {
     //Test if form is submitted or not
     if ($this->success) {
         // if this is the first version of the outing (aka creation)
         // set a flash message to encourage to also enhance the corresponding route
         if (is_null($this->document->getVersion())) {
             $this->setNotice('thanks for new outing');
         }
         // try to perform association with linked_doc (if pertinent)
         $associated_id = $this->getRequestParameter('document_id');
         $user_id = $this->getUser()->getId();
         $id = $this->document->get('id');
         if ($this->new_document && $associated_id || $associated_id && !Association::find($associated_id, $id)) {
             // we must get this document's module (site or route ?)
             $associated_doc = Document::find('Document', $associated_id, array('module'));
             if ($associated_doc) {
                 $associated_module = $associated_doc->get('module');
                 $a = new Association();
                 if ($associated_module == 'routes') {
                     $a->doSaveWithValues($associated_id, $id, 'ro', $user_id);
                     // main, linked, type
                     // clear cache of associated route ...
                     $this->clearCache('routes', $associated_id, false, 'view');
                 } elseif ($associated_module == 'sites') {
                     $a->doSaveWithValues($associated_id, $id, 'to', $user_id);
                     // main, linked, type
                     // clear cache of associated site ...
                     $this->clearCache('sites', $associated_id, false, 'view');
                 }
                 // here if we have created a new document and if $this->document->get('geom_wkt') is null, then use associated doc geom associations:
                 // this allows us to filter on ranges even if no GPX is uploaded
                 if ($this->new_document && $associated_id && !$this->document->get('geom_wkt')) {
                     // get all associated regions (only regions, countries, depts, no maps !) with this summit:
                     $associations = GeoAssociation::findAllAssociations($associated_id, array('dr', 'dc', 'dd', 'dv'));
                     // replicate them with outing_id instead of (route_id or site_id):
                     foreach ($associations as $ea) {
                         $areas_id = $ea->get('linked_id');
                         $a = new GeoAssociation();
                         $a->doSaveWithValues($id, $areas_id, $ea->get('type'));
                         // clear cache of associated areas
                         $this->clearCache('areas', $areas_id, false, 'view');
                     }
                 }
             }
         }
         // create also association with current user.
         if ($this->new_document) {
             $uo = new Association();
             $uo->doSaveWithValues($user_id, $id, 'uo', $user_id);
             // main, linked, type
             // clear cache of current user
             $this->clearCache('users', $user_id, false, 'view');
         }
         // create association with MW contest article, if requested
         if ($this->new_document) {
             $mw_contest_associate = $this->getRequestParameter('mw_contest_associate');
             if ($mw_contest_associate) {
                 $mw_article_id = sfConfig::get('app_mw_contest_id');
                 $oc = new Association();
                 $oc->doSaveWithValues($id, $mw_article_id, 'oc', $user_id);
             }
         }
         parent::endEdit();
         // redirect to document view
     }
 }
 protected function endEdit()
 {
     if ($this->success) {
         // create also association with current user (only if this is a personal article)
         if ($this->new_document && $this->document->get('article_type') == 2) {
             $user_id = $this->getUser()->getId();
             $uc = new Association();
             $uc->doSaveWithValues($user_id, $this->document->get('id'), 'uc', $user_id);
             // main, linked, type
         }
         parent::endEdit();
         // redirect to document view
     }
 }