function ecrireAction() { if ($this->_getParam('article')) { $a = $this->_helper->Article(); $j = $a->findParentJournaux(); try { $a->findDocument(); $this->redirectSimple('envoyer'); } catch (Strass_Db_Table_NotFound $e) { } } else { $a = null; $j = $this->_helper->Journal(); } $this->view->unite = $u = $j->findParentUnites(); $this->metas(array('DC.Title' => "Écrire un article")); $this->assert(null, $j, 'ecrire', "Vous n'avez pas le droit d'écrire un nouvel article dans ce journal"); $publier = $this->assert(null, $j, 'publier'); $this->view->model = $m = new Wtk_Form_Model('ecrire'); $me = Zend_Registry::get('individu'); if ($publier) { $i = $m->addEnum('auteur', "Auteur"); /* on inclus les membres de sous-unité : le scout peuvent écrire dans la gazette de troupe */ foreach ($u->findInscrits(null, 1) as $individu) { $i->addItem($individu->id, $individu->getFullname(false)); } if (!count($i)) { throw new Strass_Controller_Action_Exception_Notice("L'auteur de l'article doit être un membre, mais cette unité n'a aucun membre !"); } if ($a) { $i->set($a->findAuteur()->id); } else { $i->set($me->id); } } else { $i = $m->addInteger('auteur', "Auteur", $me->id, true); } $i = $m->addString('titre', "Titre", $a ? $a->titre : null); $m->addConstraintRequired($i); if ($publier) { $m->addEnum('public', 'Publication', $a ? $a->public : null, array(0 => 'Brouillon', 1 => 'Publier')); } $m->addString('boulet', "Boulet", $a ? $a->boulet : null); $i = $m->addString('article', "Article", $a ? $a->article : null); $m->addConstraintRequired($i); $t = $m->addTable('images', "Images", array('image' => array('File', "Image"), 'nom' => array('String', "Renommer en"), 'origin' => array('String')), false); if ($a) { foreach ($a->getImages() as $image) { $t->addRow(null, $image, $image); } } $t->addRow(); $m->addNewSubmission('poster', "Poster"); if ($m->validate()) { $t = new Articles(); $db = $t->getAdapter(); $db->beginTransaction(); try { if ($a) { $a->slug = $t->createSlug(wtk_strtoid($m->titre), $a->slug); $c = $a->findParentCommentaires(); $message = "Article édité"; } else { $c = new Commentaire(); $a = new Article(); $a->slug = $t->createSlug(wtk_strtoid($m->titre)); $message = "Nouvel article"; } $c->auteur = $m->auteur; $c->save(); $a->journal = $j->id; $a->titre = $m->titre; $a->boulet = $m->boulet; $a->article = $m->article; try { $a->public = (int) $m->public; } catch (Exception $e) { } $a->commentaires = $c->id; $a->save(); $oldImages = $a->getImages(); $newImages = array(); $table = $m->getInstance('images'); foreach ($table as $row) { if ($row->origin && $row->origin != $row->nom) { $a->renameImage($row->origin, $row->nom); array_push($newImages, $row->nom); } else { $if = $row->getChild('image'); if ($if->isUploaded()) { $nom = $row->nom ? $row->nom : $if->getBasename(); $a->storeImage($if->getTempFilename(), $nom); array_push($newImages, $nom); } else { array_push($newImages, $row->nom); } } } $oldImages = array_unique($oldImages); $newImages = array_filter($newImages); /* Nettoyage des images */ foreach ($oldImages as $image) { if (!in_array($image, $newImages)) { $a->deleteImage($image); } } $this->logger->info($message, $this->_helper->url('consulter', 'journaux', null, array('article' => $a->slug), true)); if (!$this->assert(null, $j, 'publier')) { $mail = new Strass_Mail_Article($a); $mail->send(); } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } $this->redirectSimple('consulter', 'journaux', null, array('article' => $a->slug), true); } }