Esempio n. 1
0
 /**
  * Executes remove document association
  */
 public function executeRemoveAssociation()
 {
     $user = $this->getUser();
     $user_id = $user->getId();
     $is_moderator = $user->hasCredential(sfConfig::get('app_credentials_moderator'));
     $type = $this->getRequestParameter('type');
     $main_id = $this->getRequestParameter('main_' . $type . '_id');
     $linked_id = $this->getRequestParameter('linked_id');
     $mode = $this->getRequestParameter('mode');
     $strict = $this->getRequestParameter('strict', 1);
     // whether 'remove action' should be strictly restrained to main and linked or reversed.
     $icon = $this->getRequestParameter('icon');
     // if session is time-over
     if (!$user_id) {
         return $this->ajax_feedback('Session is over. Please login again.');
     }
     // only moderators can unlink documents
     if (!$is_moderator) {
         return $this->ajax_feedback('You do not have enough credentials to perform this operation');
     }
     // association cannot be created/deleted with self.
     if ($main_id == $linked_id) {
         return $this->ajax_feedback('A document can not be linked to itself');
     }
     // We check that this association type really exists
     // for that, yaml is preferable over a db request, since all associations types are not allowed for quick associations
     if (!in_array($type, sfConfig::get('app_associations_types'))) {
         return $this->ajax_feedback('Wrong association type');
     }
     $models = c2cTools::Type2Models($type);
     $main_model = $models['main'];
     $linked_model = $models['linked'];
     $main = Document::find($main_model, $main_id, array('id', 'module'));
     if (!$main) {
         return $this->ajax_feedback('Document does not exist');
     }
     // check that linked doc exists:
     // FIXME : combine request with main doc by looking only in documents table and check 'module' field is correct ?
     $linked = Document::find($linked_model, $linked_id, $linked_model == 'Article' ? array('id', 'article_type') : array('id'));
     if (!$linked) {
         return $this->ajax_feedback('Document does not exist');
     }
     $main_module = c2cTools::model2module($main_model);
     // check whether association has already been done or not
     $a = Association::find($main_id, $linked_id, $type, false);
     // false means not strict search (main and linked can be reversed)
     if ($a) {
         // check that user is moderator is done in security.yml
         // For a summit route association or a user outing association,
         // we must prevent the deletion of the last associated doc
         // For outings, we must check that at least one route or site will still be associated
         if (($type == 'sr' || $type == 'uo') && Association::countMains($linked_id, $type) == 1 || ($type == 'ro' || $type == 'to') && Association::countMains($linked_id, array('ro', 'to')) == 1) {
             return $this->ajax_feedback('Operation forbidden: last association');
         } else {
             // delete association in Database
             $conn = sfDoctrine::Connection();
             try {
                 $conn->beginTransaction();
                 $a->delete();
                 $al = new AssociationLog();
                 $al->main_id = $main_id;
                 $al->linked_id = $linked_id;
                 $al->type = $type;
                 $al->user_id = $user_id;
                 $al->is_creation = 'false';
                 $al->save();
                 $conn->commit();
             } catch (exception $e) {
                 $conn->rollback();
                 c2cTools::log("executeRemoveAssociation() : Association deletion + log failed ({$main_id}, {$linked_id}, {$type}, {$user_id}) - rollback");
                 return $this->ajax_feedback('Association deletion failed');
             }
         }
     } else {
         return $this->ajax_feedback('Operation not allowed');
     }
     // view action cache clearing (without whatsnew), since association is not logged in app_history_metadata and associations only appear on view:
     $this->clearCache($main_module, $main_id, false, 'view');
     $this->clearCache(c2cTools::model2module($linked_model), $linked_id, false, 'view');
     // for some cases (typically unlinking an image), we reload the doc
     // rather than removing a list entry
     if ($this->hasRequestParameter('reload')) {
         return $this->setNoticeAndRedirect('Image has been unlinked', $this->getRequest()->getReferer() . '#images');
     } else {
         return $this->renderText('');
     }
 }
Esempio n. 2
0
 public function executeAddroute()
 {
     $id = $this->getRequestParameter('document_id');
     // check if a summit is already associated to hut. if not, create it
     $create_summit = Association::countMains($id, 'sh') == 0;
     if ($create_summit) {
         $document = Document::find('Hut', $id, array('elevation', 'geom_wkt'));
         $conn = sfDoctrine::Connection();
         try {
             $conn->beginTransaction();
             // create first version of document, with culture and geometry of hut document
             $hut_elevation = $document['elevation'];
             $hut_lat = $document['lat'];
             $hut_lon = $document['lon'];
             $hut_culture = $document->getCulture();
             $hut_name = $document['name'];
             $history_metadata = new HistoryMetadata();
             $history_metadata->setComment($this->__('Created summit synchronized with hut for access'));
             $history_metadata->set('is_minor', false);
             $history_metadata->set('user_id', 2);
             // C2C user
             $history_metadata->save();
             $summit = new Summit();
             $summit->setCulture($hut_culture);
             $summit->set('name', $hut_name);
             $summit->set('elevation', $hut_elevation);
             $summit->set('summit_type', 100);
             // set summit type to ' hut'
             $summit->set('lat', $hut_lat);
             $summit->set('lon', $hut_lon);
             $summit->save();
             $conn->commit();
             // add others culture versions
             foreach ($document->get('HutI18n') as $i18n) {
                 $culture = $i18n->getCulture();
                 if ($culture != $hut_culture) {
                     $conn->beginTransaction();
                     $hut_name = $i18n->getName();
                     $history_metadata = new HistoryMetadata();
                     $history_metadata->setComment($this->__('Created summit synchronized with hut for access'));
                     $history_metadata->set('is_minor', false);
                     $history_metadata->set('user_id', 2);
                     // C2C user
                     $history_metadata->save();
                     $summit->setCulture($culture);
                     $summit->set('name', $hut_name);
                     $summit->save();
                     $conn->commit();
                 }
             }
         } catch (Exception $e) {
             $conn->rollback();
             return $this->setErrorAndRedirect($this->__('Failed to create synchronized summit'), "routes/edit?link={$summit_id}");
         }
         $summit_id = $summit->get('id');
         // get all associated regions (3+maps) with this hut:
         $associations = GeoAssociation::findAllAssociations($id, array('dr', 'dc', 'dd', 'dv', 'dm'));
         // replicate them with summit_id instead of id:
         foreach ($associations as $ea) {
             $a = new GeoAssociation();
             $a->doSaveWithValues($summit_id, $ea->get('linked_id'), $ea->get('type'));
         }
         // associate hut to summit
         $asso = new Association();
         $asso->doSaveWithValues($summit_id, $id, 'sh', 2);
         // C2C user
     } else {
         $associations = Association::findAllAssociations($id, 'sh');
         $summit_id = $associations[0]->get('main_id');
     }
     $this->clearCache('huts', $id);
     return $this->redirect("routes/edit?link={$summit_id}");
 }