protected function _beforeRender(KControllerContextInterface $context) { $controller = $context->getSubject(); $request = $controller->getRequest(); foreach ($this->_vars as $name => $param) { if (is_numeric($name)) $name = $param; $method = '_set' . ucfirst($name); if (method_exists($this, $method)) { call_user_func_array(array($this, $method), array($request, $param)); } else { $request->query->set($name, $this->_params->get($param)); } } if ($request->getFormat() == 'rss') { $query = $request->getQuery(); $states = array('limit' => 20, 'offset' => 0, 'sort' => 'created_on', 'direction' => 'desc'); foreach ($states as $name => $value) { $query->set($name, $value); // Set as internal. $controller->getModel()->getState()->setProperty($name, 'internal', true); } } // Update the model state. $controller->getModel()->setState($request->getQuery()->toArray()); }
public function _actionRender(KControllerContextInterface $context) { $data = array('url' => $context->getRequest()->query->get('url', 'url'), 'content-length' => false); if (!function_exists('curl_init')) { throw new RuntimeException('Curl library does not exist', KHttpResponse::SERVICE_UNAVAILABLE); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $data['url']); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 20); //CURLOPT_NOBODY changes the request from GET to HEAD curl_setopt($ch, CURLOPT_NOBODY, true); $response = curl_exec($ch); if (curl_errno($ch)) { throw new RuntimeException('Curl Error: ' . curl_error($ch), KHttpResponse::SERVICE_UNAVAILABLE); } $info = curl_getinfo($ch); if (isset($info['http_code']) && $info['http_code'] != 200) { throw new RuntimeException($data['url'] . ' Not Found', $info['http_code']); } if (isset($info['download_content_length'])) { $data['content-length'] = $info['download_content_length']; } curl_close($ch); //Set the data in the response $context->response->setContent(json_encode($data), 'application/json'); return $data; }
protected function _afterRead(KControllerContextInterface $context) { if ($context->result->locale !== $this->_locale) { $message = 'COM_TRANSLATIONS_ONLY_AVAILABLE_IN_' . strtoupper(substr($context->result->locale, 0, 2)); $context->getResponse()->addMessage($this->getObject('translator')->translate($message), KControllerResponseInterface::FLASH_WARNING); } }
public function prepareGatewayPaypal_express_checkout(KControllerContextInterface $context) { //TODO: Check if cart is not empty! $data = $context->request->data; $entity = $context->result; $storage = $this->getPayum()->getStorage('ComSalesModelEntityPayment'); $payment = $storage->create(); $payment->setNumber(uniqid()); $payment->setCurrencyCode($entity->currency_code); $payment->setClientEmail($data->email); $items = array(); $index = 0; foreach ($entity->cart->items as $item) { $items['L_PAYMENTREQUEST_0_NAME' . $index] = $item['name']; $items['L_PAYMENTREQUEST_0_AMT' . $index] = number_format($item['price'], 2); $items['L_PAYMENTREQUEST_0_QTY' . $index] = $item['quantity']; $index++; } //TODO: Improve! $entity->discount = $entity->discount ? $entity->discount : new KObjectConfig(array()); $entity->discount->append(array('coupons' => array(), 'amount' => 0)); foreach ($entity->discount->coupons as $coupon) { $items['L_PAYMENTREQUEST_0_NAME' . $index] = $coupon->title; $items['L_PAYMENTREQUEST_0_DESC' . $index] = '20% discount'; $items['L_PAYMENTREQUEST_0_AMT' . $index] = number_format(-$coupon->amount, 2); $items['L_PAYMENTREQUEST_0_QTY' . $index] = 1; $index++; } $payment->setTotalAmount(($entity->cart['total_price'] - $entity->discount['amount']) * 100); $payment->setDetails(array_merge($items, array('uuid' => $entity->uuid, 'referrer' => urlencode($context->request->getReferrer()), 'transaction_id' => 'N/A', 'payment_type' => 'PayPal Express Checkout', 'account_info' => 'N/A'))); $storage->update($payment); $context->getSubject()->setPayment($payment); }
protected function _zaad(KControllerContextInterface $context) { $referrer = (string) $context->getRequest()->getReferrer(); $uri = JURI::getInstance($referrer); $router = clone JFactory::getApplication()->getRouter(); $query = $router->parse($uri); $referrer = $this->getObject('lib:http.url', array('url' => JURI::base() . 'index.php?' . http_build_query($query))); $context->getUser()->getSession()->set('referrer', urlencode($referrer)); }
/** * Load admin language file in forms since some of the layouts are shared * * @param KControllerContextInterface $context */ protected function _beforeRender(KControllerContextInterface $context) { $request = $context->getRequest(); if ($request->getFormat() === 'html' && $request->query->view === 'document' && $this->getView()->getLayout() === 'form') { $this->getObject('translator')->load('com://admin/docman'); } }
/** * Create an Invoice * * @todo define an array of entity fields to map to invoice fields * * @param KControllerContextInterface $context * * @return void */ protected function _afterAdd(KControllerContextInterface $context) { $entity = $context->result; // Order entity $data = array('package' => $context->getSubject()->getIdentifier()->package, 'customer_id' => $entity->account_number, 'order_id' => $entity->id, 'product_name' => $entity->package_name, 'amount' => $entity->package_price); $controller = $this->getObject($this->_controller); if ($controller instanceof KControllerModellable) { $controller->add($data); } }
/** * Avoid getting redirected to the configs view. It doesn't exist. */ protected function _setRedirect(KControllerContextInterface $context) { $response = $context->getResponse(); if ($response->isRedirect()) { $url = $response->getHeaders()->get('Location'); if (strpos($url, 'view=configs') !== false) { $response->setRedirect(str_replace('view=configs', 'view=documents', $url)); } } }
protected function _checkUser(KControllerContextInterface $context) { if (!$context->user->isAuthentic()) { $message = $this->getObject('translator')->translate('You need to be logged in to access your document list'); $url = $context->getRequest()->getUrl(); $redirect = JRoute::_('index.php?option=com_users&view=login&return='.base64_encode($url), false); JFactory::getApplication()->redirect($redirect, $message, 'error'); } }
/** * Avoid getting redirected to the configs view. It doesn't exist. */ protected function _setRedirect(KControllerContextInterface $context) { $response = $context->getResponse(); if ($response->isRedirect()) { if ($context->action == 'cancel') { $response->setRedirect(JURI::base()); } else { $response->setRedirect('index.php?option=com_ohanah&view=events'); } } }
protected function _actionDone(KControllerContextInterface $context) { $token = $this->getHttpRequestVerifier()->verify($context->getRequest()->getQuery()->toArray()); $identity = $token->getDetails(); $model = $this->getPayum()->getStorage($identity->getClass())->find($identity); $this->setGateway($token->getGatewayName()); $gateway = $this->getPayum()->getGateway($token->getGatewayName()); $gateway->execute($status = new GetHumanStatus($model)); $context->params = array('status' => $status, 'token' => $token); $this->getHttpRequestVerifier()->invalidate($token); $this->execute($status->getValue(), $context); }
protected function _actionCapture(KControllerContextInterface $context) { $token = $this->getHttpRequestVerifier()->verify($context->getRequest()->getQuery()->toArray()); $gateway = $this->getPayum()->getGateway($token->getGatewayName()); if ($reply = $gateway->execute(new Capture($token), true)) { if ($reply instanceof HttpRedirect) { header("Location: " . $reply->getUrl()); die; } throw new \LogicException('Unsupported reply', null, $reply); } $this->getHttpRequestVerifier()->invalidate($token); $context->getResponse()->setRedirect($token->getAfterUrl()); }
public function prepareGatewayOffline(KControllerContextInterface $context) { //TODO: Check if cart is not empty! $data = $context->request->data; $entity = $context->result; $storage = $this->getPayum()->getStorage('ComSalesModelEntityPayment'); $payment = $storage->create(); $payment->setNumber(uniqid()); $payment->setCurrencyCode($entity->currency_code); $payment->setClientEmail($data->email); $payment->setTotalAmount($entity->total_price); $payment->setDetails(array('uuid' => $entity->uuid, 'referrer' => urlencode($context->request->getReferrer()), 'transaction_id' => 'N/A', 'payment_type' => 'Offline', 'account_info' => 'N/A', 'paid' => 0)); $storage->update($payment); $context->getSubject()->setPayment($payment); }
public function prepareGatewayPaypal_pro_checkout(KControllerContextInterface $context) { //TODO: Check if cart is not empty! $data = $context->request->data; $entity = $context->result; $storage = $this->getPayum()->getStorage('ComSalesModelEntityPayment'); $payment = $storage->create(); $payment->setNumber(uniqid()); $payment->setCurrencyCode($entity->currency_code); $payment->setTotalAmount(number_format($entity->cart->total_price - $entity->discount->amount, 2, '.', '')); $payment->setClientEmail($data->email); $payment->setDetails(array('uuid' => $entity->uuid, 'referrer' => urlencode($context->request->getReferrer()), 'payment_type' => 'Credit Card', 'ACCT' => preg_replace('/\\s+/', '', $data->cc_num), 'CVV2' => $data->cc_cvc, 'EXPDATE' => $data->cc_exp_month . $data->cc_exp_year, 'NAME' => isset($data->card_holders_name) ? $data->card_holders_name : null, 'COMMENT1' => isset($data->comment_1) ? $data->comment_1 : null, 'COMMENT2' => isset($data->comment_2) ? $data->comment_2 : null, 'FIRSTNAME' => isset($data->first_name) ? $data->first_name : null, 'LASTNAME' => isset($data->last_name) ? $data->last_name : null, 'EMAIL' => isset($data->email) ? $data->email : null, 'PHONENUM' => isset($data->phone_number) ? $data->phone_number : null, 'STREET' => isset($data->street_address) ? $data->street_address : null, 'CITY' => isset($data->city) ? $data->city : null, 'STATE' => isset($data->state) ? $data->state : null, 'ZIP' => isset($data->zip_code) ? $data->zip_code : null)); $storage->update($payment); $context->getSubject()->setPayment($payment); }
protected function _beforeEdit(KControllerContextInterface $context) { $translator = $this->getObject('translator'); $data = $context->getRequest()->getData(); $model = $this->getModel(); $table = $model->getTable(); $entity = clone $model->fetch(); $fields = $entity->hasProperty('fields') ? $entity->fields : array(); $messages = array(); $parameters = array_merge($entity->getParameters()->toArray(), $data->has('parameters') ? $data->parameters : array()); foreach ($fields as $field) { if ($field->editable === false) { unset($parameters[$field->property]); continue; } if ($field->required) { switch ($field->element) { case 'email': if (!$parameters[$field->property]) { if (!$this->getObject('lib:filter.email')->validate($parameters[$field->property])) { $messages[] = $translator->translate('Please enter a valid E-mail address'); } } break; case 'boolean': if (!is_bool($parameters[$field->property])) { if (!is_numeric($parameters[$field->property])) { $messages[] = $translator->translate($field->label) . ' ' . $translator->translate('is a required field'); } $parameters[$field->property] = (bool) $parameters[$field->property]; } break; default: if (!$parameters[$field->property]) { $messages[] = $translator->translate($field->label) . ' ' . $translator->translate('is a required field'); } } } } $data->parameters = $parameters; if ($messages) { foreach ($messages as $message) { $context->getResponse()->addMessage($message, KControllerResponseInterface::FLASH_ERROR); } //TODO: Improve handeling error! $context->getRequest()->setData(array()); } }
protected function _actionPrepare(KControllerContextInterface $context) { $message = ''; $type = KControllerResponseInterface::FLASH_SUCCESS; if ($this->hasHandler()) { $gateway_name = $context->getRequest()->getData()->payment_gateway; $method = 'prepareGateway' . ucfirst($gateway_name); $this->{$method}($context); $captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken($gateway_name, $this->getPayment(), 'payment_done'); $location = $captureToken->getTargetUrl(); } else { $location = $context->getRequest()->getReferrer(); $message = 'No sales plugin enabled for: ' . $this->getRequestComponent(); $type = KControllerResponseInterface::FLASH_ERROR; } $context->getResponse()->setRedirect($location, $message, $type); }
protected function _afterAdd(KControllerContextInterface $context) { if ($context->getResponse()->getStatusCode() == KHttpResponse::CREATED) { $page = JFactory::getApplication()->getMenu()->getItem($this->getRequest()->query->Itemid); $translator = $this->getObject('translator'); $emails = $page->params->get('notification_emails'); if (!empty($emails)) { $emails = explode("\n", $emails); $config = JFactory::getConfig(); $from_name = $config->get('fromname'); $mail_from = $config->get('mailfrom'); $sitename = $config->get('sitename'); $subject = $translator->translate('A new document was submitted for you to review on {sitename}', array( 'sitename' => $sitename )); $admin_link = JURI::root().'administrator/index.php?option=com_docman&view=documents'; $title = $context->result->title; $admin_title = $translator->translate('Document Manager'); foreach ($emails as $email) { $body = $translator->translate('Submit notification mail body', array( 'title' => $title, 'sitename' => $sitename, 'url' => $admin_link, 'url_text' => $admin_title )); JFactory::getMailer()->sendMail($mail_from, $from_name, $email, $subject, $body, true); } } } }
/** * Execute an action by triggering a method in the derived class. * * @param string $action The action to execute * @param KControllerContextInterface $context A command context object * @throws Exception * @throws BadMethodCallException * @return mixed|bool The value returned by the called method, false in error case. */ public function execute($action, KControllerContextInterface $context) { $action = strtolower($action); //Set the context subject $context_subject = $context->getSubject(); $context->setSubject($this); //Set the context action $context_action = $context->getAction(); $context->setAction($action); //Execute the action if ($this->invokeCommand('before.' . $action, $context) !== false) { $method = '_action' . ucfirst($action); if (!method_exists($this, $method)) { if (isset($this->_mixed_methods[$action])) { $context->setName('action.' . $action); $context->result = $this->_mixed_methods[$action]->execute($context, $this->getCommandChain()); } else { throw new KControllerExceptionActionNotImplemented("Can't execute '{$action}', method: '{$method}' does not exist"); } } else { $context->result = $this->{$method}($context); } $this->invokeCommand('after.' . $action, $context); } //Reset the context $context->setSubject($context_subject); $context->setAction($context_action); return $context->result; }
protected function _afterEdit(KControllerContextInterface $context) { $status_code = $context->getResponse()->getStatusCode(); if ($status_code < 200 || $status_code >= 300) { return; } if ($this->_regenerate) { foreach ($context->result as $entity) { $this->_saveThumbnail($entity); } } }
protected function _cleanUp(KControllerContextInterface $context) { if ($context->getResponse()->getStatusCode() !== KHttpResponse::CREATED) { try { if ($this->_uploaded instanceof KModelEntityInterface) { $this->_uploaded->delete(); } } catch (Exception $e) { // Well, we tried } } }
protected function _afterAttach(KControllerContextInterface $context) { $context->getResponse()->setStatus(KHttpResponse::NO_CONTENT); }
/** * Load the model state from the request * * This functions merges the request information with any model state information that was saved in the session and * returns the result. * * @param KControllerContextInterface $context The active controller context * @return void */ protected function _beforeBrowse(KControllerContextInterface $context) { $query = $context->getRequest()->query; $query->add((array) $context->user->get($this->_getStateKey($context))); $this->getModel()->getState()->setValues($query->toArray()); }
/** * Entity slug has been changed which means the old URL will lead to 404. Rewrite it. * @todo this needs to be cleaned up * @param KControllerContextInterface $context */ protected function _updateReferrer(KControllerContextInterface $context) { $entity = $context->result; if ($entity instanceof KModelEntityInterface && $this->_old_slug && $this->_old_slug !== $entity->slug) { $referrer = $context->getRequest()->getReferrer(); $old_alias = $entity->id.'-'.$this->_old_slug; $new_alias = $entity->id.'-'.$entity->slug; if (strpos($referrer, $old_alias) !== false) { $referrer = strtr($referrer, array( $old_alias => $new_alias, $this->_old_slug => $entity->slug )); // Set the referrer in request and as the redirect $context->request->setReferrer($referrer); } $referrer_cookie = $this->getReferrer($context); if (strpos($referrer_cookie, $old_alias) !== false) { // Also update referrer stored in the cookie $referrer_cookie = strtr($referrer_cookie, array( $old_alias => $new_alias, $this->_old_slug => $entity->slug )); // Unset the old referrer since it's a 404 now $this->_unsetReferrer($context); // Temporarily change the referrer so that setReferrer can grab it $context->request->setReferrer($referrer_cookie); $this->setReferrer($context); $context->request->setReferrer($referrer); } if ($context->action === 'apply' || $entity->getStatus() === KModelEntityInterface::STATUS_FAILED) { $context->response->setRedirect($referrer); } else { $context->response->setRedirect($referrer_cookie); } } }