Last active
May 21, 2020 15:14
-
-
Save cgi-caesar/0ffbbf5a003aa55c6b23971631f8f415 to your computer and use it in GitHub Desktop.
Soft File to License Scheme Plugin
This file contains hidden or 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
<?php | |
/** | |
* @title Soft File to License Scheme | |
* @path application/default/plugins/misc/softsalefile-to-license.php | |
*/ | |
class Am_Plugin_SoftsalefileToLicense extends Am_Plugin | |
{ | |
const PLUGIN_STATUS = self::STATUS_PRODUCTION; | |
const PLUGIN_COMM = self::COMM_COMMERCIAL; | |
const PLUGIN_REVISION = '@@VERSION@@'; | |
protected $_configPrefix = 'misc.'; | |
static function getDbXml() | |
{ | |
return <<<CUT | |
<schema version="4.0.0"> | |
<table name="softsale_file"> | |
<field name="scheme_id" type="int" unsigned="1" notnull="0" /> | |
</table> | |
</schema> | |
CUT; | |
} | |
function isConfigured() | |
{ | |
return $this->getDi()->modules->isEnabled('softsale'); | |
} | |
function onGridContentSoftsalefileInitForm(Am_Event_Grid $e) | |
{ | |
$e->getGrid() | |
->getForm() | |
->addSelect('scheme_id') | |
->setLabel('License Schema') | |
->loadOptions(['' => ''] + $this->getSchemeOptions()); | |
} | |
function getSchemeOptions() | |
{ | |
return $this->getDi()->db->selectCol("SELECT title, scheme_id AS ARRAY_KEY FROM ?_softsale_license_scheme;"); | |
} | |
function onBeforeRender(Am_Event $e) | |
{ | |
if (stripos($e->getTemplateName(), 'softsale/licenses.phtml') !== false) { | |
$_ = $e->getView()->files; | |
$files = []; | |
foreach ($_ as $f) { | |
$files[] = new SoftsaleFileProxy($f); | |
} | |
$e->getView()->files = $files; | |
} | |
} | |
function onInitFinished(Am_Event $e) | |
{ | |
$this->getDi()->router->addRoute('my-download', new Am_Mvc_Router_Route( | |
'softsale/download', | |
[ | |
'module' => 'softsale', | |
'controller' => 'my-download', | |
'action' => 'index', | |
] | |
) | |
); | |
} | |
public function onMailTemplateBeforeParse(Am_Event $e) | |
{ | |
/* @var $t Am_Mail_Template */ | |
$t = $e->getTemplate(); | |
if (isset($t->license) && isset($t->user)) { | |
$license = $t->license; | |
$license->download_url = ''; | |
if (!empty($license->scheme_id)) { | |
$file = $this->getDi()->softsaleFileTable->findFirstBySchemeId($license->scheme_id); | |
if ($file /* && $file->hasAccess($t->user) */) { //access record can not be added at this stage | |
if ($upload = $this->getDi()->softsaleFileUploadTable->findFirstByFileId($file->pk(), 'date DESC')) { | |
$license->download_url = $this->getDi()->surl('softsale/download', ['file_id' => $file->pk(), 'file_upload_id' => $upload->pk()], false); | |
} | |
} | |
} | |
} | |
} | |
function getExpire($scheme_id, $user_id) | |
{ | |
return $this->getDi()->db->selectCell( | |
"SELECT DATE(MAX(expires)) FROM ?_softsale_license WHERE scheme_id=? AND user_id=?", | |
$scheme_id, | |
$user_id | |
); | |
} | |
} | |
if (Am_Di::getInstance()->modules->isEnabled('softsale')) : | |
require_once APPLICATION_PATH . '/softsale/controllers/DownloadController.php'; | |
class Softsale_MyDownloadController extends Softsale_DownloadController | |
{ | |
public function indexAction() | |
{ | |
$file = $this->getDi()->softsaleFileTable->load($this->_request->getInt('file_id')); | |
$upload = $this->getDi()->softsaleFileUploadTable->load($this->_request->getInt('file_upload_id')); | |
if ($file->scheme_id && $upload->date > $this->getExpire($file->scheme_id, $this->getDi()->user->pk())) { | |
throw new Am_Exception_InputError("Access denied reason #5"); | |
} | |
return parent::indexAction(); | |
} | |
protected function getExpire($scheme_id, $user_id) | |
{ | |
return $this->getDi()->plugins_misc->loadGet('softsalefile-to-license')->getExpire($scheme_id, $user_id); | |
} | |
} | |
class SoftsaleFileProxy extends SoftsaleFile | |
{ | |
protected $item; | |
function __construct($item) | |
{ | |
$this->item = $item; | |
$this->_table = $item->_table; | |
$this->_db = $this->_table->getAdapter(); | |
} | |
function __get($name) | |
{ | |
return $this->item->$name; | |
} | |
function __isset($name) | |
{ | |
return isset($this->item->$name); | |
} | |
public function getUploads($cache = false) | |
{ | |
if ($cache && $this->_uploads) | |
return $this->_uploads; | |
$expire = null; | |
if ($this->scheme_id) { | |
$expire = $this->_db->selectCell( | |
"SELECT DATE(MAX(expires)) FROM ?_softsale_license WHERE scheme_id=? AND user_id=?", | |
$this->scheme_id, | |
Am_Di::getInstance()->user->pk() | |
); | |
} | |
$ret = $this->getDi()->softsaleFileUploadTable->selectObjects(<<<CUT | |
SELECT * | |
FROM ?_softsale_file_upload | |
WHERE file_id=?d | |
AND hide <= 0 | |
{AND date <= ?} | |
ORDER BY date DESC, file_upload_id DESC | |
CUT | |
, $this->pk(), $expire ?: DBSIMPLE_SKIP); | |
if ($cache) | |
$this->_uploads = $ret; | |
return $ret; | |
} | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment