/**
  * Function to add or update segment
  * 
  * @param array $params
  * @return array $result
  * @throws Exception when params empty
  * @access public
  * @static
  */
 public static function add($params)
 {
     $result = array();
     $preSegment = array();
     if (empty($params)) {
         throw new Exception('Params can not be empty when adding or updating a segment', 9003);
     }
     $segment = new CRM_Contactsegment_BAO_Segment();
     if (isset($params['id'])) {
         $segment->id = $params['id'];
         // pre hook if edit
         $op = "edit";
         self::storeValues($segment, $preSegment);
         CRM_Utils_Hook::pre($op, 'Segment', $segment->id, $preSegment);
         $segment->find(true);
     } else {
         $op = "create";
     }
     $fields = self::fields();
     foreach ($params as $paramKey => $paramValue) {
         if (isset($fields[$paramKey])) {
             $segment->{$paramKey} = $paramValue;
         }
     }
     if (!$segment->name && $segment->label) {
         $segment->name = CRM_Contactsegment_Utils::generateNameFromLabel($segment->label);
     }
     $segment->save();
     if (!$segment->is_active) {
         CRM_Contactsegment_BAO_Segment::setInactive($segment->id);
     }
     // post hook
     CRM_Utils_Hook::post($op, 'Segment', $segment->id, $segment);
     self::storeValues($segment, $result);
     return $result;
 }
 /**
  * Method to retrieve or create option group for roles
  *
  * @throws Exception when no group found and unable to create one
  */
 private function setRoleOptionGroup()
 {
     $resourcesArray = $this->getJsonResourcesArray("segment_roles.json");
     $currentOptionGroup = CRM_Contactsegment_Utils::getOptionGroupWithName($resourcesArray['name']);
     if ($currentOptionGroup == FALSE) {
         $createOptionGroupParams = array('name' => $resourcesArray['name'], 'title' => $resourcesArray['title'], 'description' => $resourcesArray['description'], 'is_active' => $resourcesArray['is_active'], 'is_reserved' => $resourcesArray['is_reserved']);
         $currentOptionGroup = CRM_Contactsegment_Utils::createOptionGroup($createOptionGroupParams);
         if ($currentOptionGroup == FALSE) {
             throw new Exception("Could not create a new option group with name civicoop_contact_segment nor find an existing one", 9001);
         }
     }
     $optionValues = array();
     foreach ($resourcesArray['values'] as $resourceName => $resourceValue) {
         $resourceValue['option_group_id'] = $currentOptionGroup['id'];
         $optionValue = CRM_Contactsegment_Utils::createOptionValue($resourceValue);
         if ($optionValue != FALSE) {
             foreach ($optionValue['values'] as $optionValueId => $optionValue) {
                 $optionValues[$optionValueId] = $optionValue;
             }
         }
     }
     $currentOptionGroup['values'] = $optionValues;
     $this->_roleOptionGroup = $currentOptionGroup;
 }
 /**
  * Method to get select list of possible parent segments
  *
  * @access protected
  * @return array
  */
 protected function getParentList()
 {
     $parentList = array("- select -") + CRM_Contactsegment_Utils::getParentList();
     asort($parentList);
     return $parentList;
 }
 /**
  * Method to build a contact segment row, active or past
  *
  * @param object $dao
  * @return array $row
  * @access private
  */
 private function buildContactSegmentRow($dao)
 {
     $row = array();
     if (empty($dao->parent_id)) {
         $row['type'] = $this->_segmentSetting['parent_label'];
         $row['label'] = $dao->label;
     } else {
         $row['type'] = $this->_segmentSetting['child_label'];
         $row['label'] = ' - ' . $dao->label;
     }
     $row['start_date'] = $dao->start_date;
     if (isset($dao->end_date) && !empty($dao->end_date)) {
         $row['end_date'] = $dao->end_date;
     }
     $row['role'] = CRM_Contactsegment_Utils::getRoleLabel($dao->role_value);
     $row['actions'] = $this->buildRowActions($dao);
     return $row;
 }
 /**
  * Method to validate if role is unique and already active
  *
  * @param array $fields
  * @return array $errors or TRUE
  * @access public
  * @static
  */
 static function validateRoleUnique($fields)
 {
     $errors = array();
     $checkParams = array('role' => $fields['contact_segment_role'], 'contact_id' => $fields['contact_id'], 'start_date' => $fields['start_date'], 'end_date' => $fields['end_date']);
     if (!empty($fields['segment_child'])) {
         if (CRM_Contactsegment_Utils::isSegmentRoleUnique($fields['contact_segment_role'], 'child') == TRUE) {
             $checkParams['segment_id'] = $fields['segment_child'];
             if (CRM_Contactsegment_Utils::activeCurrentContactSegmentForRole($checkParams) != FALSE) {
                 $errors['segment_child'] = ts('Only 1 active role allowed, there is already an active ' . $fields['contact_segment_role']);
                 return $errors;
             }
         }
     } else {
         if (CRM_Contactsegment_Utils::isSegmentRoleUnique($fields['contact_segment_role'], 'parent') == TRUE) {
             $checkParams['segment_id'] = $fields['segment_parent'];
             if (CRM_Contactsegment_Utils::activeCurrentContactSegmentForRole($checkParams) != FALSE) {
                 $errors['segment_parent'] = ts('Only 1 active role allowed, there is already an active ' . $fields['contact_segment_role']);
                 return $errors;
             }
         }
     }
     return TRUE;
 }
 /**
  * Function to add form elements
  *
  * @access protected
  */
 protected function addFormElements()
 {
     $roleList = CRM_Contactsegment_Utils::getRoleList();
     $this->add('text', 'parent_label', ts('Label'), array('size' => 80), true);
     $this->add('text', 'child_label', ts('Label'), array('size' => 80), true);
     $parentRoleSelect = $this->addElement('advmultiselect', 'parent_roles', ts('Roles'), $roleList, array('size' => 5, 'style' => 'width:280px', 'class' => 'advmultselect'), TRUE);
     $parentRoleSelect->setButtonAttributes('add', array('value' => ts('Use for parent') . " >>"));
     $parentRoleSelect->setButtonAttributes('remove', array('value' => "<< " . ts('Not used for parent')));
     $childRoleSelect = $this->addElement('advmultiselect', 'child_roles', ts('Roles'), $roleList, array('size' => 5, 'style' => 'width:280px', 'class' => 'advmultselect'), TRUE);
     $childRoleSelect->setButtonAttributes('add', array('value' => ts('Use for child') . " >>"));
     $childRoleSelect->setButtonAttributes('remove', array('value' => "<< " . ts('Not used for child')));
     $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => true), array('type' => 'cancel', 'name' => ts('Cancel'))));
 }
