1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 16:06:15 +00:00

Add ConfigMapper, similar to chat app. Also implement setCountry call, need to use and handle it on the interface now

This commit is contained in:
Loic Blot 2015-01-06 13:49:50 +00:00
parent 858933d6c0
commit 816a3a784f
3 changed files with 93 additions and 18 deletions

View File

@ -20,6 +20,8 @@ use \OCA\OcSms\Controller\SmsController;
use \OCA\OcSms\Db\Sms;
use \OCA\OcSms\Db\SmsMapper;
use \OCA\OcSms\Db\ConfigMapper;
use \OCA\OcSms\Lib\PhoneNumberFormatter;
class OcSmsApp extends App {
@ -27,9 +29,6 @@ class OcSmsApp extends App {
/**
* @var array used to cache the parsed contacts for every request
*/
/*
caching dosn´t work because on every call all will be reinstantiated
*/
private static $contacts;
private static $contactPhotos;
@ -45,22 +44,16 @@ class OcSmsApp extends App {
$app = $this;
/**
* Controllers
* Database Layer
*/
$container->registerService('SmsController', function($c) use($app) {
return new SmsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId'),
$c->query('SmsMapper'),
$app
$container->registerService('ConfigMapper', function ($c) use ($app) {
return new ConfigMapper(
$c->query('ServerContainer')->getDb(),
$app->getUserId(),
$c->query('ServerContainer')->getCrypto()
);
});
/**
* Database Layer
*/
$container->registerService('Sms', function($c) {
return new Sms($c->query('ServerContainer')->getDb());
});
@ -76,6 +69,20 @@ class OcSmsApp extends App {
return \OCP\User::getUser();
});
/**
* Controllers
*/
$container->registerService('SmsController', function($c) use($app) {
return new SmsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId'),
$c->query('SmsMapper'),
$c->query('ConfigMapper'),
$app
);
});
/**
* Managers
*/

View File

@ -16,22 +16,29 @@ use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
use \OCA\OcSms\AppInfo\OcSmsApp;
use \OCA\OcSms\Db\ConfigMapper;
use \OCA\OcSms\Db\SmsMapper;
use \OCA\OcSms\Lib\CountryCodes;
use \OCA\OcSms\Lib\PhoneNumberFormatter;
class SmsController extends Controller {
private $app;
private $userId;
private $configMapper;
private $smsMapper;
private $errorMsg;
public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, OcSmsApp $app){
public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, ConfigMapper $cfgMapper, OcSmsApp $app){
parent::__construct($appName, $request);
$this->app = $app;
$this->userId = $userId;
$this->smsMapper = $mapper;
$this->configMapper = $cfgMapper;
}
/**
@ -287,6 +294,10 @@ class SmsController extends Controller {
* @NoAdminRequired
*/
function setCountry($country) {
if (!array_key_exists($country, CountryCodes::$codes)) {
return new JSONResponse(array("status" => false, "msg" => "Invalid country"));
}
$this->configMapper->set("country", $country);
return new JSONResponse(array("status" => true, "msg" => "OK"));
}
}

57
db/configmapper.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* ownCloud - ocsms
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @copyright Loic Blot 2014-2015
*/
namespace OCA\OcSms\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use \OCP\AppFramework\Db\Mapper;
use \OCP\IDb;
class ConfigMapper extends Mapper {
/**
* @var string ownCloud user id
*/
private $user;
/**
* @var \OCP\Security\ICrypto
*/
private $crypto;
public function __construct(IDb $api, $user, $crypto){
parent::__construct($api, 'ocsms_config');
$this->user = $user;
$this->crypto = $crypto;
}
public function set($backend, $key, $value){
$value = $this->crypto->encrypt($value);
if($this->hasKey($key, $value)){
$sql = "UPDATE `*PREFIX*chat_config` SET `value` = ? WHERE `user` = ? AND `keyi` = ?";
$this->execute($sql, array($value, $this->user, $backend, $key));
} else {
$sql = "INSERT INTO `*PREFIX*chat_config` (`user`,`key`,`value`) VALUES (?,?,?);";
$this->execute($sql, array($this->user, $key, $value));
}
}
public function hasKey($key, $value){
try {
$sql = "SELECT key FROM `*PREFIX*ocsms_config` WHERE `key` = ? AND `user` = ?";
$this->findEntity($sql, array($key, $this->user));
return true;
} catch (DoesNotExistException $e){
return false;
}
}
?>