/**
  * These tasks are the core set of tasks that the user can perform
  * on a relationship / group of relationships
  *
  * @return array
  *   the set of tasks for a group of relationships
  */
 public static function &tasks()
 {
     if (!self::$_tasks) {
         self::$_tasks = array(1 => array('title' => ts('Export Relationships'), 'class' => array('CRM_Relationship_Export_Form_Select', 'CRM_Relationship_Export_Form_Map'), 'result' => FALSE));
         CRM_Utils_Hook::searchTasks('relationship', self::$_tasks);
         asort(self::$_tasks);
     }
     return self::$_tasks;
 }
 /**
  * @param CRM_Core_Form $form
  * @param bool $useTable
  */
 public static function preProcessCommon(&$form, $useTable = FALSE)
 {
     $form->_relationshipIds = array();
     $values = $form->controller->exportValues($form->get('searchFormName'));
     $form->_task = $values['task'];
     $relationshipTasks = CRM_Relationship_Task::tasks();
     $form->assign('taskName', $relationshipTasks[$form->_task]);
     $ids = array();
     if ($values['radio_ts'] == 'ts_sel') {
         foreach ($values as $name => $value) {
             if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
                 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
             }
         }
     } else {
         $queryParams = $form->get('queryParams');
         $sortOrder = NULL;
         if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
             $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
         }
         $query = new CRM_Relationship_BAO_Query($queryParams);
         $query->_distinctComponentClause = 'civicrm_relationship.id';
         $query->_groupByComponentClause = ' GROUP BY civicrm_relationship.id ';
         $result = $query->searchQuery(0, 0, $sortOrder);
         while ($result->fetch()) {
             $ids[] = $result->relationship_id;
         }
         $form->assign('totalSelectedRelationships', $form->get('rowCount'));
     }
     if (!empty($ids)) {
         $form->_componentClause = ' civicrm_relationship.id IN ( ' . implode(',', $ids) . ' ) ';
         $form->assign('totalSelectedRelationships', count($ids));
     }
     $form->_relationshipIds = $form->_componentIds = $ids;
     //set the context for redirection for any task actions
     $session = CRM_Core_Session::singleton();
     $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
     $urlParams = 'force=1';
     if (CRM_Utils_Rule::qfKey($qfKey)) {
         $urlParams .= "&qfKey={$qfKey}";
     }
     $searchFormName = strtolower($form->get('searchFormName'));
     if ($searchFormName == 'search') {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/rel/search', $urlParams));
     } else {
         $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/{$searchFormName}", $urlParams));
     }
 }
 /**
  * Build the form object.
  */
 public function buildQuickForm()
 {
     $this->_searchPane = CRM_Utils_Array::value('searchPane', $_GET);
     if (!$this->_searchPane || $this->_searchPane == 'basic') {
         CRM_Relationship_Form_Search_Criteria::basic($this);
     }
     $allPanes = array();
     $paneNames = array(ts('Custom Fields') => 'custom', ts('Contact A') => 'contact_a', ts('Contact B') => 'contact_b');
     //check if there are any custom data searchable fields
     $extends = array('Relationship');
     $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
     // if no searchable fields unset panel
     if (empty($groupDetails)) {
         unset($paneNames[ts('Custom Fields')]);
     }
     //TODO: allow hook injected panes
     //$hookPanes = array();
     //CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
     //$paneNames = array_merge($paneNames, $hookPanes);
     $this->_paneTemplatePath = array();
     foreach ($paneNames as $name => $type) {
         $allPanes[$name] = array('url' => CRM_Utils_System::url('civicrm/rel/search', "snippet=1&searchPane={$type}&qfKey={$this->controller->_key}"), 'open' => 'false', 'id' => $type);
         // see if we need to include this paneName in the current form
         if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) || CRM_Utils_Array::value("hidden_{$type}", $this->_formValues)) {
             $allPanes[$name]['open'] = 'true';
             CRM_Relationship_Form_Search_Criteria::$type($this);
             $template = ucfirst($type);
             $this->_paneTemplatePath[$type] = "CRM/Relationship/Form/Search/Criteria/{$template}.tpl";
         }
     }
     $this->assign('allPanes', $allPanes);
     if (!$this->_searchPane) {
         parent::buildQuickForm();
         $resources = CRM_Core_Resources::singleton();
         $resources->addScriptFile('be.chiro.civi.relationship', 'js/markrelationselector.js', 1, 'html-header');
         //remove original js file
         $searchformjspath = $resources->getUrl('civicrm', 'js/crm.searchForm.js', TRUE);
         unset(CRM_Core_Region::instance('html-header')->_snippets[$searchformjspath]);
         $permission = CRM_Core_Permission::getPermission();
         // some tasks.. what do we want to do with the selected contacts ?
         $tasks = CRM_Relationship_Task::permissionedTaskTitles($permission, CRM_Utils_Array::value('deleted_contacts', $this->_formValues));
         $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
         $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
         $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
         $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
         $selectedRelationshipIds = array();
         $qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
         // We use ajax to handle selections only if the search results component_mode is set to "relationships"
         if ($qfKeyParam && $this->get('component_mode') <= 1) {
             $this->addClass('crm-ajax-selection-form');
             $qfKeyParam = "civicrm search {$qfKeyParam}";
             $selectedRelationshipIdsArr = CRM_Core_BAO_PrevNextCache::getSelection($qfKeyParam);
             $selectedRelationshipIds = array_keys($selectedRelationshipIdsArr[$qfKeyParam]);
         }
         $this->assign_by_ref('selectedRelationshipIds', $selectedRelationshipIds);
         $rows = $this->get('rows');
         if (is_array($rows)) {
             $this->addRowSelectors($rows);
         }
     } else {
         $this->assign('suppressForm', TRUE);
     }
 }
 /**
  * Determine the form name based on the action. This allows us
  * to avoid using  conditional state machine, much more efficient
  * and simpler
  *
  * @param CRM_Core_Controller $controller
  *   The controller object.
  *
  * @param string $formName
  *
  * @return string
  *   the name of the form that will handle the task
  */
 public function taskName($controller, $formName = 'Search')
 {
     // total hack, check POST vars and then session to determine stuff
     $value = CRM_Utils_Array::value('task', $_POST);
     if (!isset($value)) {
         $value = $this->_controller->get('task');
     }
     $this->_controller->set('task', $value);
     return CRM_Relationship_Task::getTask($value);
 }