Skip to content
Permalink
Browse files

Move functions to where they are actually used & filter out contact l…

…ists
  • Loading branch information...
flack committed May 16, 2019
1 parent 21d646c commit c8d5cbd7fa3de7ab02e6aa3dc6b15c805cca9863
Showing with 56 additions and 103 deletions.
  1. +0 −64 lib/org/openpsa/helpers/list.php
  2. +56 −2 lib/org/openpsa/sales/handler/edit.php
  3. +0 −37 test/org/openpsa/helpers/listTest.php
@@ -13,70 +13,6 @@
*/
class org_openpsa_helpers_list
{
private static $_seen = [];
/**
* Function for listing groups task/salesproject contacts are members of
*
* @param midcom_core_dbaobject $task The task/salesproject we're working with
* @param string $mode By which property should groups be listed
* @param array $contacts Default contacts for nonpersistent objects
*/
public static function task_groups(midcom_core_dbaobject $task, $mode = 'id', array $contacts = [])
{
$ret = [0 => ''];
self::$_seen = [];
if (!in_array($mode, ['id', 'guid'])) {
debug_add('Mode ' . $mode . ' not supported', MIDCOM_LOG_ERROR);
return $ret;
}
//Make sure the currently selected customer (if any) is listed
if ($task->customer > 0) {
//Make sure we can read the current customer for the name
midcom::get()->auth->request_sudo('org.openpsa.helpers');
self::task_groups_put($ret, $mode, $task->customer);
midcom::get()->auth->drop_sudo();
}
if (empty($contacts)) {
$task->get_members();
$contacts = $task->contacts;
if (empty($contacts)) {
return $ret;
}
}
$mc = midcom_db_member::new_collector();
$mc->add_constraint('uid', 'IN', array_keys($contacts));
/* Skip magic groups */
$mc->add_constraint('gid.name', 'NOT LIKE', '\_\_%');
$memberships = $mc->get_values('gid');
foreach ($memberships as $gid) {
self::task_groups_put($ret, $mode, $gid);
}
reset($ret);
asort($ret);
return $ret;
}
private static function task_groups_put(array &$ret, $mode, $company_id)
{
if (empty(self::$_seen[$company_id])) {
try {
$company = new org_openpsa_contacts_group_dba($company_id);
} catch (midcom_error $e) {
return;
}
self::$_seen[$company->id] = true;
$ret[$company->$mode] = $company->get_label();
}
}
/**
* List tasks user can see
*/
@@ -35,7 +35,7 @@ public function _handler_edit(Request $request, $guid)
$schemadb = schemadb::from_path($this->_config->get('schemadb_salesproject'));
$field =& $schemadb->get('default')->get_field('customer');
$field['type_config']['options'] = org_openpsa_helpers_list::task_groups($this->_salesproject);
$field['type_config']['options'] = $this->list_groups($this->_salesproject);
$dm = new datamanager($schemadb);
$dm->set_storage($this->_salesproject);
@@ -71,7 +71,7 @@ public function _handler_new(Request $request, $guid = null)
} catch (midcom_error $e) {
$customer = new org_openpsa_contacts_person_dba($guid);
$defaults['customerContact'] = $customer->id;
$field['type_config']['options'] = org_openpsa_helpers_list::task_groups(new org_openpsa_sales_salesproject_dba, 'id', [$customer->id => true]);
$field['type_config']['options'] = $this->list_groups(new org_openpsa_sales_salesproject_dba, [$customer->id => true]);
}
$this->add_breadcrumb($this->router->generate('list_customer', ['guid' => $customer->guid]),
sprintf($this->_l10n->get('salesprojects with %s'), $customer->get_label()));
@@ -94,6 +94,60 @@ public function save_callback()
return $this->router->generate('salesproject_view', ['guid' => $this->_salesproject->guid]);
}
/**
* Function for listing groups salesproject contacts are members of
*
* @param org_openpsa_sales_salesproject_dba $salesproject The salesproject we're working with
* @param array $contacts Default contacts for nonpersistent objects
*/
private function list_groups(org_openpsa_sales_salesproject_dba $salesproject, array $contacts = [])
{
$ret = [0 => ''];
// Make sure the currently selected customer (if any) is listed
if ($salesproject->customer > 0) {
// Make sure we can read the current customer for the name
midcom::get()->auth->request_sudo('org.openpsa.helpers');
$this->load_group($ret, $salesproject->customer);
midcom::get()->auth->drop_sudo();
}
if (empty($contacts)) {
$salesproject->get_members();
$contacts = $salesproject->contacts;
if (empty($contacts)) {
return $ret;
}
}
$mc = midcom_db_member::new_collector();
$mc->add_constraint('uid', 'IN', array_keys($contacts));
// Skip magic groups and contact lists
$mc->add_constraint('gid.name', 'NOT LIKE', '\_\_%');
$mc->add_constraint('gid.orgOpenpsaObtype', '<>', org_openpsa_contacts_group_dba::MYCONTACTS);
$memberships = $mc->get_values('gid');
foreach ($memberships as $gid) {
$this->load_group($ret, $gid);
}
reset($ret);
asort($ret);
return $ret;
}
private function load_group(array &$ret, $company_id)
{
if (!array_key_exists($company_id, $ret)) {
try {
$company = org_openpsa_contacts_group_dba::get_cached($company_id);
$ret[$company->id] = $company->get_label();
} catch (midcom_error $e) {
$e->log();
}
}
}
/**
* @param Request $request The request object
* @param string $guid The salesproject GUID

This file was deleted.

Oops, something went wrong.

0 comments on commit c8d5cbd

Please sign in to comment.
You can’t perform that action at this time.