Skip to content

Commit

Permalink
related articles fixes and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilles107 committed Jan 7, 2015
1 parent 799f6dd commit aaa3d80
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 127 deletions.
135 changes: 67 additions & 68 deletions newscoop/composer.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions newscoop/install/Resources/sql/campsite_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2172,21 +2172,22 @@ UNLOCK TABLES;

DROP TABLE IF EXISTS `context_articles`;

-- Tables for context box
CREATE TABLE IF NOT EXISTS `context_articles` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`fk_context_id` int(10) NOT NULL,
`fk_article_no` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE context_articles (
Id INT AUTO_INCREMENT NOT NULL,
fk_context_id INT NOT NULL,
fk_article_no INT NOT NULL,
`order_number` INT NOT NULL,
INDEX article_number (fk_article_no),
PRIMARY KEY(Id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

DROP TABLE IF EXISTS `context_boxes`;

CREATE TABLE IF NOT EXISTS `context_boxes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`fk_article_no` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;


--
Expand Down
18 changes: 9 additions & 9 deletions newscoop/install/Resources/sql/campsite_demo_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1765,14 +1765,14 @@ CREATE TABLE `comment_commenter` (
DROP TABLE IF EXISTS `context_articles`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `context_articles` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`fk_context_id` int(10) NOT NULL,
`fk_article_no` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `article_number` (`fk_article_no`)
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE context_articles (
Id INT AUTO_INCREMENT NOT NULL,
fk_context_id INT NOT NULL,
fk_article_no INT NOT NULL,
`order_number` INT NOT NULL,
INDEX article_number (fk_article_no),
PRIMARY KEY(Id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

--
-- Table structure for table `context_boxes`
Expand All @@ -1786,7 +1786,7 @@ CREATE TABLE `context_boxes` (
`fk_article_no` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `article_number` (`fk_article_no`)
) ENGINE=MyISAM AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE context_articles ADD `order_number` INT NOT NULL;
35 changes: 34 additions & 1 deletion newscoop/library/Newscoop/Entity/RelatedArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* RelatedArticle entity
*
* @ORM\Entity(repositoryClass="Newscoop\Entity\Repository\RelatedArticleRepository")
* @ORM\Table(name="context_articles")
* @ORM\Table(name="context_articles", indexes={
* @ORM\Index(name="article_number", columns={"fk_article_no"}),
* })
*/
class RelatedArticle
{
Expand All @@ -38,10 +40,17 @@ class RelatedArticle
*/
protected $articleNumber;

/**
* @ORM\Column(type="integer", name="order_number")
* @var integer
*/
protected $order;

public function __construct($articleListId, $articleNumber)
{
$this->setArticleListId($articleListId);
$this->setArticleNumber($articleNumber);
$this->setOrder(0);
}

/**
Expand Down Expand Up @@ -101,4 +110,28 @@ public function getId()
{
return $this->id;
}

/**
* Gets the value of order.
*
* @return integer
*/
public function getOrder()
{
return $this->order;
}

/**
* Sets the value of order.
*
* @param integer $order the order
*
* @return self
*/
public function setOrder($order)
{
$this->order = $order;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
namespace Newscoop\Entity\Repository;

use Doctrine\ORM\EntityRepository;
use Newscoop\Entity\RelatedArticles;
use Doctrine\ORM\Query;

/**
* RelatedArticle repository
*/
class RelatedArticleRepository extends EntityRepository
{
/**
* Get all rlated articles for article and related articles container
*
* @param RelatedArticles $relatedArticles
* @param integer $articleNumber
*
* @return Query
*/
public function getRelatedArticle($relatedArticles, $articleNumber)
{
$qb = $this->createQueryBuilder('r')
Expand All @@ -30,35 +40,29 @@ public function getRelatedArticle($relatedArticles, $articleNumber)
return $query;
}

public function getAllArticles($relatedArticles)
/**
* Get all related articles for related articles container, optionaly get only count of articles
*
* @param RelatedArticles $relatedArticles
* @param boolean $countOnly
*
* @return Query
*/
public function getAllArticles($relatedArticles, $countOnly = false)
{
$qb = $this->createQueryBuilder('r')
->where('r.articleListId = :articleListId')
->setParameters(array(
'articleListId' => $relatedArticles->getId()
));
))
->orderBy('r.order', 'ASC');

if ($countOnly) {
return $qb->select('COUNT(r.id)')->getQuery();
}

$query = $qb->getQuery();

return $query;
}

public function changeOneArticlePosition()
{
/* $qb = $this->getEntityManager()->createQueryBuilder();
$qb = $qb->update('Newscoop\Entity\Article', 'a')
->set('a.indexed', 'CURRENT_TIMESTAMP()');
if (!is_null($articles) && count($articles) > 0) {
$articleNumbers = array();
foreach ($articles AS $article) {
$articleNumbers[] = $article->getNumber();
}
$qb = $qb->where($qb->expr()->in('a.number', $articleNumbers));
}
return $qb->getQuery()->execute();*/
}
}
1 change: 1 addition & 0 deletions newscoop/library/Newscoop/Services/ArticleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private function updateArticleMeta($article, $attributes)
{
$article->setName($attributes['name']);
$article->setCommentsEnabled($attributes['comments_enabled']);
$article->setCommentsLocked($attributes['comments_locked']);
$article->setOnFrontPage($attributes['onFrontPage']);
$article->setOnSection($attributes['onSection']);
$article->setKeywords($attributes['keywords']);
Expand Down
123 changes: 104 additions & 19 deletions newscoop/library/Newscoop/Services/RelatedArticlesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,31 @@
use Newscoop\Entity\Article;
use Newscoop\Entity\RelatedArticles;
use Newscoop\Entity\RelatedArticle;
use Doctrine\ORM\EntityManager;

/**
* Manage related articles
*/
class RelatedArticlesService
{
/**
* @var EntityManager
*/
protected $em;

/**
* @param EntityManager $em
*/
public function __construct($em)
{
$this->em = $em;
}

/**
* Get related articles for article
*
* @param Article $article
*/
public function getRelatedArticles($article)
{
$relatedArticlesBox = $this->findRelatedArticlesBox($article);
Expand All @@ -34,6 +46,14 @@ public function getRelatedArticles($article)
return $relatedArticles;
}

/**
* Remove related article from related articles container
*
* @param Article $article
* @param Article $articleToRemove
*
* @return boolean
*/
public function removeRelatedArticle($article, $articleToRemove)
{
$relatedArticles = $this->findRelatedArticlesBox($article);
Expand All @@ -42,16 +62,27 @@ public function removeRelatedArticle($article, $articleToRemove)
->getRelatedArticle($relatedArticles, $articleToRemove->getNumber())
->getOneOrNullResult();

$removedRelatedArticlePosition = $relatedArticle->getOrder();

if ($relatedArticle) {
$this->em->remove($relatedArticle);
$this->em->flush();

$this->reorderAfterRemove($relatedArticles, $relatedArticle);
$this->reorderAfterRemove($relatedArticles, $removedRelatedArticlePosition);
}

return true;
}

/**
* Add new related article to related articles container
*
* @param Article $article
* @param Article $articleToAdd
* @param integer $position
*
* @return boolean
*/
public function addArticle($article, $articleToAdd, $position = false)
{
$relatedArticles = $this->findRelatedArticlesBox($article);
Expand All @@ -75,12 +106,46 @@ public function addArticle($article, $articleToAdd, $position = false)
return true;
}

private function reorderAfterRemove($relatedArticles, $removedRelatedArticle)
private function reorderAfterRemove($relatedArticles, $removedRelatedArticlePosition)
{
// lock table
// get old position
// move all bigger than old position up (-1)
// unlock table
$this->initOrderOnRelatedArticles($relatedArticles);

try {
$this->em->getConnection()->exec('LOCK TABLES context_articles WRITE;');

// move all bigger than old position up (-1)
$this->em
->createQuery('UPDATE Newscoop\Entity\RelatedArticle r SET r.order = r.order-1 WHERE r.order > :oldPosition')
->setParameter('oldPosition', $removedRelatedArticlePosition)
->execute();

$this->em->getConnection()->exec('UNLOCK TABLES;');
} catch (\Exception $e) {
$this->em->getConnection()->exec('UNLOCK TABLES;');
}

return true;
}

private function initOrderOnRelatedArticles($relatedArticles)
{
$articles = $this->em->getRepository('Newscoop\Entity\RelatedArticle')
->getAllArticles($relatedArticles)
->getResult();

$index = 0;
foreach ($articles as $article) {
if (is_int($article->getOrder()) && $article->getOrder() > 0 && $index == 0) {
return;
}
$index++;

$article->setOrder($index);
}

$this->em->flush();

return true;
}

private function positionRelateArticle($relatedArticles, $relatedArticle, $position)
Expand All @@ -89,26 +154,46 @@ private function positionRelateArticle($relatedArticles, $relatedArticle, $posit
return;
}

// when adding new article with precised position update segments of articles with lower and bigger possition (than new one).
// This will allow for rendering only few lats articles in playlist and related articles boxes (with load more option)
// and still allowing updating list order and adding new elements
$this->initOrderOnRelatedArticles($relatedArticles);

try {
$this->em->getConnection()->exec('LOCK TABLES context_articles WRITE;');
$this->em->getConnection()->exec('LOCK TABLES context_articles WRITE, context_articles as c0_ WRITE;');

// check if position isn't bigger that max one;
$maxPosition = $this->em
->createQuery('SELECT COUNT(r) FROM Newscoop\Entity\RelatedArticle r WHERE r.articleListId = :articleListId AND r.order > 0 ORDER BY r.order ASC')
->setParameter('articleListId', $relatedArticles->getId())
->getSingleScalarResult();

if ($position > ((int)$maxPosition)) {
$position = (int)$maxPosition ;
}

// get article - move to position 0
$oldOrder = $relatedArticle->getOrder();
$relatedArticle->setOrder(0);
$this->em->flush();

// move all bigger than old position up (-1)
$this->em
->createQuery('UPDATE Newscoop\Entity\RelatedArticle r SET r.order = r.order-1 WHERE r.order > :oldPosition')
->setParameter('oldPosition', $oldOrder)
->execute();

// move all bigger than new position down (+1)
$this->em
->createQuery('UPDATE Newscoop\Entity\RelatedArticle r SET r.order = r.order+1 WHERE r.order >= :newPosition')
->setParameter('newPosition', $position)
->execute();

// move changed element from position 0 to new position
$relatedArticle->setOrder($position);
$this->em->flush();

$this->em->getConnection()->exec('UNLOCK TABLES;');
} catch (\Exception $e) {
$this->em->getConnection()->exec('UNLOCK TABLES;');
ladybug_dump_die($e);
}

// lock table
// get old position - move to position 0
// move all bigger than old position up (-1)
// check new position and update
// move all bigger than new position down (+1)
// move changed element from position 0 to new position
// unlock table
}

private function findRelatedArticlesBox($article)
Expand Down
Loading

0 comments on commit aaa3d80

Please sign in to comment.