/**
  * Method to save the settings in json file in resources
  *
  * @throws Exception when not able to write settings
  */
 private function saveSegmentConfig()
 {
     $config = CRM_Contactsegment_Config::singleton();
     $fileName = $config->getResourcesPath() . 'segment_config.json';
     try {
         $fh = fopen($fileName, 'w');
         fwrite($fh, json_encode($this->_segmentSettingArray, JSON_PRETTY_PRINT));
         fclose($fh);
     } catch (Exception $ex) {
         throw new Exception('Could not open segment_config.json, contact your system administrator. Error reported: ' . $ex->getMessage());
     }
 }
 /**
  * CRM_Contactsegment_Form_Report_ContactSegment constructor.
  */
 function __construct()
 {
     $this->_exposeContactID = FALSE;
     $config = CRM_Contactsegment_Config::singleton();
     $roleOptionGroup = $config->getRoleOptionGroup();
     foreach ($roleOptionGroup['values'] as $optionValueId => $optionValue) {
         $roleList[$optionValue['value']] = $optionValue['label'];
     }
     asort($roleList);
     $activeLabels = array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes'));
     $this->_columns = array('civicrm_contact_segment' => array('dao' => 'CRM_Contactsegment_DAO_ContactSegment', 'alias' => 'cs', 'fields' => array('id' => array('no_display' => TRUE, 'required' => TRUE), 'contact_id' => array('no_display' => TRUE, 'required' => TRUE), 'is_active' => array('no_display' => TRUE, 'required' => TRUE), 'segment_id' => array('no_display' => TRUE, 'required' => TRUE), 'role_value' => array('title' => ts('Role'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING), 'start_date' => array('title' => ts('Start Date'), 'default' => TRUE), 'end_date' => array('title' => ts('End Date'), 'default' => TRUE)), 'filters' => array('role_value' => array('title' => ts('Roles'), 'type' => CRM_Utils_Type::T_STRING, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $roleList), 'start_date' => array('title' => ts('Start Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'is_active' => array('title' => ts('Active?'), 'type' => CRM_Report_Form::OP_INT, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $activeLabels, 'default' => 1))), 'civicrm_segment' => array('dao' => 'CRM_Contactsegment_DAO_Segment', 'alias' => 'segment', 'fields' => array('label' => array('title' => 'Segment', 'type' => CRM_Utils_Type::T_STRING, 'default' => TRUE, 'required' => TRUE), 'parent_id' => array('no_display' => TRUE, 'required' => TRUE))), 'civicrm_contact' => array('dao' => 'CRM_Contact_DAO_Contact', 'alias' => 'contact', 'fields' => array('display_name' => array('title' => ts('Contact Name'), 'required' => TRUE, 'type' => CRM_Utils_Type::T_STRING), 'gender_id' => array('title' => ts('Gender'), 'type' => CRM_Utils_Type::T_STRING), 'birth_date' => array('title' => ts('Birth Date'), 'type' => CRM_Utils_Type::T_DATE))), 'civicrm_phone' => array('dao' => 'CRM_Core_DAO_Phone', 'alias' => 'phone', 'fields' => array('phone' => array('title' => ts('Phone'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING))), 'civicrm_email' => array('dao' => 'CRM_Core_DAO_Email', 'alias' => 'email', 'fields' => array('email' => array('title' => ts('Email'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING))), 'civicrm_address' => array('dao' => 'CRM_Core_DAO_Address', 'alias' => 'addr', 'fields' => array('street_address' => array('title' => ts('Address'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING), 'postal_code' => array('title' => ts('Post Code'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING), 'city' => array('title' => ts('City'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING), 'country_id' => array('title' => ts('Country'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_STRING))));
     $this->_groupFilter = FALSE;
     $this->_tagFilter = FALSE;
     parent::__construct();
 }
 /**
  * Method to get list of possible roles for a segment
  *
  * @return array
  * @throws CiviCRM_API3_Exception
  *
  */
 public static function getRoleList()
 {
     $roleList = array();
     $config = CRM_Contactsegment_Config::singleton();
     $optionValueParams = array('option_group_id' => $config->getRoleOptionGroup('id'), 'is_active' => 1);
     $roles = civicrm_api3('OptionValue', 'Get', $optionValueParams);
     foreach ($roles['values'] as $optionValueId => $optionValue) {
         $roleList[$optionValue['value']] = $optionValue['label'];
     }
     return $roleList;
 }
 /**
  * Method to get a contact with a given role for a given segment on a specific date
  * (assumption is that there is only 1 contact. If there are more, method will return the first one found)
  *
  * @param string $role
  * @param int $segmentId
  * @param date $testDate
  * @return bool|array
  * @access public
  * @static
  */
 public static function getRoleContactActiveOnDate($role, $segmentId, $testDate)
 {
     if (empty($role) || empty($segmentId) || empty($testDate)) {
         return FALSE;
     }
     $testDate = new DateTime($testDate);
     $config = CRM_Contactsegment_Config::singleton();
     $roleOptionGroup = $config->getRoleOptionGroup();
     $roleFound = FALSE;
     foreach ($roleOptionGroup['values'] as $roleOptionValue) {
         if ($roleOptionValue['label'] == $role) {
             $roleFound = TRUE;
         }
     }
     if ($roleFound) {
         $params = array('segment_id' => $segmentId, 'role_value' => $role);
         $roleContactSegments = civicrm_api3('ContactSegment', 'Get', $params);
         foreach ($roleContactSegments['values'] as $contactSegment) {
             $startDate = new DateTime($contactSegment['start_date']);
             if ($testDate >= $startDate) {
                 if (empty($contactSegment['end_date'])) {
                     return $contactSegment['contact_id'];
                 } else {
                     $endDate = new DateTime($contactSegment['end_date']);
                     if ($endDate >= $testDate) {
                         return $contactSegment['contact_id'];
                     }
                 }
             }
         }
     }
     return FALSE;
 }
 /**
  * Method to return singleton object
  *
  * @return object $_singleton
  * @access public
  * @static
  */
 public static function &singleton()
 {
     if (self::$_singleton === NULL) {
         self::$_singleton = new CRM_Contactsegment_Config();
     }
     return self::$_singleton;
 }
/**
 * Implements hook_civicrm_enable().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
 */
function contactsegment_civicrm_enable()
{
    // instantiate config to create option group if not exists
    CRM_Contactsegment_Config::singleton();
    _contactsegment_civix_civicrm_enable();
}