Store relative avatar path in database

Also it fixes #114
This commit is contained in:
Dmitriy Simushev 2015-03-16 12:56:36 +00:00
parent f6fe379a54
commit 6e71f0542b
7 changed files with 104 additions and 5 deletions

View File

@ -17,6 +17,7 @@
* limitations under the License.
*/
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
use Mibew\Settings;
use Mibew\Thread;
use Mibew\Style\ChatStyle;
@ -375,12 +376,15 @@ function setup_chatview(Thread $thread)
* Prepare some data for chat for user
*
* @param UrlGeneratorInterface $url_generator A URL generator object.
* @param AssetUrlGeneratorInterface $asset_url_generator An asset URL generator
* object.
* @param Request $request The current request.
* @param Thread $thread thread object that will be used
* @return array Array of chat view data
*/
function setup_chatview_for_user(
UrlGeneratorInterface $url_generator,
AssetUrlGeneratorInterface $asset_url_generator,
Request $request,
Thread $thread
) {
@ -428,7 +432,9 @@ function setup_chatview_for_user(
// Set default operator's avatar
$operator = operator_by_id($thread->agentId);
$data['chat']['avatar'] = ($operator['vcavatar'] ? $operator['vcavatar'] : '');
$data['chat']['avatar'] = $operator['vcavatar']
? $asset_url_generator->generate($operator['vcavatar'])
: '';
return $data;
}

View File

@ -40,6 +40,7 @@ class ThreadController extends AbstractController
$processor->setRouter($this->getRouter());
$processor->setAuthenticationManager($this->getAuthenticationManager());
$processor->setMailerFactory($this->getMailerFactory());
$processor->setAssetManager($this->getAssetManager());
return $processor->handleRequest($request);
}

View File

@ -54,6 +54,7 @@ class UserChatController extends AbstractController
$page = setup_chatview_for_user(
$this->getRouter(),
$this->getAssetManager()->getUrlGenerator(),
$request,
$thread
);

View File

@ -58,7 +58,7 @@ class AvatarController extends AbstractController
throw new NotFoundException('The operator is not found');
}
$page['avatar'] = $op['vcavatar'];
$page['avatar'] = $op['vcavatar'] ? $this->asset($op['vcavatar']) : '';
$page['currentop'] = $op
? get_operator_name($op) . ' (' . $op['vclogin'] . ')'
: getlocal('-not found-');
@ -123,7 +123,7 @@ class AvatarController extends AbstractController
// Move uploaded file to avatar directory
try {
$file->move($avatar_local_dir, $new_file_name);
$avatar = $this->asset('files/avatar/' . $new_file_name);
$avatar = 'files/avatar/' . $new_file_name;
} catch (Exception $e) {
$errors[] = failed_uploading_file($orig_filename, "Error moving file");
}

View File

@ -19,6 +19,7 @@
namespace Mibew\Controller;
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
use Mibew\EventDispatcher\EventDispatcher;
use Mibew\EventDispatcher\Events;
use Mibew\Settings;
@ -125,7 +126,10 @@ class WidgetController extends AbstractController
$response_data['dependencies']['invitationCreate'] = array();
$response_data['data']['invitation'] = array(
'operatorName' => htmlspecialchars($operator_name),
'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'avatarUrl' => htmlspecialchars($this->asset(
$operator['vcavatar'],
AssetUrlGeneratorInterface::ABSOLUTE_URL
)),
'threadUrl' => $this->generateUrl(
'chat_user_invitation',
array(),

View File

@ -258,4 +258,63 @@ class Updater
return false;
}
}
/**
* Performs all database updates needed for 2.0.0-beta.4.
*
* @return boolean True if the updates have been applied successfully and
* false otherwise.
*/
protected function update20000Beta4()
{
$db = $this->getDatabase();
if (!$db) {
return false;
}
$db->query('START TRANSACTION');
try {
$operators = $db->query(
'SELECT operatorid AS id, vcavatar AS avatar FROM {operator}',
null,
array('return_rows' => Database::RETURN_ALL_ROWS)
);
// Mibew base path should not be included in operators' avatars
// which stored in the database. Remove the prefixes one by one.
foreach ($operators as $operator) {
if (empty($operator['avatar'])) {
// The operator has no avatar.
continue;
}
if (!preg_match("/^.*(files\/avatar\/[^\/]+)$/", $operator['avatar'], $matches)) {
// Avatar's path has an unknown format.
continue;
}
// Remove Mibew's web root from avatar's path
$db->query(
'UPDATE {operator} SET vcavatar = :avatar WHERE operatorid = :id',
array(
':id' => $operator['id'],
':avatar' => $matches[1],
)
);
}
} catch (\Exception $e) {
// Something went wrong. We actually cannot update the database.
$this->errors[] = getlocal('Cannot update content: {0}', $e->getMessage());
// The database changes should be discarded.
$db->query('ROLLBACK');
return false;
}
// All needed data has been updated.
$db->query('COMMIT');
return true;
}
}

View File

@ -20,6 +20,8 @@
namespace Mibew\RequestProcessor;
// Import namespaces and classes of the core
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Http\Exception\AccessDeniedException;
@ -53,6 +55,7 @@ use Symfony\Component\HttpFoundation\Request;
*/
class ThreadProcessor extends ClientSideProcessor implements
RouterAwareInterface,
AssetManagerAwareInterface,
AuthenticationManagerAwareInterface,
MailerFactoryAwareInterface
{
@ -80,6 +83,13 @@ class ThreadProcessor extends ClientSideProcessor implements
*/
protected $mailerFactory = null;
/**
* An instance of asset manager.
*
* @var AssetManagerInterface|null
*/
protected $assetManager = null;
/**
* Loads thread by id and token and checks if thread loaded
*
@ -187,6 +197,22 @@ class ThreadProcessor extends ClientSideProcessor implements
return $this->mailerFactory;
}
/**
* {@inheritdoc}
*/
public function getAssetManager()
{
return $this->assetManager;
}
/**
* {@inheritdoc}
*/
public function setAssetManager(AssetManagerInterface $manager)
{
$this->assetManager = $manager;
}
/**
* Class constructor
*/
@ -662,6 +688,7 @@ class ThreadProcessor extends ClientSideProcessor implements
// Prepare chat options
$client_data = setup_chatview_for_user(
$this->getRouter(),
$this->getAssetManager()->getUrlGenerator(),
$this->currentRequest,
$thread
);
@ -834,7 +861,8 @@ class ThreadProcessor extends ClientSideProcessor implements
if ($thread->agentId) {
$operator = operator_by_id($thread->agentId);
if ($operator['vcavatar']) {
$image_link = $operator['vcavatar'];
$url_generator = $this->getAssetManager()->getUrlGenerator();
$image_link = $url_generator->generate($operator['vcavatar']);
}
}