Unverified Commit 6f4eba9c authored by Sebastian Castro's avatar Sebastian Castro
Browse files

Fix missing contributions or element (broken association)

parent f7c240d0
......@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\Request;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\CoreBundle\Form\UserProfileType;
use Symfony\Component\Form\FormError;
use Biopen\GeoDirectoryBundle\Document\Coordinates;
use Biopen\GeoDirectoryBundle\Document\Coordinates;
class UserController extends GoGoController
{
......@@ -25,14 +25,14 @@ class UserController extends GoGoController
$userEmail = $user->getEmail();
$elementsOwned = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->findElementsOwnedBy($userEmail);
$elementsOwned = array_filter($elementsOwned->toArray(), function($element) use ($userEmail) {
return !$element->isPending() || $element->getCurrContribution()->getUserEmail() != $userEmail;
$elementsOwned = array_filter($elementsOwned->toArray(), function($element) use ($userEmail) {
return !$element->isPending() || $element->getCurrContribution()->getUserEmail() != $userEmail;
});
$allContribs = $dm->getRepository('BiopenGeoDirectoryBundle:UserInteractionContribution')->findByUserEmail($userEmail);
$allContribs = array_filter($allContribs, function($interaction) {
return in_array($interaction->getType(), [InteractionType::Add, InteractionType::Edit]);
$allContribs = array_filter($allContribs, function($interaction) {
return in_array($interaction->getType(), [InteractionType::Add, InteractionType::Edit]);
});
$elementsUserHaveContributed = [];
$pendingContribs = [];
......@@ -40,13 +40,16 @@ class UserController extends GoGoController
if ($contrib->getStatus() == null) $pendingContribs[] = $contrib;
if ($contrib->countAsValidContributionFrom($userEmail)
&& !in_array($contrib->getElement(), $elementsUserHaveContributed)
&& !in_array($contrib->getElement(), $elementsOwned))
array_push($elementsUserHaveContributed, $contrib->getElement());
&& !in_array($contrib->getElement(), $elementsUserHaveContributed)
&& !in_array($contrib->getElement(), $elementsOwned)) {
try { if ($contrib->getElement()->getName()) {
array_push($elementsUserHaveContributed, $contrib->getElement());
}} catch (\Exception $e) {}
}
}
usort($pendingContribs, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
usort($allContribs, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
usort($allContribs, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
return $this->render('@BiopenCoreBundle/user/contributions/my-contributions.html.twig', array(
'elementsOwned' => $elementsOwned,
......@@ -62,7 +65,7 @@ class UserController extends GoGoController
$userEmail = $user->getEmail();
$votes = $dm->getRepository('BiopenGeoDirectoryBundle:UserInteractionVote')->findByUserEmail($userEmail);
usort($votes, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
usort($votes, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
return $this->render('@BiopenCoreBundle/user/contributions/votes.html.twig', array('votes' => $votes));
}
......@@ -74,7 +77,7 @@ class UserController extends GoGoController
$userEmail = $user->getEmail();
$reports = $dm->getRepository('BiopenGeoDirectoryBundle:UserInteractionReport')->findByUserEmail($userEmail);
usort($reports, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
usort($reports, function ($a, $b) { return $b->getTimestamp() - $a->getTimestamp(); });
return $this->render('@BiopenCoreBundle/user/contributions/reports.html.twig', array('reports' => $reports));
}
......@@ -82,23 +85,23 @@ class UserController extends GoGoController
public function becomeOwnerAction($id, Request $request)
{
$dm = $this->get('doctrine_mongodb')->getManager();
$element = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->find($id);
$element = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->find($id);
if (!$element->getUserOwnerEmail()) {
$user = $this->get('security.context')->getToken()->getUser();
$userEmail = $user->getEmail();
$element->setUserOwnerEmail($userEmail);
$request->getSession()->getFlashBag()->add('success', "Vous êtes maintenant propriétaire de la fiche " . $element->getName() . " !");
$request->getSession()->getFlashBag()->add('success', "Vous êtes maintenant propriétaire de la fiche " . $element->getName() . " !");
$dm->flush();
}
else
{
$request->getSession()->getFlashBag()->add('error', "Désolé, cet élément appartient déjà à quelqu'un !");
$request->getSession()->getFlashBag()->add('error', "Désolé, cet élément appartient déjà à quelqu'un !");
}
return $this->redirectToRoute('biopen_user_contributions');
}
public function profileAction(Request $request)
{
$user = $this->get('security.context')->getToken()->getUser();
......@@ -111,36 +114,36 @@ class UserController extends GoGoController
if (!$user->getNewsletterRange()) $user->setNewsletterRange(50);
if ($form->handleRequest($request)->isValid())
{
{
$alreadyUsedEmail = ($current_user->getEmail() != $user->getEmail()) && count($userRepo->findByEmail($user->getEmail())) > 0;
$alreadyUsedUserName = ($current_user->getUsername() != $user->getUsername()) && count($userRepo->findByUsername($user->getUsername())) > 0;
$locationSetToReceiveNewsletter = $user->getNewsletterFrequency() > 0 && !$user->getLocation();
$geocodeError = false;
if ($user->getLocation()) {
try
{
$geocoded = $this->get('bazinga_geocoder.geocoder')->using('google_maps')->geocode($user->getLocation())->first();
$user->setGeo(new Coordinates($geocoded->getLatitude(), $geocoded->getLongitude()));
}
catch (\Exception $error) { $geocodeError = true; }
}
catch (\Exception $error) { $geocodeError = true; }
}
if ($form->isValid() /*&& !$alreadyUsedEmail */&& !$alreadyUsedUserName && !$locationSetToReceiveNewsletter && !$geocodeError)
if ($form->isValid() /*&& !$alreadyUsedEmail */&& !$alreadyUsedUserName && !$locationSetToReceiveNewsletter && !$geocodeError)
{
$em->persist($user);
$em->flush();
$request->getSession()->getFlashBag()->add('info', "Modifications sauvegardées !");
}
else
}
else
{
// if ($alreadyUsedEmail) $form->get('email')->addError(new FormError('Cet email est déjà utilisé'));
if ($alreadyUsedUserName) $form->get('username')->addError(new FormError("Ce nom d'utilisateur est déjà pris !"));
if ($locationSetToReceiveNewsletter) $form->get('location')->addError(new FormError("Si vous voulez recevoir les nouveaux ajouts, vous devez renseigner une adresse"));
if ($geocodeError) $form->get('location')->addError(new FormError("Impossible de localiser cette adresse"));
}
}
}
return $this->render('@BiopenCoreBundle/user/profile.html.twig', array('user' => $user, 'form' => $form->createView(), 'config' => $config));
return $this->render('@BiopenCoreBundle/user/profile.html.twig', array('user' => $user, 'form' => $form->createView(), 'config' => $config));
}
}
......@@ -23,9 +23,9 @@ class ElementAdminShowEdit extends ElementAdminList
{
$formMapper
->with('Informations générales', array())
->add('name', 'text')
->add('name', 'text')
->add('userOwnerEmail', 'text', array('required' => false, 'label' => "Email de l'utilisateur propriétaire de cette fiche"))
// ->add('images', 'sonata_type_collection', array('by_reference' => true, 'type_options' => array('delete' => true)),
// ->add('images', 'sonata_type_collection', array('by_reference' => true, 'type_options' => array('delete' => true)),
// array('edit' => 'inline', 'inline' => 'table'))
// ->add('stamps', 'sonata_type_model', array(
// 'label' => "Etiquettes",
......@@ -34,45 +34,45 @@ class ElementAdminShowEdit extends ElementAdminList
// 'expanded' => false,
// 'multiple' => true,
// ))
->end();
}
->end();
}
protected function configureShowFields(ShowMapper $show)
{
{
$needModeration = $this->subject->getModerationState() != 0;
$statusClass = $needModeration ? 'col-md-6' : 'col-md-12';
if ($this->subject->isPending())
{
$show
$show
->with('En attente', array('class' => $statusClass))
->add('currContribution', null, array('template' => '@BiopenAdmin/partials/show_one_contribution.html.twig'))->end();
}
else
{
$show->with('Status', array('class' => $statusClass))
->add('status', 'choice', [ 'choices'=> $this->statusChoices ])->end();
->add('status', 'choice', [ 'choices'=> $this->statusChoices ])->end();
}
if ($needModeration) {
$show
->with('Modération', array('class' => 'col-md-6 col-sm-12'))
$show
->with('Modération', array('class' => 'col-md-6 col-sm-12'))
->add('moderationState', 'choice', [
'label' => 'Moderation',
'choices'=> $this->moderationChoices,
'template' => '@BiopenAdmin/partials/show_choice_moderation.html.twig'
])
])
->add('reports', null, array('template' => '@BiopenAdmin/partials/show_pending_reports.html.twig', 'label' => 'Signalements'))
->end();
}
}
$show
->with('Autre infos', array('class' => 'col-md-6 col-sm-12'))
->add('id')
->add('optionValues', null, [
'template' => '@BiopenAdmin/partials/show_option_values.html.twig',
'template' => '@BiopenAdmin/partials/show_option_values.html.twig',
'choices' => $this->optionList,
'label' => 'Catégories' ])
->add('email', 'text', array('label' => 'Email de contact'))
->add('email', 'text', array('label' => 'Email de contact'))
->add('images', null, array('template' => '@BiopenAdmin/partials/show_element_images.html.twig'))
->add('randomHash')
->add('oldId', null, array('label' => 'Id dans la base de données importée'))
......@@ -81,7 +81,7 @@ class ElementAdminShowEdit extends ElementAdminList
->add('updatedAt', 'datetime', array("format" => "d/m/Y à H:i"))
->end()
->with('Localisation', array('class' => 'col-md-6 col-sm-12'))
->with('Localisation', array('class' => 'col-md-6 col-sm-12'))
->add('address.formatedAddress', null, array('label' => 'Adresse complète'))
->add('address.streetAddress', null, array('label' => 'Adresse'))
->add('address.addressLocality', null, array('label' => 'Ville'))
......@@ -89,18 +89,18 @@ class ElementAdminShowEdit extends ElementAdminList
->add('address.addressCountry', null, array('label' => 'Pays'))
->add('geo.latitude')
->add('geo.longitude')
->end()
->end()
->with('Champs personnalisés', array('class' => 'col-md-12 col-sm-12'))
->add('data', null, array('template' => '@BiopenAdmin/partials/show_element_data.html.twig'))
->end()
->add('data', null, array('template' => '@BiopenAdmin/partials/show_element_data.html.twig'))
->end()
->with('Hitorique des contributions', array('class' => 'col-sm-12'))
->with('Historique des contributions', array('class' => 'col-sm-12'))
->add('contributions', null, array('template' => '@BiopenAdmin/partials/show_contributions.html.twig'))
->end();
->end();
$show
->with('JSON', array('class' => 'col-md-12', 'box_class' => 'box box-default'))
->with('JSON', array('class' => 'col-md-12', 'box_class' => 'box box-default'))
->add('compactJson')
->add('baseJson')
->add('privateJson')
......
......@@ -848,7 +848,7 @@ class Element
private function getArrayFromCollection($collection)
{
if ($collection == null) return [];
else if (is_array($collection)) return [];
else if (is_array($collection)) return $collection;
else return $collection->toArray();
}
......@@ -991,7 +991,18 @@ class Element
*/
public function getContributions()
{
return $this->contributions;
// Sometime the association between Element and Contribution is broken, and so
// need to sensure the contribution exists
// #UglyFix
$contribs = [];
foreach ($this->contributions as $contrib) {
try {
if ($contrib->getType() != null) array_push($contribs, $contrib);
} catch (\Exception $e) {
$this->removeContribution($contrib);
}
}
return $contribs;
}
/**
......
......@@ -199,9 +199,9 @@ class ElementImportService
$deleteQuery = $qb
->field('source')->references($import)
->field('status')->equals(ElementStatus::DynamicImportTemp);
// really needed?
$deletedElementIds = array_keys($deleteQuery->select('id')->hydrate(false)->getQuery()->execute()->toArray());
$qb = $this->em->createQueryBuilder(UserInteractionContribution::class);
$qb = $this->em->createQueryBuilder(UserInteraction::class);
$qb->field('element.id')->in($deletedElementIds)->remove()->getQuery()->execute();
$countElemenDeleted = $deleteQuery->remove()->getQuery()->execute()['n'];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment