/** * Executes Quickcreate action. * if id exists, return just the name of summit, * if id = 0 and name provided, quickly create summit whose name was given * * routes/quickcreate?link=summit_id // in action, specify that a link received for Route model is mandatorily a summit one. * plus return whole list of associated routes to this summit with created one = selected */ public function executeQuickcreate() { sfLoader::loadHelpers(array('Url', 'Tag', 'Form')); $name = $this->getRequestParameter('name'); // name string = title of document to create $link_with = $this->getRequestParameter('link_with'); // eventually, a link parameter might be received $model = $this->model_class; // type of document to create $lcmodel = strtolower($model); $id_string = $lcmodel . '_id'; // convention : // if we are looking for a summit, id key is summit_id // if we are looking for a route, id key is route_id $id = $this->getRequestParameter($id_string); $user = $this->getUser(); $lang = $user->getCulture(); $module = $lcmodel . 's'; // user must be logged if (!$user->isConnected()) { return $this->ajax_feedback('Your session is over. Please login again.'); } if ($id) { // if given id is not null, then do not create document, just fetch it : $document = Document::find($model, $id, array('id')); //hydrate best name : $prefered_cultures = $this->getUser()->getCulturesForDocuments(); $document->setBestCulture($prefered_cultures); } else { // here, we must create doc. // useful protection : if (strlen($name) < sfConfig::get('app_autocomplete_min_chars')) { return $this->ajax_feedback('Could not create document with so short a title'); } $document = new $model(); $document->setCulture($lang); $document->set('name', $name); $document->doSaveWithMetadata($user->getId(), false, "Quick creation of a {$lcmodel}"); $id = $document->get('id'); // a link request only occurs when a new document has to be quickly created. if ($link_with) { switch ($model) { case 'Route': $summit = Document::find('Summit', $link_with, array('id')); if (!$summit) { return $this->ajax_feedback('Summit not found'); } // association has to be done in SummitRoute // link route $id with summit $link_with try { $sr = new Association(); $sr->set('main_id', $link_with); $sr->set('linked_id', $id); $sr->set('type', 'sr'); $sr->save(); } catch (exception $e) { // fixme : catch exception. } break; default: // for the moment, we do not manage other associations than summits with route upon quick document creations. break; } } } // format the response and send back : if ($link_with) { // return whole list of associated routes to this summit ($link_with) with created one = selected one $routes = Association::findAllWithBestName($link_with, $this->getUser()->getCulturesForDocuments(), 'sr'); $output = ''; foreach ($routes as $route) { $route_id = $route['id']; $output .= $route_id == $id ? '<option value="' . $route['id'] . '" selected="selected">' . $route['name'] . '</option>' : '<option value="' . $route['id'] . '">' . $route['name'] . '</option>'; } return $this->renderText($output); } else { return $this->renderText(input_hidden_tag($id_string, $id) . link_to($document->get('name'), "@document_by_id?module={$module}&id={$id}")); } }