-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #7893 added possibility to measure mobile apps
- Loading branch information
Showing
51 changed files
with
1,733 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\Measurable; | ||
|
||
use Exception; | ||
use Piwik\Site; | ||
|
||
/** | ||
* Provides access to individual measurables. | ||
*/ | ||
class Measurable extends Site | ||
{ | ||
|
||
public function getSettingValue($name) | ||
{ | ||
$settings = new MeasurableSettings($this->id, $this->getType()); | ||
$setting = $settings->getSetting($name); | ||
|
||
if (!empty($setting)) { | ||
return $setting->getValue(); // Calling `getValue` makes sure we respect read permission of this setting | ||
} | ||
|
||
throw new Exception(sprintf('Setting %s does not exist', $name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\Measurable; | ||
|
||
use Piwik\Piwik; | ||
|
||
/** | ||
* Describes a Type setting for a website, mobile app, ... | ||
* | ||
* See {@link \Piwik\Plugin\Settings}. | ||
*/ | ||
class MeasurableSetting extends \Piwik\Settings\Setting | ||
{ | ||
/** | ||
* By default the value of the type setting is only readable by users having at least view access to one site | ||
* | ||
* @var bool | ||
* @since 2.14.0 | ||
*/ | ||
public $readableByCurrentUser = false; | ||
|
||
/** | ||
* By default the value of the type setting is only writable by users having at least admin access to one site | ||
* @var bool | ||
* @internal | ||
*/ | ||
public $writableByCurrentUser = false; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $name The persisted name of the setting. | ||
* @param string $title The display name of the setting. | ||
*/ | ||
public function __construct($name, $title) | ||
{ | ||
parent::__construct($name, $title); | ||
|
||
$this->writableByCurrentUser = Piwik::isUserHasSomeAdminAccess(); | ||
$this->readableByCurrentUser = Piwik::isUserHasSomeViewAccess(); | ||
} | ||
|
||
/** | ||
* Returns `true` if this setting is writable for the current user, `false` if otherwise. In case it returns | ||
* writable for the current user it will be visible in the Plugin settings UI. | ||
* | ||
* @return bool | ||
*/ | ||
public function isWritableByCurrentUser() | ||
{ | ||
return $this->writableByCurrentUser; | ||
} | ||
|
||
/** | ||
* Returns `true` if this setting can be displayed for the current user, `false` if otherwise. | ||
* | ||
* @return bool | ||
*/ | ||
public function isReadableByCurrentUser() | ||
{ | ||
return $this->readableByCurrentUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
namespace Piwik\Measurable; | ||
|
||
use Piwik\Db; | ||
use Piwik\Piwik; | ||
use Piwik\Plugin\Settings; | ||
use Piwik\Measurable\Settings\Storage; | ||
use Piwik\Settings\Setting; | ||
use Piwik\Measurable\Type; | ||
|
||
class MeasurableSettings extends Settings | ||
{ | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $idSite = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $idType = null; | ||
|
||
/** | ||
* @param int $idSite The id of a site. If you want to get settings for a not yet created site just pass an empty value ("0") | ||
* @param string $idType If no typeId is given, the type of the site will be used. | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function __construct($idSite, $idType) | ||
{ | ||
$this->idSite = $idSite; | ||
$this->idType = $idType; | ||
$this->storage = new Storage(Db::get(), $this->idSite); | ||
$this->pluginName = 'MeasurableSettings'; | ||
|
||
$this->init(); | ||
} | ||
|
||
protected function init() | ||
{ | ||
$typeManager = new Type\Manager(); | ||
$type = $typeManager->getType($this->idType); | ||
$type->configureMeasurableSettings($this); | ||
|
||
/** | ||
* This event is posted when generating settings for a Measurable (website). You can add any Measurable settings | ||
* that you wish to be shown in the Measurable manager (websites manager). If you need to add settings only for | ||
* eg MobileApp measurables you can use eg `$type->getId() === Piwik\Plugins\MobileAppMeasurable\Type::ID` and | ||
* add only settings if the condition is true. | ||
* | ||
* @since Piwik 2.14.0 | ||
* @deprecated will be removed in Piwik 3.0.0 | ||
* | ||
* @param MeasurableSettings $this | ||
* @param \Piwik\Measurable\Type $type | ||
* @param int $idSite | ||
*/ | ||
Piwik::postEvent('Measurable.initMeasurableSettings', array($this, $type, $this->idSite)); | ||
} | ||
|
||
public function addSetting(Setting $setting) | ||
{ | ||
if ($this->idSite && $setting instanceof MeasurableSetting) { | ||
$setting->writableByCurrentUser = Piwik::isUserHasAdminAccess($this->idSite); | ||
} | ||
|
||
parent::addSetting($setting); | ||
} | ||
|
||
public function save() | ||
{ | ||
Piwik::checkUserHasAdminAccess($this->idSite); | ||
|
||
$typeManager = new Type\Manager(); | ||
$type = $typeManager->getType($this->idType); | ||
|
||
/** | ||
* Triggered just before Measurable settings are about to be saved. You can use this event for example | ||
* to validate not only one setting but multiple ssetting. For example whether username | ||
* and password matches. | ||
* | ||
* @since Piwik 2.14.0 | ||
* @deprecated will be removed in Piwik 3.0.0 | ||
* | ||
* @param MeasurableSettings $this | ||
* @param \Piwik\Measurable\Type $type | ||
* @param int $idSite | ||
*/ | ||
Piwik::postEvent('Measurable.beforeSaveSettings', array($this, $type, $this->idSite)); | ||
|
||
$this->storage->save(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\Measurable\Settings; | ||
|
||
use Piwik\Db; | ||
use Piwik\Common; | ||
use Piwik\Settings\Setting; | ||
|
||
/** | ||
* Storage for site settings | ||
*/ | ||
class Storage extends \Piwik\Settings\Storage | ||
{ | ||
private $idSite = null; | ||
|
||
/** | ||
* @var Db | ||
*/ | ||
private $db = null; | ||
|
||
private $toBeDeleted = array(); | ||
|
||
public function __construct(Db\AdapterInterface $db, $idSite) | ||
{ | ||
$this->db = $db; | ||
$this->idSite = $idSite; | ||
} | ||
|
||
protected function deleteSettingsFromStorage() | ||
{ | ||
$table = $this->getTableName(); | ||
$sql = "DELETE FROM $table WHERE `idsite` = ?"; | ||
$bind = array($this->idSite); | ||
|
||
$this->db->query($sql, $bind); | ||
} | ||
|
||
public function deleteValue(Setting $setting) | ||
{ | ||
$this->toBeDeleted[$setting->getName()] = true; | ||
parent::deleteValue($setting); | ||
} | ||
|
||
public function setValue(Setting $setting, $value) | ||
{ | ||
$this->toBeDeleted[$setting->getName()] = false; // prevent from deleting this setting, we will create/update it | ||
parent::setValue($setting, $value); | ||
} | ||
|
||
/** | ||
* Saves (persists) the current setting values in the database. | ||
*/ | ||
public function save() | ||
{ | ||
$table = $this->getTableName(); | ||
|
||
foreach ($this->toBeDeleted as $name => $delete) { | ||
if ($delete) { | ||
$sql = "DELETE FROM $table WHERE `idsite` = ? and `setting_name` = ?"; | ||
$bind = array($this->idSite, $name); | ||
|
||
$this->db->query($sql, $bind); | ||
} | ||
} | ||
|
||
$this->toBeDeleted = array(); | ||
|
||
foreach ($this->settingsValues as $name => $value) { | ||
$value = serialize($value); | ||
|
||
$sql = "INSERT INTO $table (`idsite`, `setting_name`, `setting_value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `setting_value` = ?"; | ||
$bind = array($this->idSite, $name, $value, $value); | ||
|
||
$this->db->query($sql, $bind); | ||
} | ||
} | ||
|
||
protected function loadSettings() | ||
{ | ||
$sql = "SELECT `setting_name`, `setting_value` FROM " . $this->getTableName() . " WHERE idsite = ?"; | ||
$bind = array($this->idSite); | ||
|
||
$settings =$this->db->fetchAll($sql, $bind); | ||
|
||
$flat = array(); | ||
foreach ($settings as $setting) { | ||
$flat[$setting['setting_name']] = unserialize($setting['setting_value']); | ||
} | ||
|
||
return $flat; | ||
} | ||
|
||
private function getTableName() | ||
{ | ||
return Common::prefixTable('site_setting'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
namespace Piwik\Measurable; | ||
|
||
class Type | ||
{ | ||
const ID = ''; | ||
protected $name = 'General_Measurable'; | ||
protected $namePlural = 'General_Measurables'; | ||
protected $description = 'Default measurable type'; | ||
protected $howToSetupUrl = ''; | ||
|
||
public function isType($typeId) | ||
{ | ||
// here we should add some point also check whether id matches any extended ID. Eg if | ||
// MetaSites extends Websites, then we expected $metaSite->isType('website') to be true (maybe) | ||
return $this->getId() === $typeId; | ||
} | ||
|
||
public function getId() | ||
{ | ||
$id = static::ID; | ||
|
||
if (empty($id)) { | ||
$message = 'Type %s does not define an ID. Set the ID constant to fix this issue';; | ||
throw new \Exception(sprintf($message, get_called_class())); | ||
} | ||
|
||
return $id; | ||
} | ||
|
||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getNamePlural() | ||
{ | ||
return $this->namePlural; | ||
} | ||
|
||
public function getHowToSetupUrl() | ||
{ | ||
return $this->howToSetupUrl; | ||
} | ||
|
||
public function configureMeasurableSettings(MeasurableSettings $settings) | ||
{ | ||
} | ||
} | ||
|
Oops, something went wrong.