Skip to content
Snippets Groups Projects
Commit 21e91e75 authored by ErikHommel's avatar ErikHommel
Browse files

wip completed basic form flow

parent db09d620
No related branches found
No related tags found
No related merge requests found
Showing
with 1121 additions and 170 deletions
......@@ -4,4 +4,23 @@ use CRM_Simpleorders_ExtensionUtil as E;
class CRM_Simpleorders_BAO_SoItem extends CRM_Simpleorders_DAO_SoItem {
/**
* Method to get a list of items
* @return array
*/
public function getItemList(): array {
$itemList = [];
try {
$soItems = \Civi\Api4\SoItem::get(FALSE)
->addSelect('id', 'name', 'item_type:label')
->execute();
foreach ($soItems as $soItem) {
$itemList[$soItem['id']] = $soItem['name'] . "(" . $soItem['item_type:label'] . ")";
}
}
catch (\CRM_Core_Exception $ex) {
}
return $itemList;
}
}
......@@ -4,4 +4,86 @@ use CRM_Simpleorders_ExtensionUtil as E;
class CRM_Simpleorders_BAO_SoOrder extends CRM_Simpleorders_DAO_SoOrder {
/**
* Method to get SoOrder with Order Contacts
*
* @param int $soOrderId
* @return array
*/
public function getSoOrderWithOrderContacts(int $soOrderId): array {
$soOrderData = [];
try {
$soOrder = \Civi\Api4\SoOrder::get(FALSE)
->addSelect('*', 'campaign_id.title', 'state_id.name', 'country_id.name')
->addWhere('id', '=', $soOrderId)
->execute()->first();
if (isset($soOrder['id'])) {
$soOrderData = $soOrder;
$soOrderContacts = \Civi\Api4\SoOrderContact::get(FALSE)
->addWhere('order_id', '=', $soOrderId)
->execute();
foreach ($soOrderContacts as $soOrderContact) {
$soOrderData[$soOrderContact['role']] = $soOrderContact['contact_id'];
}
}
}
catch (\CRM_Core_Exception $ex) {
}
return $soOrderData;
}
public function createSoOrder(array $soOrderValues): ?int {
$soOrderId = NULL;
$apiValues = $this->setApiValues($soOrderValues);
try {
$soOrder = \Civi\Api4\SoOrder::create(FALSE)
->setValues($apiValues)->execute()->first();
if (isset($soOrder['id'])) {
$soOrderId = $soOrder['id'];
$soOrderContact = new CRM_Simpleorders_BAO_SoOrderContact();
$soOrderContact->addSoOrderContactsFromOrder($soOrderId, $soOrderValues);
}
}
catch (\CRM_Core_Exception $ex) {
}
return $soOrderId;
}
public function updateSoOrder(int $soOrderId, array $soOrderValues): void {
$apiValues = $this->setApiValues($soOrderValues);
try {
$soOrder = \Civi\Api4\SoOrder::update(FALSE)
->addWhere('id', '=', $soOrderId)
->setValues($apiValues)->execute()->first();
if (isset($soOrder['id'])) {
$soOrderId = $soOrder['id'];
$soOrderContact = new CRM_Simpleorders_BAO_SoOrderContact();
$soOrderContact->addSoOrderContactsFromOrder($soOrderId, $soOrderValues);
}
}
catch (\CRM_Core_Exception $ex) {
}
}
/**
* Method to set the api values to create an order
*
* @param array $soOrderValues
* @return array
*/
private function setApiValues(array $soOrderValues): array {
$apiValues = [];
try {
$soOrderFields = \Civi\Api4\SoOrder::getFields(FALSE)
->addWhere('name', '!=', 'id')
->execute();
foreach ($soOrderFields as $soOrderField) {
if (isset($soOrderValues[$soOrderField['name']])) {
$apiValues[$soOrderField['name']] = $soOrderValues[$soOrderField['name']];
}
}
}
catch (\CRM_Core_Exception $ex) {
}
return $apiValues;
}
}
......@@ -4,4 +4,85 @@ use CRM_Simpleorders_ExtensionUtil as E;
class CRM_Simpleorders_BAO_SoOrderContact extends CRM_Simpleorders_DAO_SoOrderContact {
/**
* Method to count the number of orders for a contact
* @param int $contactId
* @return int
*/
public function countContactOrders(int $contactId): int {
$noOfOrders = 0;
try {
$noOfOrders = \Civi\Api4\SoOrderContact::get(FALSE)
->addWhere('contact_id', '=', $contactId)
->execute()->count();
}
catch (\CRM_Core_Exception $ex) {
}
return $noOfOrders;
}
/**
* Method to process the contacts from the order values from the order form
*
* @param int $soOrderId
* @param array $soOrderValues
* @return void
*/
public function addSoOrderContactsFromOrder(int $soOrderId, array $soOrderValues): void {
$service = simpleorders_get_service();
$firstContact = $service->getFirstContactName() . "_contact_id";
if (isset($soOrderValues[$firstContact])) {
$this->createSoOrderContact($soOrderId, $service->getFirstContactName(), $soOrderValues[$firstContact]);
}
$secondContact = $service->getSecondContactName() . "_contact_id";
if (isset($soOrderValues[$secondContact])) {
$this->createSoOrderContact($soOrderId, $service->getSecondContactName(), $soOrderValues[$secondContact]);
}
}
/**
* Method to add so order contact
*
* @param int $soOrderId
* @param string $role
* @param int $contactId
* @return void
*/
public function createSoOrderContact(int $soOrderId, string $role, int $contactId): void {
if (!$this->isExistingSoOrderContact($soOrderId, $contactId)) {
try {
\Civi\Api4\SoOrderContact::create(FALSE)
->addValue('contact_id', $contactId)
->addValue('role', $role)
->addValue('order_id', $soOrderId)
->execute();
}
catch (\CRM_Core_Exception $e) {
}
}
}
/**
* Method to check if order contact already exists
*
* @param int $soOrderId
* @param int $contactId
* @return bool
*/
public function isExistingSoOrderContact(int $soOrderId, int $contactId): bool {
$exists = FALSE;
try {
$count = \Civi\Api4\SoOrderContact::get(FALSE)
->addWhere('contact_id', '=', $contactId)
->addWhere('order_id', '=', $soOrderId)
->execute()->count();
if ($count > 0) {
$exists = TRUE;
}
}
catch (\CRM_Core_Exception $ex) {
}
return $exists;
}
}
......@@ -4,4 +4,61 @@ use CRM_Simpleorders_ExtensionUtil as E;
class CRM_Simpleorders_BAO_SoOrderItem extends CRM_Simpleorders_DAO_SoOrderItem {
/**
* Method to get the quantity for an order item
* @param int $soOrderItemId
* @return int
*/
public function getSoOrderItem(int $soOrderItemId): array {
$data = [];
try {
$soOrderItem = \Civi\Api4\SoOrderItem::get(FALSE)
->addSelect('id', 'item_id', 'quantity', 'item_id.name')
->addWhere('id', '=', $soOrderItemId)
->execute()->first();
if (isset($soOrderItem['id'])) {
$data = $soOrderItem;
}
}
catch (\CRM_Core_Exception $ex) {
}
return $data;
}
/**
* Method to create a new order item
*
* @param array $values
* @return void
*/
public function createSoOrderItem(array $values): void {
try {
\Civi\Api4\SoOrderItem::create(FALSE)
->addValue('order_id', $values['so_order_id'])
->addValue('item_id', $values['item_id'])
->addValue('quantity', $values['quantity'])
->execute();
}
catch (\CRM_Core_Exception $ex) {
}
}
/**
* Method to update an order item
*
* @param int $soOrderItemId
* @param array $values
* @return void
*/
public function updateSoOrderItem(int $soOrderItemId, array $values): void {
try {
\Civi\Api4\SoOrderItem::update(FALSE)
->addWhere('id', '=', $soOrderItemId)
->addValue('quantity', $values['quantity'])
->execute();
}
catch (\CRM_Core_Exception $ex) {
}
}
}
......@@ -9,74 +9,218 @@ use CRM_Simpleorders_ExtensionUtil as E;
*/
class CRM_Simpleorders_Form_SoOrder extends CRM_Core_Form {
private int $_soOrderId;
private int $_contactId;
private array $_currentData = [];
/**
* @throws \CRM_Core_Exception
* Overridden method to build form
*
* @return void
* @throws CRM_Core_Exception
*/
public function buildQuickForm(): void {
// add form elements
$this->add(
'select', // field type
'favorite_color', // field name
'Favorite Color', // field label
$this->getColorOptions(), // list of options
TRUE // is required
);
$service = simpleorders_get_service();
$this->add('hidden', 'so_order_id');
$this->add('hidden', 'contact_id');
$this->addEntityRef('campaign_id', E::ts("Campaign"), [
'entity' => 'campaign',
'multiple' => FALSE,
'api' => ['params' => ['is_active' => TRUE]],
'placeholder' => E::ts('- select campaign -'),
'select' => ['minimumInputLength' => 2],
]);
$this->addContactElements();
$this->add('datepicker', 'order_date', E::ts("Order Date"), [], TRUE, ['time' => FALSE]);
$this->add('text', 'delivery_address', E::ts("Delivery Address"), ['size' => CRM_Utils_Type::HUGE], TRUE);
$this->add('text', 'postcode', E::ts("Postcode"), [], TRUE);
$this->add('text', 'city', E::ts("City"), [], TRUE);
$this->addStateAndCountry();
$this->add('text', 'source', E::ts("Source of the Order"));
$this->add('checkbox', 'forwarded', E::ts('Order forwarded?'));
$this->addButtons([
[
'type' => 'submit',
'name' => E::ts('Submit'),
'isDefault' => TRUE,
],
['type' => 'next', 'name' => E::ts('Save'), 'isDefault' => TRUE],
['type' => 'submit', 'name' => E::ts('Save and Order Items')],
['type' => 'cancel', 'name' => E::ts('Cancel')],
]);
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
$this->assign('elementNames', $service->getRenderableElementNames($this));
parent::buildQuickForm();
}
public function postProcess(): void {
$values = $this->exportValues();
$options = $this->getColorOptions();
CRM_Core_Session::setStatus(E::ts('You picked color "%1"', [
1 => $options[$values['favorite_color']],
]));
parent::postProcess();
/**
* Method to add the state/province and country elements if needed
*
* @return void
*/
private function addStateAndCountry(): void {
$service = simpleorders_get_service();
if ($service->usesState()) {
$this->addEntityRef('state_id', E::ts("State/Province"), [
'entity' => 'state_province',
'multiple' => FALSE,
'api' => [
'params' => [
'is_active' => TRUE,
]
]]);
}
if ($service->usesCountry()) {
$this->addEntityRef('country_id', E::ts("Country"), [
'entity' => 'country',
'multiple' => FALSE,
'api' => [
'params' => [
'is_active' => TRUE,
]
]]);
}
}
/**
* Method to add the contact elements, based on the names and labels from the
* settings for the extension
*
* @return void
*/
private function addContactElements(): void {
$service= simpleorders_get_service();
$this->addEntityRef($service->getFirstContactName() . "_contact_id", $service->getFirstContactLabel(), [
'placeholder' => E::ts('- select contact -'),
'select' => ['minimumInputLength' => 3],
]);
$this->addEntityRef($service->getSecondContactName() . "_contact_id", $service->getSecondContactLabel(), [
'placeholder' => E::ts('- select contact -'),
'select' => ['minimumInputLength' => 3],
]);
}
/**
* Overridden method to prepare form
*
* @return void
* @throws CRM_Core_Exception
*/
public function preProcess(): void {
$contactId = CRM_Utils_Request::retrieveValue('cid', 'Integer');
if (!$contactId) {
$contactId = $this->getSubmittedValue('contact_id');
}
$this->_contactId = $contactId;
if ($this->_action == CRM_Core_Action::ADD) {
if ($this->_contactId) {
$service = simpleorders_get_service();
CRM_Utils_System::setTitle(E::ts("Add Order for ") . $service->getContactName($this->_contactId));
}
else {
CRM_Utils_System::setTitle(E::ts("Add Order"));
}
}
else {
CRM_Utils_System::setTitle(E::ts("Update Order"));
$soOrderId = CRM_Utils_Request::retrieveValue('oid', 'Integer');
if (!$soOrderId) {
$soOrderId = $this->getSubmittedValue('so_order_id');
}
if ($soOrderId) {
$this->_soOrderId = $soOrderId;
$soOrder = new CRM_Simpleorders_BAO_SoOrder();
$this->_currentData = $soOrder->getSoOrderWithOrderContacts($this->_soOrderId);
}
}
if ($this->_contactId) {
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/view', [
'reset' => TRUE,
'force' => TRUE,
'selectedChild' => 'contact_so_orders',
'cid' => $this->_contactId]));
}
}
/**
* Overridden parent method to set defaults
*
* @return array
*/
public function setDefaultValues(): array {
$defaults = [];
if ($this->_contactId) {
$defaults = ['contact_id' => $this->_contactId];
}
if ($this->_action == CRM_Core_Action::UPDATE) {
$defaults['so_order_id'] = $this->_soOrderId;
foreach ($this->_currentData as $currentField => $currentValue) {
$defaults[$currentField] = $currentValue;
}
$this->setContactDefaults($defaults);
}
return $defaults;
}
/**
* Method to set the defaults for the contacts on order
*
* @param array $defaults
* @return void
*/
private function setContactDefaults(array &$defaults): void {
$service= simpleorders_get_service();
if (isset($this->_currentData[$service->getFirstContactName()])) {
$defaults[$service->getFirstContactName() . "_contact_id"] = $this->_currentData[$service->getFirstContactName()];
unset($defaults[$service->getFirstContactName()]);
}
if (isset($this->_currentData[$service->getSecondContactName()])) {
$defaults[$service->getSecondContactName() . "_contact_id"] = $this->_currentData[$service->getSecondContactName()];
unset($defaults[$service->getSecondContactName()]);
}
}
public function getColorOptions(): array {
$options = [
'' => E::ts('- select -'),
'#f00' => E::ts('Red'),
'#0f0' => E::ts('Green'),
'#00f' => E::ts('Blue'),
'#f0f' => E::ts('Purple'),
];
foreach (['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e'] as $f) {
$options["#{$f}{$f}{$f}"] = E::ts('Grey (%1)', [1 => $f]);
/**
* Overridden parent method to process form submission
*
* @return void
*/
public function postProcess(): void {
$submitValues = $this->getSubmittedValues();
if ($submitValues['contact_id']) {
$this->_contactId = $submitValues['contact_id'];
}
if ($submitValues['so_order_id']) {
$this->_soOrderId = $submitValues['so_order_id'];
}
$soOrder = new CRM_Simpleorders_BAO_SoOrder();
if ($this->_action == CRM_Core_Action::ADD) {
$this->_soOrderId = $soOrder->createSoOrder($submitValues);
CRM_Core_Session::setStatus(E::ts("Order saved"), E::ts("Saved"), "success");
}
return $options;
if ($this->_action == CRM_Core_Action::UPDATE) {
$soOrder->updateSoOrder($this->_soOrderId, $submitValues);
}
$this->processItemsIfNeeded();
parent::postProcess();
}
/**
* Get the fields/elements defined in this form.
* Method to process to the order item form or page if needed
*
* @return array (string)
* @return void
*/
public function getRenderableElementNames(): array {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = [];
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
private function processItemsIfNeeded(): void {
if ($this->controller->getButtonName() == "_qf_SoOrder_submit") {
if ($this->_action == CRM_Core_Action::ADD) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/simpleorders/form/soorderitem', [
'reset' => TRUE,
'action' => 'add',
'oid' => $this->_soOrderId,
]));
}
if ($this->_action == CRM_Core_Action::UPDATE) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/simpleorders/page/soorderitem', [
'reset' => TRUE,
'oid' => $this->_soOrderId,
]));
}
}
return $elementNames;
}
}
<?php
use CRM_Simpleorders_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://docs.civicrm.org/dev/en/latest/framework/quickform/
*/
class CRM_Simpleorders_Form_SoOrderItem extends CRM_Core_Form {
private int $_soOrderId;
private int $_soOrderItemId;
private array $_currentData = [];
/**
* @throws \CRM_Core_Exception
*/
public function buildQuickForm(): void {
$service = simpleorders_get_service();
$this->add('hidden', 'so_order_id');
$this->add('hidden', 'so_order_item_id');
$soItem = new CRM_Simpleorders_BAO_SoItem();
if ($this->_action == CRM_Core_Action::ADD) {
$this->add('select', 'item_id', E::ts('Item'), $soItem->getItemList(), TRUE);
}
else {
$this->add('text', 'item_id', E::ts('Item'), ['readonly' => TRUE]);
}
$this->add('text', 'quantity', E::ts("Quantity"), [], TRUE);
$this->addRule('quantity', E::ts("You can only enter numbers"), 'numeric');
$this->addRule('quantity',E::ts('You can only enter numbers'),'nopunctuation');
$this->addButtons([
['type' => 'next', 'name' => E::ts('Save'), 'isDefault' => TRUE],
['type' => 'cancel', 'name' => E::ts('Cancel')],
]);
// export form elements
$this->assign('elementNames', $service->getRenderableElementNames($this));
parent::buildQuickForm();
}
/**
* Overridden method to prepare form
*
* @return void
* @throws CRM_Core_Exception
*/
public function preProcess(): void {
$orderId = CRM_Utils_Request::retrieveValue('oid', 'Integer');
if (!$orderId) {
$orderId = $this->getSubmittedValue('so_order_id');
}
$this->_soOrderId = $orderId;
if ($this->_action == CRM_Core_Action::ADD) {
CRM_Utils_System::setTitle(E::ts("Add Item to Order ") . $orderId);
}
else {
CRM_Utils_System::setTitle(E::ts("Update Item on Order ") . $orderId);
$orderItemId = CRM_Utils_Request::retrieveValue('oiid', 'Integer');
if (!$orderItemId) {
$orderItemId = $this->getSubmittedValue('so_order_item_id');
}
if ($orderItemId) {
$this->_soOrderItemId = $orderItemId;
$soOrderItem = new CRM_Simpleorders_BAO_SoOrderItem();
$this->_currentData = $soOrderItem->getSoOrderItem($this->_soOrderItemId);
}
}
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/simpleorders/page/soorderitem', [
'reset' => TRUE,
'oid' => $this->_soOrderId]));
}
/**
* Overridden parent method to set defaults
*
* @return array
*/
public function setDefaultValues(): array {
$defaults = ['so_order_id' => $this->_soOrderId];
if ($this->_action == CRM_Core_Action::UPDATE) {
$defaults['so_order_item_id'] = $this->_soOrderItemId;
$defaults['quantity'] = $this->_currentData['quantity'];
$defaults['item_id'] = $this->_currentData['item_id.name'];
}
return $defaults;
}
/**
* Overridden parent method to process form submission
*
* @return void
*/
public function postProcess(): void {
$submitValues = $this->getSubmittedValues();
if ($submitValues['so_order_item_id']) {
$this->_soOrderItemId = $submitValues['so_order_item_id'];
}
if ($submitValues['so_order_id']) {
$this->_soOrderId = $submitValues['so_order_id'];
}
$soOrderItem = new CRM_Simpleorders_BAO_SoOrderItem();
if ($this->_action == CRM_Core_Action::ADD) {
$soOrderItem->createSoOrderItem($submitValues);
CRM_Core_Session::setStatus(E::ts("Item on Order saved"), E::ts("Saved"), "success");
}
if ($this->_action == CRM_Core_Action::UPDATE) {
$soOrderItem->updateSoOrderItem($this->_soOrderItemId, $submitValues);
}
parent::postProcess();
}
}
<?php
use CRM_Simpleorders_ExtensionUtil as E;
/**
* Class for the contact tab showing orders for contact
*/
class CRM_Simpleorders_Page_SoOrder extends CRM_Core_Page {
private $_contactId;
/**
* Overridden parent method to run the page
*
* @return void
* @throws CRM_Core_Exception
*/
public function run() {
// Example: Set the page-title dynamically; alternatively, declare a static title in xml/Menu/*.xml
CRM_Utils_System::setTitle(E::ts('SoOrder'));
$this->initializePage();
$this->assign('so_orders', $this->getContactOrders());
parent::run();
}
// Example: Assign a variable for use in a template
$this->assign('currentTime', date('Y-m-d H:i:s'));
/**
* Method to get all contact orders
*
* @return array
*/
public function getContactOrders(): array {
$contactOrders = [];
try {
$orders = \Civi\Api4\SoOrderContact::get(TRUE)
->addSelect('role', 'role:label', 'order_id.*', 'campaign.title')
->addWhere('contact_id', '=', $this->_contactId)
->addJoin('Campaign AS campaign', 'LEFT', ['order_id.campaign_id', '=', 'campaign.id'])
->execute();
foreach ($orders as $order) {
$contactOrders[$order['id']] = $this->setContactOrderValues($order);
}
}
catch (CRM_Core_Exception $ex) {
}
return $contactOrders;
}
parent::run();
/**
* Method to set the values for the order row
*
* @param array $order
* @return array
*/
private function setContactOrderValues(array $order): array {
$contactOrderValues = [
'role' => $order['role:label'],
'campaign' => $order['campaign.title'],
'order_date' => $order['order_id.order_date'],
'delivery_address' => $order['order_id.delivery_address'],
'postcode' => $order['order_id.postcode'],
'city' => $order['order_id.city'],
'source' => $order['order_id.source'],
'actions' => $this->setActionLinks($order['order_id.id']),
];
if ($order['order_id..forwarded']) {
$contactOrderValues['forwarded'] = E::ts("Yes");
}
else {
$contactOrderValues['forwarded'] = E::ts("No");
}
$otherContact = $this->getOtherContact($order['order_id.id']);
if (isset($otherContact['id'])) {
$contactOrderValues['other_contact_id'] = $otherContact['id'];
}
if (isset($otherContact['name'])) {
$contactOrderValues['other_contact'] = $otherContact['name'];
}
return $contactOrderValues;
}
/**
* Method to get the other contact for the order
*
* @param int $orderId
* @return array
*/
private function getOtherContact(int $orderId): array {
$otherContact = [];
try {
$orderContact = \Civi\Api4\SoOrderContact::get(FALSE)
->addSelect('contact_id.display_name', 'contact_id', 'role:label')
->addWhere('order_id', '=', $orderId)
->addWhere('contact_id', '!=', $this->_contactId)
->execute()->first();
if (isset($orderContact['contact_id'])) {
$otherContact = [
'id' => $orderContact['contact_id'],
'name' => $orderContact['contact_id.display_name'] . " (" . $orderContact['role:label'] . ")",
];
}
}
catch (\CRM_Core_Exception $ex) {
}
return $otherContact;
}
/**
* Method to set the action links
*
* @param int $orderId
* @return array
*/
private function setActionLinks(int $orderId): array {
$deleteUrl = CRM_Utils_System::url('civicrm/simpleorders/form/delsoorder', [
'reset' => TRUE,
'action' => 'delete',
'oid' => $orderId,
'cid' => $this->_contactId,
],
);
$editUrl = CRM_Utils_System::url('civicrm/simpleorders/form/soorder', [
'reset' => TRUE,
'action' => 'update',
'oid' => $orderId,
'cid' => $this->_contactId,
]);
return [
'<a class="action-item" title="Edit" href="' . $editUrl . '">' . E::ts('Edit') . '</a>',
'<a class="action-item" title="Delete" href="' . $deleteUrl . '">' . E::ts('Delete') . '</a>',
];
}
/**
* Initialize the page
*
* @return void
* @throws CRM_Core_Exception
*/
private function initializePage(): void {
$this->_contactId = CRM_Utils_Request::retrieveValue('id', 'Integer');
if (!$this->_contactId) {
throw new Exception(E::ts("No contact ID found to show orders for"));
}
$this->assign('new_url', CRM_Utils_System::url('civicrm/simpleorders/form/soorder', [
'reset' => TRUE,
'action' => 'add',
'cid' => $this->_contactId,
]));
$service = simpleorders_get_service();
CRM_Utils_System::setTitle(E::ts("Orders for ") . $service->getContactName($this->_contactId));
}
}
<?php
use CRM_Simpleorders_ExtensionUtil as E;
/**
* Class showing items on order
*/
class CRM_Simpleorders_Page_SoOrderItem extends CRM_Core_Page {
private $_soOrderId;
/**
* Overridden parent method to run the page
*
* @return void
* @throws CRM_Core_Exception
*/
public function run() {
$this->initializePage();
$this->assign('so_order_items', $this->getOrderItems());
parent::run();
}
/**
* Method to get all order items
*
* @return array
*/
public function getOrderItems(): array {
$orderItems = [];
try {
$soOrderItems = \Civi\Api4\SoOrderItem::get(TRUE)
->addSelect('quantity', 'item_id', 'item_id.name', 'item_id.item_type:label')
->addWhere('order_id', '=', $this->_soOrderId)
->execute();
foreach ($soOrderItems as $soOrderItem) {
$orderItems[$soOrderItem['id']] = [
'item' => $soOrderItem['item_id.name'],
'item_type' => $soOrderItem['item_id.item_type:label'],
'quantity' => $soOrderItem['quantity'],
'actions' => $this->setActionLinks($soOrderItem['id']),
];
}
}
catch (CRM_Core_Exception $ex) {
}
return $orderItems;
}
/**
* Method to set the action links
*
* @param int $orderItemId
* @return array
*/
private function setActionLinks(int $orderItemId): array {
$deleteUrl = CRM_Utils_System::url('civicrm/simpleorders/form/delsoorderitem', [
'reset' => TRUE,
'action' => 'delete',
'oiid' => $orderItemId,
'oid' => $this->_soOrderId,
],
);
$editUrl = CRM_Utils_System::url('civicrm/simpleorders/form/soorderitem', [
'reset' => TRUE,
'action' => 'update',
'oiid' => $orderItemId,
'oid' => $this->_soOrderId,
]);
return [
'<a class="action-item" title="Edit" href="' . $editUrl . '">' . E::ts('Edit') . '</a>',
'<a class="action-item" title="Remove" href="' . $deleteUrl . '">' . E::ts('Remove') . '</a>',
];
}
/**
* Initialize the page
*
* @return void
* @throws CRM_Core_Exception
*/
private function initializePage(): void {
$this->_soOrderId = CRM_Utils_Request::retrieveValue('oid', 'Integer');
if (!$this->_soOrderId) {
throw new Exception(E::ts("No order ID found to show items for"));
}
$this->assign('new_order_item_url', CRM_Utils_System::url('civicrm/simpleorders/form/soorderitem', [
'reset' => TRUE,
'action' => 'add',
'oid' => $this->_soOrderId,
]));
CRM_Utils_System::setTitle(E::ts("Items on Order ") . $this->_soOrderId);
}
}
......@@ -21,9 +21,19 @@ class CRM_Simpleorders_Upgrader extends \CRM_Extension_Upgrader_Base {
public function addOptionGroups(): void {
$optionGroups = [
"so_item_type" => [
"title" => "Item Type (Simple Orders)",
'title' => "Item Type (Simple Orders)",
'values' => [
"digital" => E::ts("Digital"),
"physical" => E::ts("Physical"),
]
],
"so_contact_role" => [
'title' => "Contact Role (Simple Orders)",
'values' => [
"order" => E::ts("Placed order"),
"behalf" => E::ts("On behalf of"),
]
],
'values' => []
];
$og = new Civi\Simpleorders\OptionGroup();
foreach ($optionGroups as $optionGroupName => $optionGroupData) {
......
<?php
/**
* @author Erik Hommel <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\Simpleorders;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use CRM_Simpleorders_ExtensionUtil as E;
class Container implements CompilerPassInterface {
/**
* You can modify the container here before it is dumped to PHP code.
*/
public function process(ContainerBuilder $container): void {
if ($container->hasDefinition('data_processor_factory')) {
$factoryDefinition = $container->getDefinition('data_processor_factory');
$factoryDefinition->addMethodCall('addDataSource', ['soitem', 'Civi\Simpleorders\DataProcessor\SoItem', E::ts('Item (Simple Orders)')]);
}
if ($container->hasDefinition('action_provider')) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction',
['add_so_order', 'Civi\Simpleorders\Actions\AddSoOrder', E::ts('Add Order (Simple Orders)'), []]);
}
$definition = new Definition('Civi\Simpleorders\Service');
$definition->setFactory(['Civi\Simpleorders\Service', 'getInstance']);
$container->setDefinition('simpleorders', $definition);
$definition->setPublic(TRUE);
if (method_exists(Definition::class, 'setPrivate')) {
$definition->setPrivate(FALSE);
}
}
}
<?php
/**
* @author Erik Hommel <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\Simpleorders\DataProcessor;
use Civi\DataProcessor\Source\AbstractCivicrmEntitySource;
class SoItem extends AbstractCivicrmEntitySource {
/**
* Returns the entity name
*
* @return String
*/
protected function getEntity() {
return 'SoItem';
}
/**
* Returns the table name of this entity
*
* @return String
*/
protected function getTable() {
return 'civicrm_so_item';
}
}
<?php
namespace Civi\Simpleorders;
use \Civi\Core\Event\GenericHookEvent;
use CRM_Simpleorders_ExtensionUtil as E;
/**
* Class to handle the hooks for the simple orders extension
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
class HookHandler {
/**
* Process tab set hook: tab for orders
*
* @param GenericHookEvent $event
* @return void
*/
public static function processTabSet(GenericHookEvent $event): void {
if ($event->tabsetName == 'civicrm/contact/view' && isset($event->context['contact_id'])) {
$contactOrder = new \CRM_Simpleorders_BAO_SoOrderContact();
if ($contactOrder->countContactOrders($event->context['contact_id']) > 0) {
$event->tabs[] = [
'id' => 'contact_so_orders',
'url' => \CRM_Utils_System::url('civicrm/simpleorders/page/soorders', [
'reset' => TRUE,
'id' => $event->context['contact_id'],
]),
'title' => E::ts("Orders"),
'count' => $contactOrder->countContactOrders($event->context['contact_id']),
'weight' => 12
];
}
}
}
}
<?php
namespace Civi\Simpleorders;
use CRM_Simpleorders_ExtensionUtil as E;
/**
* Class for Simple Orders configuration and helper functions (with container usage)
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
class Service {
/**
* @var Service
*/
protected static $singleton;
/**
* Service constructor.
*/
public function __construct() {
if (!self::$singleton) {
self::$singleton = $this;
}
}
/**
* @return Service
*/
public static function getInstance(): Service {
if (!self::$singleton) {
self::$singleton = new Service();
}
return self::$singleton;
}
/**
* Method to get the name of a contact
*
* @param int $contactId
* @return string
*/
public function getContactName(int $contactId): string {
$contactName = "";
try {
$contact = \Civi\Api4\Contact::get(FALSE)
->addWhere('id', '=', $contactId)
->addSelect('display_name')
->execute()->first();
if (isset($contact['display_name'])) {
$contactName = $contact['display_name'];
}
}
catch (\CRM_Core_Exception $ex) {
}
return $contactName;
}
/**
* @return bool
*/
public function usesState(): bool {
return \Civi::settings()->get('simpleorders_use_state');
}
/**
* @return bool
*/
public function usesCountry(): bool {
return \Civi::settings()->get('simpleorders_use_country');
}
public function getFirstContactName(): string {
return "order";
}
public function getFirstContactLabel(): string {
return "Placed Order";
}
public function getSecondContactName(): string {
return "behalf";
}
public function getSecondContactLabel(): string {
return "On Behalf Of";
}
/**
* Get the fields/elements defined in this form.
*
* @param \CRM_Core_Form $form
* @return array (string)
*/
public function getRenderableElementNames(\CRM_Core_Form $form): array {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = [];
foreach ($form->_elements as $element) {
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}
......@@ -2,5 +2,5 @@
<af-field name="name" />
<af-field name="description" />
<af-field name="item_type" defn="{input_type: 'Select', input_attrs: {multiple: true}}" />
<crm-search-display-table search-name="SoItem_Search_by_Karel_Vandamme" display-name="SoItem_Search_by_Karel_Vandamme_Table_1"></crm-search-display-table>
<crm-search-display-table search-name="SoItem_Search" display-name="SoItem_Search_Table"></crm-search-display-table>
</div>
<?php
use CRM_Simpleorders_ExtensionUtil as E;
return [
[
'name' => 'SavedSearch_SoItem_Search_by_Karel_Vandamme',
'entity' => 'SavedSearch',
'cleanup' => 'unused',
'update' => 'unmodified',
'params' => [
'version' => 4,
'values' => [
'name' => 'SoItem_Search_by_Karel_Vandamme',
'label' => E::ts('SoItem Search'),
'api_entity' => 'SoItem',
'api_params' => [
'version' => 4,
'select' => [
'id',
'name',
'description',
'item_type:label',
],
'orderBy' => [],
'where' => [],
'groupBy' => [],
'join' => [],
'having' => [],
],
],
'match' => ['name'],
],
],
[
'name' => 'SavedSearch_SoItem_Search_by_Karel_Vandamme_SearchDisplay_SoItem_Search_by_Karel_Vandamme_Table_1',
'entity' => 'SearchDisplay',
'cleanup' => 'unused',
'update' => 'unmodified',
'params' => [
'version' => 4,
'values' => [
'name' => 'SoItem_Search_by_Karel_Vandamme_Table_1',
'label' => E::ts('SoItem List'),
'saved_search_id.name' => 'SoItem_Search_by_Karel_Vandamme',
'type' => 'table',
'settings' => [
'description' => E::ts(NULL),
'sort' => [],
'limit' => 50,
'pager' => [],
'placeholder' => 5,
'columns' => [
[
'type' => 'field',
'key' => 'id',
'dataType' => 'Integer',
'label' => E::ts('Item ID'),
'sortable' => TRUE,
],
[
'type' => 'field',
'key' => 'name',
'dataType' => 'String',
'label' => E::ts('Item Name'),
'sortable' => TRUE,
'editable' => TRUE,
],
[
'type' => 'field',
'key' => 'description',
'dataType' => 'Text',
'label' => E::ts('Item Description'),
'sortable' => TRUE,
'editable' => TRUE,
],
[
'type' => 'field',
'key' => 'item_type:label',
'dataType' => 'String',
'label' => E::ts('Item Type'),
'sortable' => TRUE,
'editable' => TRUE,
],
],
'actions' => TRUE,
'classes' => ['table', 'table-striped'],
'toolbar' => [
[
'path' => 'civicrm/simpleorders/newitem',
'icon' => 'fa-external-link',
'text' => E::ts('Add item'),
'style' => 'primary',
'condition' => [],
'task' => '',
'entity' => '',
'action' => '',
'join' => '',
'target' => 'crm-popup',
],
],
],
],
'match' => [
'saved_search_id',
'name',
],
],
],
];
......@@ -37,7 +37,7 @@ return [
'title' => E::ts('Item Type'),
'sql_type' => 'varchar(64)',
'input_type' => 'Text',
'description' => E::ts('Item Description'),
'description' => E::ts('Item Type'),
'pseudoconstant' => [
'option_group_name' => 'so_item_type',
],
......
......@@ -8,7 +8,7 @@ return [
'getInfo' => fn() => [
'title' => E::ts('SoOrder'),
'title_plural' => E::ts('SoOrders'),
'description' => E::ts('FIXME'),
'description' => E::ts('Orders (Simple Orders)'),
'log' => TRUE,
],
'getFields' => fn() => [
......
......@@ -8,7 +8,7 @@ return [
'getInfo' => fn() => [
'title' => E::ts('SoOrderContact'),
'title_plural' => E::ts('SoOrderContacts'),
'description' => E::ts('FIXME'),
'description' => E::ts('Link between Order and Contact (Simple Orders)'),
'log' => TRUE,
],
'getFields' => fn() => [
......@@ -43,6 +43,15 @@ return [
'on_delete' => 'CASCADE',
],
],
'role' => [
'title' => E::ts('Contact Role'),
'sql_type' => 'varchar(64)',
'input_type' => 'Text',
'description' => E::ts('Contact Role'),
'pseudoconstant' => [
'option_group_name' => 'so_contact_role',
],
],
],
'getIndices' => fn() => [],
'getPaths' => fn() => [],
......
......@@ -8,7 +8,7 @@ return [
'getInfo' => fn() => [
'title' => E::ts('SoOrderItem'),
'title_plural' => E::ts('SoOrderItems'),
'description' => E::ts('FIXME'),
'description' => E::ts('Link between Order and Item (Simple Orders)'),
'log' => TRUE,
],
'getFields' => fn() => [
......
<?php
use CRM_Simpleorders_ExtensionUtil as E;
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
/*
* Settings metadata file
*/
return [
'simpleorders_first_contact' => [
'name' => 'simpleorders_first_contact',
'title' => E::ts('Role for the first contact on order'),
'description' => E::ts('The role used for the first contact on the order'),
'type' => 'String',
'html_type' => 'select',
'html_attributes' => [
'class' => 'crm-select2',
'multiple' => FALSE,
],
'pseudoconstant' => [
'callback' => 'CRM_Simpleorders_BAO_SoOrderContact::getContactRoleList',
],
'add' => '5.7',
'is_domain' => 1,
'is_contact' => 0,
'help_text' => E::ts('The role used for the first contact on order, for example the order role for the contact that placed the order'),
'settings_pages' => ['simpleorders' => ['weight' => 10]],
],
'simpleorders_second_contact' => [
'name' => 'simpleorders_second_contact',
'title' => E::ts('Role for the second contact on order'),
'description' => E::ts('The role used for the second contact on the order'),
'type' => 'String',
'html_type' => 'select',
'html_attributes' => [
'class' => 'crm-select2',
'multiple' => FALSE,
],
'pseudoconstant' => [
'callback' => 'CRM_Simpleorders_BAO_SoOrderContact::getContactRoleList',
],
'add' => '5.7',
'is_domain' => 1,
'is_contact' => 0,
'help_text' => E::ts('The role used for the second contact on order, for example the behalf role for the contact on whose behalf the order is placed'),
'settings_pages' => ['simpleorders' => ['weight' => 15]],
],
'simpleorders_use_state' => [
'name' => 'simpleorders_use_state',
'title' => E::ts('Use province/state for address?'),
'description' => E::ts('Use province/state for address?'),
'type' => 'Boolean',
'html_type' => 'checkbox',
'default' => FALSE,
'add' => '5.7',
'is_domain' => 1,
'is_contact' => 0,
'help_text' => E::ts('Should we be able to use province/state for the address?'),
'settings_pages' => ['simpleorders' => ['weight' => 20]],
],
'simpleorders_use_country' => [
'name' => 'simpleorders_use_country',
'title' => E::ts('Use country for address?'),
'description' => E::ts('Use country for address?'),
'type' => 'Boolean',
'html_type' => 'checkbox',
'default' => FALSE,
'add' => '5.7',
'is_domain' => 1,
'is_contact' => 0,
'help_text' => E::ts('Should we be able to use country for the address? Not required for example if only get orders from the default CiviCRM country.'),
'settings_pages' => ['simpleorders' => ['weight' => 25]],
],
];
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment