Skip to content

Commit

Permalink
issue #1374: myNotes refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kojoty committed Feb 5, 2018
1 parent d0d492f commit ae8827f
Show file tree
Hide file tree
Showing 57 changed files with 1,375 additions and 583 deletions.
5 changes: 4 additions & 1 deletion Config/Menu/customUserMenu.default.php
@@ -1,4 +1,7 @@
<?php
use Utils\Uri\SimpleRouter;
use Controllers\CacheNotesController;

/**
* This is simple configuration of links presented in sidebar of the page
* for authorized users only.
Expand All @@ -24,7 +27,7 @@
'mnu_myAccount' => '/myprofile.php',

'mnu_myRoutes' => '/myroutes.php',
'mnu_myCacheNotes' => '/mycache_notes.php',
'mnu_myCacheNotes' => SimpleRouter::getLink(CacheNotesController::class),
'mnu_watchedCaches' => '/mywatches.php',
'mnu_ignoredCaches' => '/myignores.php',
'mnu_myRecommends' => '/mytop5.php',
Expand Down
256 changes: 256 additions & 0 deletions Controllers/CacheNotesController.php
@@ -0,0 +1,256 @@
<?php

namespace Controllers;

/**
* This controller handled user notes list view
*/

use Utils\Uri\Uri;
use lib\Objects\ChunkModels\PaginationModel;
use lib\Objects\ChunkModels\ListOfCaches\Column_CacheLastLog;
use lib\Objects\ChunkModels\ListOfCaches\Column_CacheName;
use lib\Objects\ChunkModels\ListOfCaches\Column_CacheTypeIcon;
use lib\Objects\ChunkModels\ListOfCaches\Column_EllipsedText;
use lib\Objects\ChunkModels\ListOfCaches\Column_OnClickActionIcon;
use lib\Objects\ChunkModels\ListOfCaches\ListOfCachesModel;
use lib\Objects\GeoCache\CacheNote;
use lib\Objects\GeoCache\UserCacheCoords;
use lib\Objects\Coordinates\Coordinates;
use lib\Objects\GeoCache\MultiCacheStats;
use lib\Objects\GeoCache\MultiLogStats;
use lib\Objects\User\MultiUserQueries;
use lib\Objects\ChunkModels\ListOfCaches\Column_SimpleText;
use Utils\Debug\Debug;

