示例#1
0
 public function executeSuggest()
 {
     sfLoader::loadHelpers(array('General', 'Url'));
     $model = $this->hasRequestParameter('module_id') ? c2cTools::module2model(sfConfig::get('app_modules_list')[$this->getRequestParameter('module_id')]) : $this->model_class;
     if (!in_array($model, array('Hut', 'Image', 'Parking', 'Product', 'Site', 'Summit', 'User'))) {
         return $this->setErrorAndRedirect('Invalid request');
     }
     $exclude = $this->hasRequestParameter('exclude') ? array_map('intval', explode(',', $this->getRequestParameter('exclude'))) : null;
     if ($model == 'User') {
         // get  user id
         $userid = intval($this->getRequestParameter('id'));
         if ($userid <= 0) {
             return $this->setErrorAndRedirect('Invalid request');
         }
         $items = Language::getTheBest(User::getFriends($userid, $exclude), $model);
     } else {
         // get lat, lon and excluded values
         $lon = floatval($this->getRequestParameter('lon'));
         $lat = floatval($this->getRequestParameter('lat'));
         if (!$lon || !$lat) {
             return $this->setErrorAndRedirect('Invalid request');
         }
         $items = Language::getTheBest(Document::getNearest($lon, $lat, $model, $exclude), $model);
     }
     $output = [];
     foreach ($items as $item) {
         $routing_rule = $model == 'User' ? '@document_by_id_lang' : '@document_by_id_lang_slug';
         array_push($output, array('id' => $item['id'], 'name' => $item[$model . 'I18n'][0]['name'], 'url' => url_for($routing_rule . '?module=' . c2cTools::Model2Module($model) . '&id=' . $item['id'] . '&lang=' . $item[$model . 'I18n'][0]['culture'] . '&slug=' . make_slug($item[$model . 'I18n'][0]['name']))));
     }
     return $this->renderText(json_encode($output));
 }
示例#2
0
 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);
 }