Fixup some behaviour in record_preview

This commit is contained in:
Benoît Burnichon
2016-03-07 00:11:11 +01:00
parent 7399e3de56
commit f1cd8d7449
3 changed files with 48 additions and 85 deletions

View File

@@ -259,7 +259,7 @@ class FeedEntry
/** /**
* Get items * Get items
* *
* @return \Doctrine\Common\Collections\Collection * @return FeedItem[]|\Doctrine\Common\Collections\Collection
*/ */
public function getItems() public function getItems()
{ {

View File

@@ -1917,7 +1917,7 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
/** /**
* @return Connection * @return Connection
*/ */
private function getDataboxConnection() protected function getDataboxConnection()
{ {
if (null === $this->connection) { if (null === $this->connection) {
$this->connection = $this->getDatabox()->get_connection(); $this->connection = $this->getDatabox()->get_connection();

View File

@@ -10,8 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\Basket; use Alchemy\Phrasea\Model\Entities\FeedEntry;
use Alchemy\Phrasea\Model\Entities\BasketElement;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface; use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
use Guzzle\Http\Url; use Guzzle\Http\Url;
@@ -54,17 +53,17 @@ class record_preview extends record_adapter
protected $short_history; protected $short_history;
/** /**
* @var media * @var media_adapter
*/ */
protected $view_popularity; protected $view_popularity;
/** /**
* @var media * @var media_adapter
*/ */
protected $refferer_popularity; protected $refferer_popularity;
/** /**
* @var media * @var media_adapter
*/ */
protected $download_popularity; protected $download_popularity;
@@ -131,7 +130,6 @@ class record_preview extends record_adapter
$Basket = $app['converter.basket']->convert($contId); $Basket = $app['converter.basket']->convert($contId);
$app['acl.basket']->hasAccess($Basket, $app->getAuthenticatedUser()); $app['acl.basket']->hasAccess($Basket, $app->getAuthenticatedUser());
/* @var $Basket Basket */
$this->container = $Basket; $this->container = $Basket;
$this->total = $Basket->getElements()->count(); $this->total = $Basket->getElements()->count();
$i = 0; $i = 0;
@@ -139,25 +137,18 @@ class record_preview extends record_adapter
foreach ($Basket->getElements() as $element) { foreach ($Basket->getElements() as $element) {
$i ++; $i ++;
if ($first) { if ($element->getOrd() == $pos || $first) {
$this->original_item = $element; $this->original_item = $element;
$sbas_id = $element->getRecord($this->app)->getDataboxId(); $sbas_id = $element->getSbasId();
$record_id = $element->getRecord($this->app)->getRecordId(); $record_id = $element->getRecordId();
$this->name = $Basket->getName();
$number = $element->getOrd();
}
$first = false;
if ($element->getOrd() == $pos) {
$this->original_item = $element;
$sbas_id = $element->getRecord($this->app)->getDataboxId();
$record_id = $element->getRecord($this->app)->getRecordId();
$this->name = $Basket->getName(); $this->name = $Basket->getName();
$number = $element->getOrd(); $number = $element->getOrd();
$first = false;
} }
} }
break; break;
case "FEED": case "FEED":
/** @var FeedEntry $entry */
$entry = $app['repo.feed-entries']->find($contId); $entry = $app['repo.feed-entries']->find($contId);
$this->container = $entry; $this->container = $entry;
@@ -167,25 +158,24 @@ class record_preview extends record_adapter
foreach ($entry->getItems() as $element) { foreach ($entry->getItems() as $element) {
$i ++; $i ++;
if ($first) { if ($element->getOrd() == $pos || $first) {
$sbas_id = $element->getRecord($this->app)->get_sbas_id(); $sbas_id = $element->getSbasId();
$record_id = $element->getRecord($this->app)->get_record_id(); $record_id = $element->getRecordId();
$this->name = $entry->getTitle();
$this->original_item = $element;
$number = $element->getOrd();
}
$first = false;
if ($element->getOrd() == $pos) {
$sbas_id = $element->getRecord($this->app)->get_sbas_id();
$record_id = $element->getRecord($this->app)->get_record_id();
$this->name = $entry->getTitle(); $this->name = $entry->getTitle();
$this->original_item = $element; $this->original_item = $element;
$number = $element->getOrd(); $number = $element->getOrd();
$first = false;
} }
} }
break; break;
default:
throw new InvalidArgumentException(sprintf('Expects env argument to one of (RESULT, REG, BASK, FEED) got %s', $env));
} }
if (!(isset($sbas_id) && isset($record_id))) {
throw new Exception('No record could be found');
}
parent::__construct($app, $sbas_id, $record_id, $number); parent::__construct($app, $sbas_id, $record_id, $number);
} }
@@ -215,22 +205,23 @@ class record_preview extends record_adapter
} }
/** /**
* * @return bool
* @return boolean
*/ */
public function is_from_result() public function is_from_result()
{ {
return $this->env == 'RESULT'; return $this->env == 'RESULT';
} }
/**
* @return bool
*/
public function is_from_feed() public function is_from_feed()
{ {
return $this->env == 'FEED'; return $this->env == 'FEED';
} }
/** /**
* * @return bool
* @return boolean
*/ */
public function is_from_basket() public function is_from_basket()
{ {
@@ -238,8 +229,7 @@ class record_preview extends record_adapter
} }
/** /**
* * @return bool
* @return boolean
*/ */
public function is_from_reg() public function is_from_reg()
{ {
@@ -292,7 +282,6 @@ class record_preview extends record_adapter
} }
/** /**
*
* @return mixed content * @return mixed content
*/ */
public function get_container() public function get_container()
@@ -301,8 +290,7 @@ class record_preview extends record_adapter
} }
/** /**
* * @return array
* @return Array
*/ */
public function get_short_history() public function get_short_history()
{ {
@@ -314,9 +302,6 @@ class record_preview extends record_adapter
$report = $this->app->getAclForUser($this->app->getAuthenticatedUser())->has_right_on_base($this->getBaseId(), 'canreport'); $report = $this->app->getAclForUser($this->app->getAuthenticatedUser())->has_right_on_base($this->getBaseId(), 'canreport');
$databox = $this->app->findDataboxById($this->getDataboxId());
$connsbas = $databox->get_connection();
$sql = 'SELECT d . * , l.user, l.usrid as usr_id, l.site $sql = 'SELECT d . * , l.user, l.usrid as usr_id, l.site
FROM log_docs d, log l FROM log_docs d, log l
WHERE d.log_id = l.id WHERE d.log_id = l.id
@@ -331,12 +316,7 @@ class record_preview extends record_adapter
$sql .= 'ORDER BY d.date, usrid DESC'; $sql .= 'ORDER BY d.date, usrid DESC';
$stmt = $connsbas->prepare($sql); foreach ($this->getDataboxConnection()->executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC) as $row) {
$stmt->execute($params);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
foreach ($rs as $row) {
$hour = $this->app['date-formatter']->getPrettyString(new DateTime($row['date'])); $hour = $this->app['date-formatter']->getPrettyString(new DateTime($row['date']));
if ( ! isset($tab[$hour])) if ( ! isset($tab[$hour]))
@@ -379,8 +359,7 @@ class record_preview extends record_adapter
} }
/** /**
* * @return media_adapter
* @return media_image
*/ */
public function get_view_popularity() public function get_view_popularity()
{ {
@@ -418,19 +397,14 @@ class record_preview extends record_adapter
AND site_id = :site AND site_id = :site
GROUP BY datee ORDER BY datee ASC'; GROUP BY datee ORDER BY datee ASC';
$databox = $this->app->findDataboxById($this->getDataboxId()); $result = $this->getDataboxConnection()
$connsbas = $databox->get_connection(); ->executeQuery($sql, [
$stmt = $connsbas->prepare($sql);
$stmt->execute(
[
':record_id' => $this->getRecordId(), ':record_id' => $this->getRecordId(),
':site' => $this->app['conf']->get(['main', 'key']) ':site' => $this->app['conf']->get(['main', 'key'])
] ])
); ->fetchAll(PDO::FETCH_ASSOC);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
foreach ($rs as $row) { foreach ($result as $row) {
if (isset($views[$row['datee']])) { if (isset($views[$row['datee']])) {
$views[$row['datee']] = (int) $row['views']; $views[$row['datee']] = (int) $row['views'];
$top = max((int) $row['views'], $top); $top = max((int) $row['views'], $top);
@@ -469,8 +443,7 @@ class record_preview extends record_adapter
} }
/** /**
* * @return media_adapter
* @return media
*/ */
public function get_refferer_popularity() public function get_refferer_popularity()
{ {
@@ -487,23 +460,19 @@ class record_preview extends record_adapter
return $this->refferer_popularity; return $this->refferer_popularity;
} }
$databox = $this->app->findDataboxById($this->getDataboxId());
$connsbas = $databox->get_connection();
$sql = 'SELECT count( id ) AS views, referrer $sql = 'SELECT count( id ) AS views, referrer
FROM `log_view` FROM `log_view`
WHERE record_id = :record_id WHERE record_id = :record_id
AND date > ( NOW( ) - INTERVAL 1 MONTH ) AND date > ( NOW( ) - INTERVAL 1 MONTH )
GROUP BY referrer ORDER BY referrer ASC'; GROUP BY referrer ORDER BY referrer ASC';
$stmt = $connsbas->prepare($sql); $result = $this->getDataboxConnection()
$stmt->execute([':record_id' => $this->getRecordId()]); ->executeQuery($sql, [':record_id' => $this->getRecordId()])
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); ->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$referrers = []; $referrers = [];
foreach ($rs as $row) { foreach ($result as $row) {
if ($row['referrer'] == 'NO REFERRER') if ($row['referrer'] == 'NO REFERRER')
$row['referrer'] = $this->app->trans('report::acces direct'); $row['referrer'] = $this->app->trans('report::acces direct');
if ($row['referrer'] == $this->app['conf']->get('servername') . 'prod/') if ($row['referrer'] == $this->app['conf']->get('servername') . 'prod/')
@@ -542,8 +511,7 @@ class record_preview extends record_adapter
} }
/** /**
* * @return media_adapter
* @return media
*/ */
public function get_download_popularity() public function get_download_popularity()
{ {
@@ -581,21 +549,16 @@ class record_preview extends record_adapter
AND site= :site AND site= :site
GROUP BY datee ORDER BY datee ASC'; GROUP BY datee ORDER BY datee ASC';
$databox = $this->app->findDataboxById($this->getDataboxId()); $result = $this->getDataboxConnection()
$connsbas = $databox->get_connection(); ->executeQuery($sql, [
$stmt = $connsbas->prepare($sql);
$stmt->execute(
[
':record_id' => $this->getRecordId(), ':record_id' => $this->getRecordId(),
':site' => $this->app['conf']->get(['main', 'key']) ':site' => $this->app['conf']->get(['main', 'key'])
] ])
); ->fetchAll(PDO::FETCH_ASSOC);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$top = 10; $top = 10;
foreach ($rs as $row) { foreach ($result as $row) {
if (isset($dwnls[$row['datee']])) { if (isset($dwnls[$row['datee']])) {
$dwnls[$row['datee']] = (int) $row['dwnl']; $dwnls[$row['datee']] = (int) $row['dwnl'];
$top = max(((int) $row['dwnl'] + 10), $top); $top = max(((int) $row['dwnl'] + 10), $top);