class CacheNotesController extends BaseController
{
public function __construct()
{
parent::__construct();
}

public function isCallableFromRouter($actionName)
{
return true;
}

public function index()
{

if(!$this->isUserLogged()){
$this->redirectToLoginPage();
exit;
}

$this->view->setTemplate('cacheNotes/cacheNotes');
$this->view->addLocalCss(
Uri::getLinkWithModificationTime('/tpl/stdstyle/cacheNotes/cacheNotes.css'));

$this->view->addLocalJs(
Uri::getLinkWithModificationTime('/tpl/stdstyle/cacheNotes/cacheNotes.js'));

$this->view->addLocalCss('/tpl/stdstyle/css/lightTooltip.css');
$this->view->loadJQuery();

//load lightPopup
$this->view->addLocalCss(
Uri::getLinkWithModificationTime('/tpl/stdstyle/js/lightPopup/lightPopup.css'));
$this->view->addLocalJs(
Uri::getLinkWithModificationTime('/tpl/stdstyle/js/lightPopup/lightPopup.js'));



$rowCount = CacheNote::getCountOfUserNotesAndModCoords($this->loggedUser->getUserId());
$this->view->setVar('rowCount', $rowCount);

if($rowCount > 0){
$model = new ListOfCachesModel();

$model->addColumn(new Column_CacheTypeIcon(tr('myNotes_status'),
function($row){
return [
'type' => $row['type'],
'status' => $row['status'],
'user_sts' => isset($row['user_sts'])?$row['user_sts']:null,
];
}));
$model->addColumn(new Column_CacheName(tr('myNotes_cacheName')));
$model->addColumn(new Column_CacheLastLog(tr('myNotes_lastLogEntry'),
function($row){
if(isset($row['llog_id'])){
return [
'logId' => $row['llog_id'],
'logType' => $row['llog_type'],
'logText' => $row['llog_text'],
'logUserName' => $row['llog_userName'],
'logDate' => $row['llog_date']
];
}else{
return [];
}
}
));

// column with notes (+id of this col.)
$model->addColumn(new Column_EllipsedText(tr('myNotes_note'),function($row){
return [
'text' => isset($row['noteTxt'])?$row['noteTxt']:'-',
'maxChars' => 10,
'labelShow' => tr('myNotes_showFullNote'),
'labelHide' => tr('myNotes_hideFullNote'),
];
}));

$model->addColumn(new Column_OnClickActionIcon(tr('myNotes_removeNote'),
function($row){
return [
'icon' => isset($row['noteTxt'])?'/tpl/stdstyle/images/log/16x16-trash.png':null,
'onClick' => "removeNote(this, {$row['cache_id']})",
'title' => tr('myNotes_removeNote')
];
}
));

$model->addColumn(new Column_SimpleText(tr('myNotes_modCacheCoords'),
function($row){
if(isset($row['coords'])){
return ($row['coords'])->getAsText();
}else{
return '-';
}
}
));

$model->addColumn(new Column_OnClickActionIcon(tr('myNotes_removeCoords'),
function($row){

return [
'icon' => isset($row['coords'])?'/tpl/stdstyle/images/log/16x16-trash.png':null,
'onClick' => "removeCoords(this, {$row['cache_id']})",
'title' => tr('myNotes_removeCoords')
];
}
));

$pagination = new PaginationModel(50); //per-page number of caches
$pagination->setRecordsCount($rowCount);
list($queryLimit, $queryOffset) = $pagination->getQueryLimitAndOffset();
$model->setPaginationModel($pagination);

$model->addDataRows( self::completeDataRows(
$this->loggedUser->getUserId(), $queryLimit, $queryOffset));

$this->view->setVar('listCacheModel', $model);
}

$this->view->buildView();
}

/**
* This is called from AJAX only
* @param int $cacheId
*/
public function removeCoords($cacheId)
{
if(!$this->isUserLogged()){
$this->ajaxErrorResponse("User not logged", 401);
return;
}

//check cacheId
if(!is_numeric($cacheId)){
$this->ajaxErrorResponse("Invalid param", 400);
exit;
}

UserCacheCoords::deleteCoords($cacheId, $this->loggedUser->getUserId());
$this->ajaxSuccessResponse();
}

/**
* This is called from ajax only
* @param int cacheId
*/
public function removeNote($cacheId)
{
if(!$this->isUserLogged()){
$this->ajaxErrorResponse("User not logged", 401);
return;
}

Debug::errorLog($cacheId);

//check cacheId
if(!is_numeric($cacheId)){
$this->ajaxErrorResponse("Invalid param", 400);
exit;
}

CacheNote::deleteNote( $this->loggedUser->getUserId(), $cacheId);
$this->ajaxSuccessResponse();

}

private function completeDataRows($userId, $limit=null, $offset=null)
{
$cacheIds = CacheNote::getCachesIdsForNotesAndModCoords($userId, $limit, $offset);

$result = array_fill_keys($cacheIds, []);

// fill notes
foreach (CacheNote::getNotesByCacheIds($cacheIds) as $note){
$result[ $note['cache_id'] ]['noteTxt'] = $note['desc'];
}

// fill mod-coords
foreach ( UserCacheCoords::getCoordsByCacheIds($cacheIds) as $coord){
$result[ $coord['cache_id'] ]['coords'] =
Coordinates::FromCoordsFactory($coord['lat'], $coord['lot']);

$result[ $coord['cache_id'] ]['coordsDate'] = $coord['date'];
}

// fill caches data
$cacheFields = ['cache_id', 'name', 'type', 'status', 'wp_oc'];
foreach ( MultiCacheStats::getGeocachesById($cacheIds, $cacheFields) as $c){
foreach($cacheFields as $col){
$result[ $c['cache_id'] ][$col] = $c[$col];
}
}

// find last logs
$logFields = ['id','text','type','user_id','date'];
foreach( MultiLogStats::getLastLogForEachCache($cacheIds) as $log) {
foreach($logFields as $col){
$result[ $log['cache_id'] ]['llog_'.$col] = $log[$col];
}
}

// find cache status for user (found/not-found)
foreach ( MultiLogStats::getStatusForUser($userId, $cacheIds) as $s){
$result[ $s['cache_id']] ['user_sts'] = $s['type'];
}

// find necessary-users
$userIds = [];
foreach ($result as $r){
if (isset($r['llog_user_id'])){
$userIds[$r['cache_id']] = $r['llog_user_id'];
}
}

$userNames = MultiUserQueries::GetUserNamesForListOfIds($userIds);
foreach ($result as &$r){
if (isset($r['llog_user_id'])){
$r['llog_userName'] = $userNames[$r['llog_user_id']];
}
}

return $result;
}



}

5 changes: 4 additions & 1 deletion Controllers/UserWatchedCachesController.php
Expand Up @@ -85,6 +85,9 @@ public function mapOfWatches()
$this->view->buildView();
}

/**
* Display paginated list of caches watched by current user
*/
public function listOfWatches()
{
if(!$this->isUserLogged()){
Expand Down Expand Up @@ -135,7 +138,7 @@ function($row){
$pagination = new PaginationModel(50); //per-page number of caches
$pagination->setRecordsCount($watchedCachesCount);

list($queryLimit, $queryOffset) = $pagination->getQueryLimitAndOffset();;
list($queryLimit, $queryOffset) = $pagination->getQueryLimitAndOffset();
$model->setPaginationModel($pagination);

$model->addDataRows(
Expand Down
36 changes: 36 additions & 0 deletions Utils/Database/OcDb.php
Expand Up @@ -123,6 +123,27 @@ public function dbFetchAsKeyValArray(PDOStatement $stmt, $keyCol, $valCol)
$this->error('', new PDOException(__METHOD__.': call PDOstatement issue!'));
}

/**
* Returns array with values of $keyCol column.
*
* @param PDOStatement $stmt
* @param string $keyCol - column name to use for result key
* @return array
*/
public function dbFetchOneColumnArray(PDOStatement $stmt, $keyCol)
{
$result = [];
if(!is_null($stmt)){
while($row = $this->dbResultFetch($stmt, OcDb::FETCH_ASSOC)){
$result[] = $row[$keyCol];
}
return $result;
}

$this->error('', new PDOException(__METHOD__.': call PDOstatement issue!'));
}


/**
* This method returns the value from first column of first row in statement
*
Expand Down Expand Up @@ -456,5 +477,20 @@ public function quoteLimitOffset($limit, $offset=null){
return array($limit, $offset);
}

/**
* Quote string before use in DB query
* (needs for IN strings etc.)
*
* @param string $str
* @return string
*/
public function quoteString($str)
{
$value = $this->quote($str, self::PARAM_STR);
$value = substr($value, 1, -1); //remove ' char from the begining and end of the string
$value = mb_ereg_replace('&', '\&', $value); //escape '&' char
return $value;
}

}

0 comments on commit ae8827f

Please sign in to comment.