Newer
Older
<?php
namespace QUI\Memberships;
use QUI\CRUD\Factory;
use QUI\Utils\Grid;
use QUI;
class Handler extends Factory
{
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())
{
$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';
/**
* Get membership
*
* @param int $id
* @return Membership
*/
public function getChild($id)
{
return parent::getChild($id);
}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @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',
'autoRenew',
'editDate',
'editUser',
// these fields require quiqqer/order
'paymentInterval',
'netPrice',
'netPriceOffer',
// 'grossPrice',
'paymentMethodIds'
// @todo additional fields for quiqqer/contracts
);
}
/**
* Search memberships
*
* @param array $searchParams
* @param bool $countOnly (optional) - get count for search result only [default: false]
131
132
133
134
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
*/
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 list of all packages that are relevant for quiqqer/memberships
* and that are currently installed
*
* @return array
*/
public function getInstalledMembershipPackages()
{
$packages = array();
$relevantPackages = array(
'quiqqer/products',
'quiqqer/contracts'
);
foreach ($relevantPackages as $package) {
try {
QUI::getPackage($package);
$packages[] = $package;
} catch (\Exception $Exception) {
}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
return $packages;
}
/**
* Get IDs of all memberships that have a specific group assigned
*
* @param int $groupId
* @return int[]
*/
public function getMembershipIdsByGroupId($groupId)
{
$ids = array();
$result = QUI::getDataBase()->fetch(array(
'select' => array(
'id'
),
'from' => self::getDataBaseTableName(),
'where' => array(
'groupIds' => array(
'type' => '%LIKE%',
'value' => ',' . $groupId . ','
)
)
));
foreach ($result as $row) {
$ids[] = $row['id'];