/** * Changes the association order for associations of same type (eg summit-summit) * It should only be used for ss, pp and tt associations, but there is probably * no real problem if other associations are inverted */ public function executeInvertAssociation() { $user = $this->getUser(); $user_id = $user->getId(); $is_moderator = $user->hasCredential('moderator'); $type = $this->getRequestParameter('type'); $main_id = $this->getRequestParameter('main_id'); $linked_id = $this->getRequestParameter('linked_id'); // check if session timed out if (!$user_id) { return $this->setErrorAndRedirect('Session is over. Please login again.', $this->getRequest()->getReferer()); } // only moderators can perform such actions if (!$is_moderator) { return $this->setErrorAndRedirect('You do not have enough credentials to perform this operation', $this->getRequest()->getReferer()); } // we check that the association type really exists and that the two // documents are from the same model if (substr($type, 0, 1) != substr($type, -1) || !in_array($type, sfConfig::get('app_associations_types'))) { return $this->ajax_feedback('Wrong association type'); } // check that association exists in database $models = c2cTools::Type2Models($type); $model = $models['main']; $module = c2cTools::model2module($model); $a = Association::find($main_id, $linked_id, $type); // strict search if (!$a) { return $this->setErrorAndRedirect('Operation not allowed', $this->getRequest()->getReferer()); } // invert association $conn = sfDoctrine::Connection(); try { $conn->beginTransaction(); $a->main_id = $linked_id; $a->linked_id = $main_id; $a->save(); $al1 = new AssociationLog(); $al1->main_id = $main_id; $al1->linked_id = $linked_id; $al1->type = $type; $al1->user_id = $user_id; $al1->is_creation = 'false'; $al1->save(); $al1 = new AssociationLog(); $al1->main_id = $linked_id; $al1->linked_id = $main_id; $al1->type = $type; $al1->user_id = $user_id; $al1->is_creation = 'true'; $al1->save(); $conn->commit(); } catch (exception $e) { $conn->rollback(); c2cTools::log("executeInvertAssociation() : invertion failed ({$main_id}, {$linked_id}, {$type}, {$user_id}) - rollback"); return $this->ajax_feedback('Association invertion failed'); } // remove cache and force page reload $this->clearCache($module, $main_id, false, 'view'); $this->clearCache($module, $linked_id, false, 'view'); return $this->setNoticeAndRedirect('Association inverted', $this->getRequest()->getReferer()); }
public function doSaveWithValues($main_id, $linked_id, $type, $user_id) { $conn = sfDoctrine::Connection(); try { $conn->beginTransaction(); // we create the association: $this->main_id = $main_id; $this->linked_id = $linked_id; $this->type = $type; $this->save(); // and we log this: $al = new AssociationLog(); $al->main_id = $main_id; $al->linked_id = $linked_id; $al->type = $type; $al->user_id = $user_id; $al->is_creation = true; $al->save(); $conn->commit(); // send an email to moderators if a picture is associated to a book if ($type == 'bi') { try { // retrieve moderators email $moderator_id = sfConfig::get('app_moderator_user_id'); // currently send to topo-fr only $conn->beginTransaction(); $rows = $conn->standaloneQuery('SELECT email FROM app_users_private_data d WHERE id = ' . $moderator_id)->fetchAll(); $conn->commit(); $email_recipient = $rows[0]['email']; $mail = new sfMail(); $mail->setCharset('utf-8'); // definition of the required parameters $mail->setSender(sfConfig::get('app_outgoing_emails_sender')); $mail->setFrom(sfConfig::get('app_outgoing_emails_from')); $mail->addReplyTo(sfConfig::get('app_outgoing_emails_reply_to')); $mail->addAddress($email_recipient); $mail->setSubject('New image associated to book'); $mail->setContentType('text/html'); $server = $_SERVER['SERVER_NAME']; $body = "<p>A <a href=\"http://{$server}/images/{$linked_id}\">new image</a> has been associated to <a href=\"http://{$server}/books/{$main_id}\">book {$main_id}</a>.</p>" . "<p>The image may require a copyright license. If so, please ensure that:</p>" . "<ul>" . "<li>the owner is correctly acknowledged in the author field;</li>" . "<li>the image is not too big (max 800px width or height).</li>" . "</ul>"; $mail->setBody($body); // send the email $mail->send(); } catch (exception $e) { $conn->rollback(); c2cTools::log("Association::doSaveWithValues({$main_id}, {$linked_id}, {$type}, {$user_id}) failed sending email for image associated to book"); } } return true; } catch (exception $e) { $conn->rollback(); c2cTools::log("Association::doSaveWithValues({$main_id}, {$linked_id}, {$type}, {$user_id}) failed - rollback"); return false; } }