/**
 * Implementation of hook civicrm_navigationMenu
 * to add a contact segment menu item to the Administer/Customize Data and Screens menu
 *
 * @param array $params
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
 */
function contactsegment_civicrm_navigationMenu(&$params)
{
    $maxKey = CRM_Contactsegment_Utils::getMaxMenuKey($params);
    $menuAdministerId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Administer', 'id', 'name');
    $menuParentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Customize Data and Screens', 'id', 'name');
    $params[$menuAdministerId]['child'][$menuParentId]['child'][$maxKey + 1] = array('attributes' => array('label' => ts('Manage Contact Segments'), 'name' => ts('Manage Contact Segments'), 'url' => NULL, 'permission' => NULL, 'operator' => NULL, 'separator' => NULL, 'parentID' => $menuParentId, 'navID' => $maxKey + 1, 'active' => 1), 'child' => array(1 => array('attributes' => array('label' => ts('Contact Segment Settings'), 'name' => ts('Contact Segment Settings'), 'url' => CRM_Utils_System::url('civicrm/segmentsetting', 'reset=1&type=parent', true), 'permission' => 'administer CiviCRM', 'operator' => NULL, 'separator' => NULL, 'parentID' => $maxKey + 1, 'navID' => 1, 'active' => 1)), 2 => array('attributes' => array('label' => ts('Contact Segments'), 'name' => ts('Contact Segments'), 'url' => CRM_Utils_System::url('civicrm/segmentlist', 'force=1&crmRowCount=25', true), 'permission' => 'administer CiviCRM', 'operator' => NULL, 'separator' => NULL, 'parentID' => $maxKey + 1, 'navID' => 2, 'active' => 1))));
}