Commit 8746f3d1 authored by mattwire's avatar mattwire
Browse files

Initial commit

parents
<?php
/**
* https://civicrm.org/licensing
*/
/**
* The class that handles all the AJAX callbacks
* Class CRM_Groupadmin_AJAX
*/
class CRM_Groupadmin_AJAX {
/**
* Get a datatable formatted list of all bulk contributions for a specific identifier
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function getGroupAdminList() {
$requiredParameters = [
'group_id' => 'Positive',
];
$params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
$params += CRM_Core_Page_AJAX::validateParams($requiredParameters);
$params['return'] = ['contact_id', 'contact_id.display_name'];
$entities = civicrm_api3('GroupAdminContact', 'get', $params);
$rows = [];
foreach ($entities as $entity) {
$rows[] = [
'DT_RowId' => $entity['id'],
'DT_RowAttr' => [
'data-entity' => 'groupadmincontact',
'data-id' => $entity['id']
],
'DT_RowClass' => 'crm-entity',
'contact_id' => $entity['contact_id'],
'name' => $entity['contact_id.display_name'],
'links' => "<a href='#' data-id='{$entity['id']}' class='groupadmincontact-remove' onclick='window.groupAdminContact.removeItem(this)'>Remove</a>",
];
}
$datatableDT = [
'recordsFiltered' => count($rows),
'recordsTotal' => count($rows),
];
$datatableDT['data'] = $rows;
CRM_Utils_JSON::output($datatableDT);
}
/**
* Remove a bulk contribution from a collection of contributions
* @throws \CiviCRM_API3_Exception
*/
public static function removeFromGroup() {
$requiredParameters = [
'id' => 'Positive',
];
$params = CRM_Core_Page_AJAX::validateParams($requiredParameters);
try {
civicrm_api3('GroupAdminContact', 'delete', [
'id' => $params['id'],
]);
}
catch (Exception $e) {
self::returnAjaxError($e->getMessage());
}
}
/**
* Add a bulk contribution to a collection of contributions
* @throws \CiviCRM_API3_Exception
*/
public static function addToGroup() {
$requiredParameters = [
'contactid' => 'Positive',
'groupid' => 'String',
];
try {
$params = CRM_Core_Page_AJAX::validateParams($requiredParameters);
civicrm_api3('GroupAdminContact', 'create', [
'contact_id' => $params['contactid'],
'group_id' => $params['groupid'],
]);
}
catch (Exception $e) {
if ($e->getErrorCode() !== 'already exists') {
self::returnAjaxError($e->getMessage());
}
}
}
private static function returnAjaxError($message) {
CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
echo json_encode(['message' => $message]);
http_response_code(400);
exit(1);
}
}
<?php
use CRM_Groupadmin_ExtensionUtil as E;
class CRM_Groupadmin_BAO_GroupAdminContact extends CRM_Groupadmin_DAO_GroupAdminContact {
/**
* Create a new GroupAdminContact based on array-data
*
* @param array $params key-value pairs
* @return CRM_Groupadmin_DAO_GroupAdminContact|NULL
*
public static function create($params) {
$className = 'CRM_Groupadmin_DAO_GroupAdminContact';
$entityName = 'GroupAdminContact';
$hook = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
$instance = new $className();
$instance->copyValues($params);
$instance->save();
CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
return $instance;
} */
/**
* @param $contactID
*
* @return int[]
*/
public static function getGroupAdminList($contactID) {
if (!isset(\Civi::$statics[__CLASS__][$contactID]['groupAdminList'])) {
$groupAdminContacts = \Civi\Api4\GroupAdminContact::get()
->addSelect('group_id')
->addWhere('contact_id', '=', $contactID)
->setCheckPermissions(FALSE)
->execute()
->indexBy('group_id');
\Civi::$statics[__CLASS__][$contactID]['groupAdminList'] = array_keys($groupAdminContacts->getArrayCopy());
}
return \Civi::$statics[__CLASS__][$contactID]['groupAdminList'];
}
public static function getGroupAdminPermissionedWhereClause($contactID) {
$groups = self::getGroupAdminList($contactID);
if (!empty($groups)) {
return 'contact_a.id IN (
SELECT contact_id FROM civicrm_group_contact WHERE group_id IN ('
. implode(',', self::getGroupAdminList($contactID)) . ')
)';
}
return NULL;
}
}
<?php
/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
* Generated from /home/matthew/buildkit/build/d7master/web/sites/default/files/civicrm/ext/groupadmin/xml/schema/CRM/Groupadmin/GroupAdminContact.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:dd5f84ff388e644216a6f31f39832a37)
*/
/**
* Database access object for the GroupAdminContact entity.
*/
class CRM_Groupadmin_DAO_GroupAdminContact extends CRM_Core_DAO {
/**
* Static instance to hold the table name.
*
* @var string
*/
public static $_tableName = 'civicrm_group_admin_contact';
/**
* Should CiviCRM log any modifications to this table in the civicrm_log table.
*
* @var bool
*/
public static $_log = TRUE;
/**
* Unique GroupAdminContact ID
*
* @var int
*/
public $id;
/**
* FK to Contact
*
* @var int
*/
public $contact_id;
/**
* FK to civicrm_group
*
* @var int
*/
public $group_id;
/**
* Class constructor.
*/
public function __construct() {
$this->__table = 'civicrm_group_admin_contact';
parent::__construct();
}
/**
* Returns localized title of this entity.
*/
public static function getEntityTitle() {
return ts('Group Admin Contacts');
}
/**
* Returns foreign keys and entity references.
*
* @return array
* [CRM_Core_Reference_Interface]
*/
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'contact_id', 'civicrm_contact', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'group_id', 'civicrm_group', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
}
/**
* Returns all the column names of this table
*
* @return array
*/
public static function &fields() {
if (!isset(Civi::$statics[__CLASS__]['fields'])) {
Civi::$statics[__CLASS__]['fields'] = [
'id' => [
'name' => 'id',
'type' => CRM_Utils_Type::T_INT,
'description' => CRM_Groupadmin_ExtensionUtil::ts('Unique GroupAdminContact ID'),
'required' => TRUE,
'where' => 'civicrm_group_admin_contact.id',
'table_name' => 'civicrm_group_admin_contact',
'entity' => 'GroupAdminContact',
'bao' => 'CRM_Groupadmin_DAO_GroupAdminContact',
'localizable' => 0,
'add' => NULL,
],
'contact_id' => [
'name' => 'contact_id',
'type' => CRM_Utils_Type::T_INT,
'description' => CRM_Groupadmin_ExtensionUtil::ts('FK to Contact'),
'where' => 'civicrm_group_admin_contact.contact_id',
'table_name' => 'civicrm_group_admin_contact',
'entity' => 'GroupAdminContact',
'bao' => 'CRM_Groupadmin_DAO_GroupAdminContact',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'add' => NULL,
],
'group_id' => [
'name' => 'group_id',
'type' => CRM_Utils_Type::T_INT,
'title' => CRM_Groupadmin_ExtensionUtil::ts('Group ID'),
'description' => CRM_Groupadmin_ExtensionUtil::ts('FK to civicrm_group'),
'required' => TRUE,
'where' => 'civicrm_group_admin_contact.group_id',
'table_name' => 'civicrm_group_admin_contact',
'entity' => 'GroupAdminContact',
'bao' => 'CRM_Groupadmin_DAO_GroupAdminContact',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Group',
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'table' => 'civicrm_group',
'keyColumn' => 'id',
'labelColumn' => 'title',
],
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
return Civi::$statics[__CLASS__]['fields'];
}
/**
* Return a mapping from field-name to the corresponding key (as used in fields()).
*
* @return array
* Array(string $name => string $uniqueName).
*/
public static function &fieldKeys() {
if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {
Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
}
return Civi::$statics[__CLASS__]['fieldKeys'];
}
/**
* Returns the names of this table
*
* @return string
*/
public static function getTableName() {
return self::$_tableName;
}
/**
* Returns if this table needs to be logged
*
* @return bool
*/
public function getLog() {
return self::$_log;
}
/**
* Returns the list of fields that can be imported
*
* @param bool $prefix
*
* @return array
*/
public static function &import($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'group_admin_contact', $prefix, []);
return $r;
}
/**
* Returns the list of fields that can be exported
*
* @param bool $prefix
*
* @return array
*/
public static function &export($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'group_admin_contact', $prefix, []);
return $r;
}
/**
* Returns the list of indices
*
* @param bool $localize
*
* @return array
*/
public static function indices($localize = TRUE) {
$indices = [
'UI_group_admin_contact' => [
'name' => 'UI_group_admin_contact',
'field' => [
0 => 'contact_id',
1 => 'group_id',
],
'localizable' => FALSE,
'unique' => TRUE,
'sig' => 'civicrm_group_admin_contact::1::contact_id::group_id',
],
];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
}
}
<?php
use CRM_Groupadmin_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://docs.civicrm.org/dev/en/latest/framework/quickform/
*/
class CRM_Groupadmin_Form_Edit extends CRM_Core_Form {
/**
* @var int
* Group ID
*/
protected $groupID;
public function buildQuickForm() {
$this->groupID = CRM_Utils_Request::retrieveValue('gid', 'Positive', NULL, TRUE);
$this->setTitle('Edit Group Administrators');
$this->assign('groupTitle', civicrm_api3('Group', 'getvalue', ['id' => $this->groupID, 'return' => 'title']));
$groupAdminContacts = \Civi\Api4\GroupAdminContact::get()
->addSelect('contact_id', 'contact.display_name')
->setJoin([
['Contact AS contact', TRUE, ['contact_id', '=', 'contact_id']],
])
->addWhere('group_id', '=', $this->groupID)
->setCheckPermissions(FALSE)
->execute();
foreach ($groupAdminContacts as $groupAdminContact) {
$adminContacts[$groupAdminContact['contact_id']] = $groupAdminContact['contact.display_name'];
}
$this->assign('groupAdminContacts', $adminContacts);
$this->assign('groupID', $this->groupID);
$this->addEntityRef('contact_id', ts('Add Group Admin Contact'), ['create' => FALSE], FALSE);
$this->addButtons([
[
'type' => 'submit',
'name' => E::ts('Submit'),
'isDefault' => TRUE,
],
]);
}
}
<?php
use CRM_Groupadmin_ExtensionUtil as E;
/**
* Collection of upgrade steps.
*/
class CRM_Groupadmin_Upgrader extends CRM_Groupadmin_Upgrader_Base {
// By convention, functions that look like "function upgrade_NNNN()" are
// upgrade tasks. They are executed in order (like Drupal's hook_update_N).
/**
* Example: Run an external SQL script when the module is installed.
*
public function install() {
$this->executeSqlFile('sql/myinstall.sql');
}
/**
* Example: Work with entities usually not available during the install step.
*
* This method can be used for any post-install tasks. For example, if a step
* of your installation depends on accessing an entity that is itself
* created during the installation (e.g., a setting or a managed entity), do
* so here to avoid order of operation problems.
*/
// public function postInstall() {
// $customFieldId = civicrm_api3('CustomField', 'getvalue', array(
// 'return' => array("id"),
// 'name' => "customFieldCreatedViaManagedHook",
// ));
// civicrm_api3('Setting', 'create', array(
// 'myWeirdFieldSetting' => array('id' => $customFieldId, 'weirdness' => 1),
// ));
// }
/**
* Example: Run an external SQL script when the module is uninstalled.
*/
// public function uninstall() {
// $this->executeSqlFile('sql/myuninstall.sql');
// }
/**
* Example: Run a simple query when a module is enabled.
*/
// public function enable() {
// CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 1 WHERE bar = "whiz"');
// }
/**
* Example: Run a simple query when a module is disabled.
*/
// public function disable() {
// CRM_Core_DAO::executeQuery('UPDATE foo SET is_active = 0 WHERE bar = "whiz"');
// }
/**
* Example: Run a couple simple queries.
*
* @return TRUE on success
* @throws Exception
*/
// public function upgrade_4200() {
// $this->ctx->log->info('Applying update 4200');
// CRM_Core_DAO::executeQuery('UPDATE foo SET bar = "whiz"');
// CRM_Core_DAO::executeQuery('DELETE FROM bang WHERE willy = wonka(2)');
// return TRUE;
// }
/**
* Example: Run an external SQL script.
*
* @return TRUE on success
* @throws Exception
*/
// public function upgrade_4201() {
// $this->ctx->log->info('Applying update 4201');
// // this path is relative to the extension base dir
// $this->executeSqlFile('sql/upgrade_4201.sql');
// return TRUE;
// }
/**
* Example: Run a slow upgrade process by breaking it up into smaller chunk.
*
* @return TRUE on success
* @throws Exception
*/
// public function upgrade_4202() {
// $this->ctx->log->info('Planning update 4202'); // PEAR Log interface
// $this->addTask(E::ts('Process first step'), 'processPart1', $arg1, $arg2);
// $this->addTask(E::ts('Process second step'), 'processPart2', $arg3, $arg4);
// $this->addTask(E::ts('Process second step'), 'processPart3', $arg5);
// return TRUE;
// }
// public function processPart1($arg1, $arg2) { sleep(10); return TRUE; }
// public function processPart2($arg3, $arg4) { sleep(10); return TRUE; }
// public function processPart3($arg5) { sleep(10); return TRUE; }
/**
* Example: Run an upgrade with a query that touches many (potentially
* millions) of records by breaking it up into smaller chunks.
*
* @return TRUE on success
* @throws Exception
*/
// public function upgrade_4203() {
// $this->ctx->log->info('Planning update 4203'); // PEAR Log interface
// $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution');
// $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution');
// for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
// $endId = $startId + self::BATCH_SIZE - 1;
// $title = E::ts('Upgrade Batch (%1 => %2)', array(
// 1 => $startId,
// 2 => $endId,
// ));
// $sql = '
// UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker)
// WHERE id BETWEEN %1 and %2
// ';
// $params = array(
// 1 => array($startId, 'Integer'),
// 2 => array($endId, 'Integer'),
// );
// $this->addTask($title, 'executeSql', $sql, $params);
// }
// return TRUE;
// }
}
<?php
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
use CRM_Groupadmin_ExtensionUtil as E;
/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_Groupadmin_Upgrader_Base {
/**
* @var CRM_Groupadmin_Upgrader_Base subclass of this
*/
public static $instance;
/**
* @var CRM_Queue_TaskContext
*/
protected $ctx;
/**
* @var string eg 'com.example.myextension'
*/
protected $extensionName;
/**
* @var string full path to the extension's source tree
*/
protected $extensionDir;
/**
* @var revisionNumber[]
* sorted numerically
*/
private $revisions;
/**
* @var bool
* Flag to clean up extension revision data in civicrm_setting
*/
private $revisionStorageIsDeprecated = FALSE;
/**
* Obtain a reference to the active upgrade handler.
*/
public static function instance() {
if (!self::$instance) {
// FIXME auto-generate
self::$instance = new CRM_Groupadmin_Upgrader(
'groupadmin',
realpath(__DIR__ . '/../../../')
);
}
return self::$instance;