Newer
Older
<?php
namespace QUI\Memberships;
use QUI\CRUD\Factory;
use QUI\Utils\Grid;
use QUI;
use QUI\ERP\Products\Handler\Categories as ProductCategories;
use QUI\ERP\Products\Handler\Fields as ProductFields;
use QUI\Memberships\Products\MembershipField;
const PERMISSION_CREATE = 'quiqqer.memberships.create';
const PERMISSION_EDIT = 'quiqqer.memberships.edit';
const PERMISSION_DELETE = 'quiqqer.memberships.delete';
const PERMISSION_FORCE_EDIT = 'quiqqer.memberships.force_edit';
*/
public function createChild($data = array())
{
Permission::checkPermission(self::PERMISSION_CREATE);
$data['createDate'] = Utils::getFormattedTimestamp();
$data['createUser'] = QUI::getUserBySession()->getId();
// title
$title = trim($data['title']);
if (empty($title)) {
throw new QUI\Memberships\Exception(array(
'quiqqer/memberships',
'exception.handler.no.title'
));
}
$data['title'] = array();
foreach (QUI::availableLanguages() as $lang) {
$data['title'][$lang] = $title;
}
$data['title'] = json_encode($data['title']);
// groupIds
$Groups = QUI::getGroups();
$groupIds = $data['groupIds'];
if (empty($groupIds)
|| !is_array($groupIds)
) {
throw new QUI\Memberships\Exception(array(
'quiqqer/memberships',
'exception.handler.no.groups'
));
}
foreach ($groupIds as $groupId) {
// check if group exist by getting them
$Groups->get((int)$groupId);
}
$data['groupIds'] = ',' . implode(',', $groupIds) . ',';
$data['duration'] = '1-month';
/** @var Membership $NewMembership */
$NewMembership = parent::createChild($data);
$NewMembership->createProduct();
return $NewMembership;
/**
* Get membership
*
* @param int $id
* @return Membership
*/
public function getChild($id)
{
return parent::getChild($id);
}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @inheritdoc
* @return string
*/
public function getDataBaseTableName()
{
return 'quiqqer_memberships';
}
/**
* @inheritdoc
* @return string
*/
public function getChildClass()
{
return Membership::class;
}
/**
* @inheritdoc
* @return array
*/
public function getChildAttributes()
{
return array(
'title',
'description',
'content',
'duration',
'groupIds',
'editDate',
'editUser',
// these fields require quiqqer/order

Patrick Müller
committed
'paymentInterval'
// @todo additional fields for quiqqer/contracts
);
}
/**
* Search memberships
*
* @param array $searchParams
* @param bool $countOnly (optional) - get count for search result only [default: false]
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
*/
public function search($searchParams, $countOnly = false)
{
$memberships = array();
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$whereOr = array();
if (!empty($searchParams['search'])) {
$search = $searchParams['search'];
$whereOr['title'] = array(
'type' => '%LIKE%',
'value' => $search
);
$whereOr['description'] = array(
'type' => '%LIKE%',
'value' => $search
);
$whereOr['content'] = array(
'type' => '%LIKE%',
'value' => $search
);
}
if ($countOnly) {
$result = QUI::getDataBase()->fetch(array(
'count' => 1,
'from' => $this->getDataBaseTableName(),
'where_or' => $whereOr
));
return current(current($result));
}
$result = QUI::getDataBase()->fetch(array(
'select' => array(
'id'
),
'from' => $this->getDataBaseTableName(),
'where_or' => $whereOr,
'limit' => $gridParams['limit']
));
foreach ($result as $row) {
* Get IDs of all memberships that have specific groups assigned (OR)
public function getMembershipIdsByGroupIds($groupIds)
if (empty($groupIds)) {
return $ids;
}
$sql = 'SELECT `id` FROM ' . self::getDataBaseTableName();
$sql .= ' WHERE ';
$whereOr = array();
$binds = array();
foreach ($groupIds as $groupId) {
$whereOr[] = '`groupIds` LIKE :' . $groupId;
$binds[$groupId] = array(
'value' => '%,' . $groupId . ',%',
'type' => \PDO::PARAM_INT
);
}
$sql .= implode(" OR ", $whereOr);
$PDO = QUI::getDataBase()->getPDO();
$Stmt = $PDO->prepare($sql);
// bind search values
foreach ($binds as $var => $bind) {
$Stmt->bindValue(':' . $var, $bind['value'], $bind['type']);
}
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return array();
}
foreach ($result as $row) {
$ids[] = $row['id'];

Patrick Müller
committed
/**
* Get config entry for a membership setting
*
* @param string $key
* @return mixed
*/
public static function getSetting($key)
{
$Config = QUI::getPackage('quiqqer/memberships')->getConfig();
return $Config->get('memberships', $key);
}
*
* Get Memberships product category
*
* @return QUI\ERP\Products\Interfaces\CategoryInterface|false
*/
public static function getProductCategory()
{
$Conf = QUI::getPackage('quiqqer/memberships')->getConfig();
$categoryId = $Conf->get('products', 'categoryId');
if (empty($categoryId)) {
return false;
}
try {
return ProductCategories::getCategory((int)$categoryId);
} catch (\Exception $Exception) {
QUI\System\Log::addError(self::class . ' :: getProductCategory()');
QUI\System\Log::writeException($Exception);
return false;
}
}
/**
* Require: quiqqer/products
*
* Get quiqqer/products membership Field
*
* @return QUI\ERP\Products\Interfaces\FieldInterface|false
*/
public static function getProductField()
{
try {
return ProductFields::getField(MembershipField::FIELD_ID);
} catch (\Exception $Exception) {
QUI\System\Log::addError(self::class . ' :: getProductField()');
QUI\System\Log::writeException($Exception);
return false;
}
}