/**
  * Get widget content
  *
  * @return string|boolean
  */
 public function getContent()
 {
     $userId = UserIdentityService::getCurrentUserIdentity()['user_id'];
     // process post actions
     if ($this->getRequest()->isPost() && ApplicationCsrfUtility::isTokenValid($this->getRequest()->getPost('csrf')) && $this->getRequest()->getPost('form_name') == 'transactions') {
         $transactions = $this->getRequest()->getPost('transactions');
         if ($transactions && is_array($transactions)) {
             switch ($this->getRequest()->getQuery('action')) {
                 // delete selected transactions
                 case 'delete':
                     return $this->deleteTransactions($transactions, $userId);
                 default:
             }
         }
     }
     // get pagination options
     list($pageParamName, $perPageParamName, $orderByParamName, $orderTypeParamName) = $this->getPaginationParams();
     $page = $this->getView()->applicationRoute()->getQueryParam($pageParamName, 1);
     $perPage = $this->getView()->applicationRoute()->getQueryParam($perPageParamName);
     $orderBy = $this->getView()->applicationRoute()->getQueryParam($orderByParamName);
     $orderType = $this->getView()->applicationRoute()->getQueryParam($orderTypeParamName);
     $filters = [];
     $fieldsPostfix = '_' . $this->widgetConnectionId;
     // get a filter form
     $filterForm = $this->getServiceLocator()->get('Application\\Form\\FormManager')->getInstance('Payment\\Form\\PaymentUserTransactionFilter')->setFieldsPostfix($fieldsPostfix);
     $request = $this->getRequest();
     $filterForm->getForm()->setData($request->getQuery(), false);
     // validate the filter form
     if ($this->getRequest()->isXmlHttpRequest() || $this->getView()->applicationRoute()->getQueryParam('form_name') == $filterForm->getFormName()) {
         // check the filter form validation
         if ($filterForm->getForm()->isValid()) {
             $filters = $filterForm->getData();
         }
     }
     // get data
     $paginator = $this->getModel()->getUserTransactions($userId, $page, $perPage, $orderBy, $orderType, $filters, $fieldsPostfix);
     $dataGridWrapper = 'transactions-page-wrapper';
     // get data grid
     $dataGrid = $this->getView()->partial('payment/widget/transaction-history', ['current_currency' => PaymentService::getPrimaryCurrency(), 'payment_types' => $this->getModel()->getPaymentsTypes(false, true), 'filter_form' => $filterForm->getForm(), 'paginator' => $paginator, 'order_by' => $orderBy, 'order_type' => $orderType, 'per_page' => $perPage, 'page_param_name' => $pageParamName, 'per_page_param_name' => $perPageParamName, 'order_by_param_name' => $orderByParamName, 'order_type_param_name' => $orderTypeParamName, 'widget_connection' => $this->widgetConnectionId, 'widget_position' => $this->widgetPosition, 'data_grid_wrapper' => $dataGridWrapper]);
     if ($this->getRequest()->isXmlHttpRequest()) {
         return $dataGrid;
     }
     return $this->getView()->partial('payment/widget/transaction-history-wrapper', ['data_grid_wrapper' => $dataGridWrapper, 'data_grid' => $dataGrid]);
 }
 /**
  * Get primary currency
  *
  * @return array
  */
 public function getPrimaryCurrency()
 {
     return PaymentService::getPrimaryCurrency();
 }
 /**
  * Add a new transaction
  *
  * @param integer $userId
  * @param array $transactionInfo
  *      integer payment_type - required
  *      string comments - optional
  *      string first_name - required
  *      string last_name - required
  *      string email - required
  *      string phone - required
  *      string address - optional
  * @param array $items
  *      integer object_id
  *      integer module
  *      string title
  *      string slug
  *      float cost
  *      float discount
  *      integer count
  * @return integer|string
  */
 public function addTransaction($userId, array $transactionInfo, array $items)
 {
     try {
         $this->adapter->getDriver()->getConnection()->beginTransaction();
         $basicData = ['user_hidden' => self::TRANSACTION_USER_NOT_HIDDEN, 'paid' => self::TRANSACTION_NOT_PAID, 'language' => $this->getCurrentLanguage(), 'date' => time(), 'currency' => PaymentService::getPrimaryCurrency()['id']];
         // add the user id
         if (UserBaseModel::DEFAULT_GUEST_ID != $userId) {
             $basicData['user_id'] = $userId;
         }
         // add the discount id
         if (PaymentService::getDiscountCouponInfo()) {
             $basicData['discount_coupon'] = PaymentService::getDiscountCouponInfo()['id'];
         }
         if (!$transactionInfo['comments']) {
             $transactionInfo['comments'] = null;
         }
         $insert = $this->insert()->into('payment_transaction_list')->values(array_merge($transactionInfo, $basicData));
         $statement = $this->prepareStatementForSqlObject($insert);
         $statement->execute();
         $transactionId = $this->adapter->getDriver()->getLastGeneratedValue();
         // generate a random slug
         $update = $this->update()->table('payment_transaction_list')->set(['slug' => strtoupper($this->generateSlug($transactionId, $this->generateRandString(self::TRANSACTION_MIN_SLUG_LENGTH, self::ALLOWED_SLUG_CHARS), 'payment_transaction_list', 'id'))])->where(['id' => $transactionId]);
         $statement = $this->prepareStatementForSqlObject($update);
         $statement->execute();
         // update the discount coupon info
         if (PaymentService::getDiscountCouponInfo()) {
             $update = $this->update()->table('payment_discount_coupon')->set(['used' => self::COUPON_USED])->where(['id' => PaymentService::getDiscountCouponInfo()['id']]);
             $statement = $this->prepareStatementForSqlObject($update);
             $statement->execute();
         }
         // add  transaction's items
         foreach ($items as $item) {
             $insert = $this->insert()->into('payment_transaction_item')->values(['transaction_id' => $transactionId, 'object_id' => $item['object_id'], 'module' => $item['module'], 'title' => $item['title'], 'slug' => $item['slug'], 'cost' => $item['cost'], 'discount' => $item['discount'], 'count' => $item['count'], 'paid' => self::TRANSACTION_NOT_PAID, 'extra_options' => !empty($item['extra_options']) ? $item['extra_options'] : null]);
             $statement = $this->prepareStatementForSqlObject($insert);
             $statement->execute();
         }
         $this->adapter->getDriver()->getConnection()->commit();
     } catch (Exception $e) {
         $this->adapter->getDriver()->getConnection()->rollback();
         ApplicationErrorLogger::log($e);
         return $e->getMessage();
     }
     // fire the add payment transaction event
     PaymentEvent::fireAddPaymentTransactionEvent($transactionId, $transactionInfo);
     return $transactionId;
 }