123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- namespace Ppb\Service;
- use Ppb\Db\Table\Users as UsersTable,
- Ppb\Service,
- Ppb\Model\Shipping as ShippingModel,
- Ppb\Db\Table\Row\User as UserModel,
- Cube\Db\Expr;
- class Users extends AbstractService
- {
-
- const ADMIN_ROLE_PRIMARY = 'Admin';
- const ADMIN_ROLE_MANAGER = 'Manager';
- protected static $_adminRoles = array(
- self::ADMIN_ROLE_PRIMARY => 'Administrator',
- self::ADMIN_ROLE_MANAGER => 'Manager',
- );
-
- const CUSTOM_FIELDS_TYPE = 'user';
-
- protected $_customFieldsData;
-
- protected $_paymentGateways;
-
- protected $_usersAddressBook;
-
- protected $_subscriptionTypes = array(
- 'UserVerification' => array(
- 'name' => 'User Verification Subscription',
- 'active' => 'user_verified',
- 'expirationDate' => 'user_verified_next_payment',
- 'emailFlag' => 'user_verified_email',
- 'updateMethod' => 'updateUserVerification',
- 'feesService' => '\Ppb\Service\Fees\UserVerification',
- 'renewalLink' => array('module' => 'app', 'controller' => 'payment', 'action' => 'user-verification'),
- 'managementLink' => array('module' => 'members', 'controller' => 'user', 'action' => 'verification'),
- ),
- 'StoreSubscription' => array(
- 'name' => 'Store Subscription',
- 'active' => 'store_active',
- 'expirationDate' => 'store_next_payment',
- 'emailFlag' => 'store_expiration_email',
- 'updateMethod' => 'updateStoreSubscription',
- 'feesService' => '\Ppb\Service\Fees\StoreSubscription',
- 'renewalLink' => array('module' => 'app', 'controller' => 'payment', 'action' => 'store-subscription'),
- 'managementLink' => array('module' => 'members', 'controller' => 'store', 'action' => 'setup'),
- ),
- );
-
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new UsersTable());
- }
-
- public function setCustomFieldsDataService(Service\CustomFieldsData $customFieldsData)
- {
- $this->_customFieldsData = $customFieldsData;
- return $this;
- }
-
- public function getCustomFieldsDataService()
- {
- if (!$this->_customFieldsData instanceof Service\CustomFieldsData) {
- $this->setCustomFieldsDataService(
- new Service\CustomFieldsData());
- }
- return $this->_customFieldsData;
- }
-
- public function setPaymentGateways(Service\Table\PaymentGateways $paymentGateways)
- {
- $this->_paymentGateways = $paymentGateways;
- return $this;
- }
-
- public function getPaymentGateways()
- {
- if (!$this->_paymentGateways instanceof Service\Table\PaymentGateways) {
- $this->setPaymentGateways(
- new Service\Table\PaymentGateways());
- }
- return $this->_paymentGateways;
- }
-
- public function setUsersAddressBook(Service\UsersAddressBook $addressBook)
- {
- $this->_usersAddressBook = $addressBook;
- return $this;
- }
-
- public function getUsersAddressBook()
- {
- if (!$this->_usersAddressBook instanceof Service\UsersAddressBook) {
- $this->setUsersAddressBook(
- new Service\UsersAddressBook());
- }
- return $this->_usersAddressBook;
- }
-
- public function setSubscriptionTypes($subscriptionTypes)
- {
- $this->_subscriptionTypes = $subscriptionTypes;
- return $this;
- }
-
- public function getSubscriptionTypes()
- {
- return $this->_subscriptionTypes;
- }
-
- public static function setAdminRoles($adminRoles)
- {
- self::$_adminRoles = $adminRoles;
- }
-
- public static function getAdminRoles()
- {
- return self::$_adminRoles;
- }
-
- public function findBy($name, $value, $enhanced = false)
- {
-
- $user = parent::findBy($name, $value);
- if (count($user) > 0) {
- $user->setAddress();
- if ($enhanced === true) {
-
- $customFieldsData = $this->getCustomFieldsData($user['id']);
- foreach ($customFieldsData as $key => $value) {
- $user['custom_field_' . $key] = $value;
- }
- }
- }
- return $user;
- }
-
- public function save($post, $userId = null)
- {
- $user = null;
- $data = $this->_prepareSaveData($post);
- if ($userId !== null) {
- $user = $this->findBy('id', $userId);
- }
- else if (array_key_exists('username', $post)) {
- $user = $this->findBy('username', $post['username']);
- }
- if (isset($post['name'])) {
- $post['first_name'] = (!empty($post['name']['first'])) ? $post['name']['first'] : '';
- $post['last_name'] = (!empty($post['name']['last'])) ? $post['name']['last'] : '';
- }
- if (count($user) > 0) {
- $data['updated_at'] = new Expr('now()');
- unset($data['username']);
- $this->_table->update($data, "id='{$user['id']}'");
- $id = $user['id'];
- }
- else {
- $data['created_at'] = new Expr('now()');
- if (!isset($data['postage_settings'])) {
- $data['postage_settings'] = serialize(array(
- ShippingModel::SETUP_SHIPPING_LOCATIONS => ShippingModel::POSTAGE_LOCATION_DOMESTIC,
- ShippingModel::SETUP_POSTAGE_TYPE => ShippingModel::POSTAGE_TYPE_ITEM,
- ShippingModel::SETUP_FREE_POSTAGE => 0,
- ));
- }
- $settings = $this->getSettings();
- $data['balance'] = (-1) * $settings['signup_credit'];
- $data['max_debit'] = doubleval($settings['maximum_debit']);
- $data['account_mode'] = $settings['payment_mode'];
- $this->_table->insert($data);
- $id = $this->_table->getAdapter()->lastInsertId();
- $user = $this->findBy('id', $id);
- }
- if (!empty($post['password'])) {
- $this->savePassword($user, $post['password']);
- }
- if (!isset($post['partial'])) {
-
- foreach ($post as $key => $value) {
- if (strstr($key, 'custom_field_')) {
- $fieldId = str_replace('custom_field_', '', $key);
- $this->getCustomFieldsDataService()->save(
- $value, self::CUSTOM_FIELDS_TYPE, $fieldId, $id);
- }
- }
-
- $gatewayFields = $this->getPaymentGateways()->getDirectPaymentFields();
- foreach ($gatewayFields as $key => $gatewayField) {
- $gatewayFields[$key]['user_id'] = $id;
- if (array_key_exists($gatewayField['name'], $post)) {
- $gatewayFields[$key]['value'] = $post[$gatewayField['name']];
- }
- }
- foreach ((array)$gatewayFields as $gatewayField) {
- $this->getPaymentGateways()->getPaymentGatewaysSettings()->save($gatewayField);
- }
-
- if (array_intersect($this->getUsersAddressBook()->getAddressFields(), array_keys($post))) {
- $this->getUsersAddressBook()->save($post, $id);
- }
- }
- return $id;
- }
-
- public function hashPassword($password, $salt)
- {
- return hash('sha256', $password . $salt);
- }
-
- public function savePassword(UserModel $user, $password)
- {
- $salt = date('U', time());
- $password = $this->hashPassword($password, $salt);
- $user->save(array(
- 'password' => $password,
- 'salt' => $salt,
- ));
- return $this;
- }
-
- public function delete($userId)
- {
- $where = $this->_table->getAdapter()->quoteInto('id = ?', $userId);
- return $this->_table->delete($where);
- }
-
- public function generateRegistrationKey($id, $username)
- {
- $hash = md5(uniqid(time()));
- return substr(
- hash('sha256', $id . $username . $hash), 0, 10);
- }
-
- public function verifyEmailAddress($key)
- {
- return (bool)$this->_table->update(array('mail_activated' => 1), "registration_key='{$key}'");
- }
-
- public function newsletterUnsubscribe($username, $email)
- {
- return (bool)$this->_table->update(array('newsletter_subscription' => 0), "username='{$username}' AND email='{$email}'");
- }
-
- public function getCustomFieldsData($id)
- {
- $result = array();
-
- $rowset = $this->getCustomFieldsDataService()->fetchAll(
- $this->getCustomFieldsDataService()->getTable()->select('value, field_id')
- ->where('type = ?', self::CUSTOM_FIELDS_TYPE)
- ->where('owner_id = ?', (int)$id));
- foreach ($rowset as $row) {
- $result[$row['field_id']] = \Ppb\Utility::unserialize($row['value']);
- }
- return $result;
- }
-
- protected function _prepareSaveData($data = array())
- {
- if (isset($data['id'])) {
- unset($data['id']);
- }
- if (isset($data['password'])) {
- unset($data['password']);
- }
- if (isset($data['salt'])) {
- unset($data['salt']);
- }
- return parent::_prepareSaveData($data);
- }
- }
|