$asso->doSaveWithValues($doc['id'], $image_data['id'], $association_type, $TOPO_MODERATOR_USER_ID); } $stat_associations_required++; } $image_ids[$tag[1]] = $image_data['id']; } else { // no corresponding id, the tag is incorrect and must not be modified. but a warning should be notified $stat_docs_with_invalid_references[] = $doc['id'] . ' (' . $doc['culture'] . ' - ' . $doc['name'] . ') http://' . $SERVER_NAME . '/' . strtolower($table) . 's' . '/' . $doc['id'] . '/' . $doc['culture'] . "\n"; } } // replace image tags $conn = sfDoctrine::Connection(); $db_doc = Document::find($table, $doc['id']); if (!$DRY_RUN) { $conn->beginTransaction(); $history_metadata = new HistoryMetadata(); $history_metadata->setComment('Updated image tags'); $history_metadata->set('is_minor', true); $history_metadata->set('user_id', $TOPO_MODERATOR_USER_ID); $history_metadata->save(); $db_doc->setCulture($doc['culture']); } foreach ($fields as $field) { $tag_data = $tags_for_field[$field]; $text = $doc[$field]; foreach ($tag_data as $tag_idx) { $references_to_modify++; $tag = $tags[$tag_idx]; // do not care about images with invalid reference if (empty($image_ids[$tag[1]])) { continue;
function import_new_geometry() { global $conn, $comment, $is_map, $is_new_document, $culture, $name, $map_editor, $map_scale, $map_code, $keepassociations, $region_type, $newgeomtext, $no_oldgeom, $document_id, $a_type, $fullwipe, $oldgeom, $newgeom, $prgmsg; info("Importing the new geometry...\n"); try { $conn->beginTransaction(); $history_metadata = new HistoryMetadata(); $history_metadata->setComment(isset($comment) ? $comment : ($is_new_document ? 'Imported new ' . ($is_map ? 'map' : 'area') : 'Updated geometry')); $history_metadata->set('is_minor', false); $history_metadata->set('user_id', 2); // C2C user $history_metadata->save(); if ($is_new_document) { $doc = $is_map ? new Map() : new Area(); $doc->setCulture($culture); $doc->set('name', $name); if ($is_map) { $doc->set('editor', $map_editor); $doc->set('scale', $map_scale); $doc->set('code', $map_code); } else { $doc->set('area_type', $region_type); } } else { $doc = Document::find($is_map ? 'Map' : 'Area', $document_id); $name = $doc->get('name'); if (!$is_map) { $region_type = $doc->get('area_type'); } } $doc->set('geom_wkt', $newgeomtext); $doc->save(); info("Geometry uploaded.\n"); if ($keepassociations) { exit; } if ($is_new_document) { $document_id = $doc->get('id'); } // $conn->commit(); // $conn->beginTransaction(); if ($is_map) { $a_type = 'dm'; } else { switch ($region_type) { case 1: // range $a_type = 'dr'; break; case 2: // country $a_type = 'dc'; break; case 3: // dept $a_type = 'dd'; break; } } // Some explanation for the following queries: // - && operator is used to limit docs to the one whose bouding boxes overlap (it uses spatial index) // - ST_Within and ST_Intersects are used to work on the actual geometries // - ST_Within apears to be faster, so we use it for 'points' (like summits, huts etc), but we have // to use ST_Intersects for 'geometries' (like outings, maps etc) // - we only use a buffer of 200m for areas (because of boundaries imprecision), but not for maps // - areas are not linked together // - maps are not linked to outings, users, and other maps // if it is a new document, we create geoassociations for the whole geometry // but if its an updated one, we only create geoassociations for the part of the // new geometry that does not intersect with the old one if ($is_new_document || $no_oldgeom || $fullwipe) { // retrieve geom from the database $geomquery = '(SELECT geom FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $geomqueryb = '(SELECT buffer(geom, 200) FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $queryparam = array($document_id, $document_id); } else { $queryparam = array(); $creategeom = $conn->standaloneQuery("SELECT ST_Difference('{$newgeom}', '{$oldgeom}')")->fetchAll(); $creategeomb = $conn->standaloneQuery("SELECT ST_Difference(buffer('{$newgeom}', 200), buffer('{$oldgeom}', 200))")->fetchAll(); $creategeom = $creategeom[0]['st_difference']; $creategeomb = $creategeomb[0]['st_difference']; $geomquery = "'{$creategeom}'"; $geomqueryb = "'{$creategeomb}'"; } // for maps, we don't use buffer at all if ($is_map) { $geomqueryb = $geomquery; } $queries = array(); // point geometry $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomqueryb} " . "AND ST_Within(geom, {$geomqueryb}) " . "AND module IN('summits', 'huts', 'sites', 'parkings', 'products', 'portals', 'images'" . ($is_map ? '' : ", 'users'") . ')', $queryparam); // multipoint geometry $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomqueryb} " . "AND ST_Intersects(geom, {$geomqueryb}) AND module" . ($is_map ? "='routes'" : " IN('routes', 'outings')"), $queryparam); // for maps areas associations, we always compute 'full wipe', without buffer $geomquery = '(SELECT geom FROM ' . ($is_map ? 'maps' : 'areas') . ' WHERE id=?)'; $queries[] = array("SELECT id, module FROM documents WHERE geom && {$geomquery} " . "AND ST_Intersects(geom, {$geomquery}) AND module='" . ($is_map ? 'areas' : 'maps') . "'", array($document_id, $document_id)); $results_a = array(); foreach ($queries as $query) { $results_a[] = $conn->standaloneQuery($query[0], $query[1])->fetchAll(); } $results = array(); foreach ($results_a as $results_set) { foreach ($results_set as $d) { $results[] = $d; } } $prgmsg = "Create new associations..."; info($prgmsg); $tot = count($results); foreach ($results as $i => $d) { progression($i, $tot); // Apparently in some cases, we ar trying to create associations that // already exist, so we check first if (!GeoAssociation::find($document_id, $d['id'], null, false)) { $a = new GeoAssociation(); $created[$d['module']] = isset($created[$d['module']]) ? $created[$d['module']] + 1 : 1; // for map - area geoassociations, links must not be dm but dr, dc, dd... if ($is_map && $d['module'] === 'areas') { $area = Document::find('Area', $d['id']); switch ($area->get('area_type')) { case 1: // range $t_a_type = 'dr'; break; case 2: // country $t_a_type = 'dc'; break; case 3: // dept $t_a_type = 'dd'; break; } $a->doSaveWithValues($document_id, $d['id'], $t_a_type); } else { $a->doSaveWithValues($d['id'], $document_id, $a_type); } } // inherited docs: we add geoassociations for the 'inherited docs' from sites, routes and summits // but not if they already have a geometry (gps track) switch ($d['module']) { case 'sites': case 'routes': if ($is_map) { break; } // we do not link maps to outings $associated_outings = Association::findAllAssociatedDocs($d['id'], array('id', 'geom_wkt'), $d['module'] === 'routes' ? 'ro' : 'to'); if (count($associated_outings)) { foreach ($associated_outings as $outing) { if (!$outing['geom_wkt'] && GeoAssociation::find($outing['id'], $document_id, $a_type) === false) { // we create geoassociation (if it already existed, it has been deleted before in the script) $a = new GeoAssociation(); $a->doSaveWithValues($outing['id'], $document_id, $a_type); $created['outings'] = isset($created['outings']) ? $created['outings'] + 1 : 1; } } } break; case 'summits': // if summit is of type raid, we should not try to update its routes and outings summit_type=5 $summit = Document::find('Summit', $d['id']); if ($summit->get('summit_type') == 5) { break; } $associated_routes = Association::findAllAssociatedDocs($d['id'], array('id', 'geom_wkt'), 'sr'); if (count($associated_routes)) { foreach ($associated_routes as $route) { $i = $route['id']; if (!$route['geom_wkt'] && GeoAssociation::find($i, $document_id, $a_type) === false) { $a = new GeoAssociation(); $a->doSaveWithValues($i, $document_id, $a_type); $created['routes'] = isset($created['routes']) ? $created['routes'] + 1 : 1; if (!$is_map) { $associated_outings = Association::findAllAssociatedDocs($i, array('id', 'geom_wkt'), 'ro'); if (count($associated_outings)) { foreach ($associated_outings as $outing) { $j = $outing['id']; if (!$outing['geom_wkt'] && GeoAssociation::find($j, $document_id, $a_type) === false) { $a = new GeoAssociation(); $a->doSaveWithValues($j, $document_id, $a_type); $created['outings'] = isset($created['outings']) ? $created['outings'] + 1 : 1; } } } } } } } break; } } info("\n"); $conn->commit(); if (isset($created)) { associations_result($created); } if ($is_new_document) { info('Added new ' . ($is_map ? 'map' : 'area') . " {$name} ({$document_id})\n"); } else { info('Updated ' . ($is_map ? 'map' : 'area') . " ({$document_id})\n"); } } catch (Exception $e) { $conn->rollback(); throw $e; } }
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}"); }
public function executeManageimages() { if (!$this->getUser()->isConnected()) { $referer = $this->getRequest()->getReferer(); $this->setErrorAndRedirect('You need to login to access this page', $referer); } $user_id = $this->getUser()->getId(); // logged user id $this->pager = new c2cDoctrinePager('Image', c2cTools::mobileVersion() ? sfConfig::get('app_list_mobile_maxline_number') : sfConfig::get('app_list_maxline_number')); $q = $this->pager->getQuery(); $q->select('i.id, i.filename, i.image_type, ii.name, ii.culture')->from('Image i')->leftJoin('i.associations a ON i.id = a.linked_id')->leftJoin('i.ImageI18n ii')->leftJoin('i.versions v')->leftJoin('v.history_metadata hm'); $where = 'i.image_type = 2 AND v.version = 1 AND hm.user_id = ?'; $document_type = $this->getRequestParameter('dtyp'); if (!empty($document_type)) { if ($document_type <= 1) { $types = array('ai', 'mi', 'bi', 'hi', 'pi', 'ri', 'ti', 'si'); } else { $types = array('oi', 'ui'); } $where .= " AND a.type IN ( '" . implode("', '", $types) . "' )"; } else { $document_type = $this->getRequestParameter('ctyp'); if (!empty($document_type)) { $q->leftJoin('a.Article c'); if ($document_type <= 1) { $document_type = 1; } else { $document_type = 2; } $where .= " AND a.type = 'ci' AND c.article_type = {$document_type}"; } } $q->where($where, array($user_id)); $q->orderBy('i.id DESC'); $page = $this->getRequestParameter('page', 1); $this->pager->setPage($page); $this->pager->init(); $this->page = $page; if ($this->getRequest()->getMethod() == sfRequest::POST) { // images management $switch = $this->getRequestParameter('switch'); $lang = $this->getUser()->getCulture(); if (empty($switch)) { return $this->setNoticeAndRedirect('No image has been edited', "/users/manageimages?module=users&page={$page}"); } $conn = sfDoctrine::Connection(); $conn->beginTransaction(); $history_metadata = new HistoryMetadata(); $history_metadata->setComment('Switch to collaborative license'); $history_metadata->set('is_minor', true); $history_metadata->set('user_id', $user_id); $history_metadata->save(); foreach ($switch as $image_id) { // verify id corresponds to an image created by the user $img = Doctrine_Query::create()->select('i.id')->from('Image i')->leftJoin('i.versions v')->leftJoin('v.history_metadata hm')->where('v.version = 1 AND hm.user_id = ? AND i.id = ?', array($user_id, $image_id))->execute(); if (empty($img)) { $conn->rollback(); return $this->setNoticeAndRedirect('You do not have the right to change the license of theses images', "/users/manageimages?module=users&page={$page}"); } $db_doc = Document::find('Image', $image_id); $db_doc->set('image_type', 1); $db_doc->save(); // clear cache $this->clearCache('images', $image_id, false, 'view'); $associated_docs = Association::findAllAssociatedDocs($image_id, array('id', 'module')); foreach ($associated_docs as $doc) { // clear their view cache $this->clearCache($doc['module'], $doc['id'], false, 'view'); } } // apply modifications if everything went fine $conn->commit(); return $this->setNoticeAndRedirect('Your images have been successfully updated', "/users/manageimages?module=users&page={$page}"); } else { // display form $this->setPageTitle($this->__('User image management')); } }
protected function doMerge($from_id, $document_from, $to_id, $document_to) { // fetch associated documents before doing the merging as associations will be transferred $associations = Association::findAllAssociations($from_id); parent::doMerge($from_id, $document_from, $to_id, $document_to); // search documents in which from is inserted, and replace the insertion with to foreach ($associations as $a) { $check_id = $a['main_id'] == $from_id ? $a['linked_id'] : ($check_id = $a['main_id']); $check_model = c2cTools::Letter2Model(substr($a['type'], 0, 1)); $check_module = c2cTools::Model2Module($check_model); $check_doc = Document::find($check_model, $check_id); $fields = sfConfig::get('mod_images_bbcode_fields_' . $check_module); // clear linked doc cache $this->clearCache($check_module, $check_id); $languages = $check_doc->getLanguages(); foreach ($languages as $language) { $modified = false; $conn = sfDoctrine::Connection(); $conn->beginTransaction(); $check_doc->setCulture($language); foreach ($fields as $field) { $text = $check_doc[$field]; $edited = preg_replace('#(\\[img=\\s*)' . $from_id . '([\\w\\s]*\\].*?\\[/img\\])\\n?#is', '${1}' . $to_id . '${2}', $text); $edited = preg_replace('#(\\[img=\\s*)' . $from_id . '([\\w\\s]*\\/\\])\\n?#is', '${1}' . $to_id . '${2}', $edited); if ($edited != $text) { $modified = true; $check_doc->set($field, $edited); } } if ($modified) { $history_metadata = new HistoryMetadata(); $history_metadata->setComment('Updated image tags'); $history_metadata->set('is_minor', true); $history_metadata->set('user_id', sfConfig::get('app_moderator_user_id')); $history_metadata->save(); c2cTools::log('After merge of image ' . $from_id . ' into ' . $to_id . ': update image tag for ' . strtolower($check_model) . ' ' . $check_id . ' (' . $language . ')'); $check_doc->save(); $conn->commit(); } else { $conn->rollback(); } } } // clear images lists and whatsnew cache $this->clearCache('images', 0, true); }
/** * Saves new data for the document with its metadata. * */ public function doSaveWithMetadata($user_id, $is_minor = false, $comment = null) { $conn = sfDoctrine::Connection(); try { $conn->beginTransaction(); // history metadata saving here $history_metadata = new HistoryMetadata(); $history_metadata->setComment($comment); $history_metadata->set('is_minor', $is_minor); $history_metadata->set('user_id', $user_id); // data saving must be done in this order : $history_metadata->save(); $this->save(); $conn->commit(); return true; } catch (exception $e) { $conn->rollback(); throw $e; } }