/**
  * resolve memberroles
  *
  * @param $records
  */
 protected function _resolveMemberroles($records)
 {
     $listRoles = Addressbook_Controller_ListRole::getInstance()->getAll();
     $contactIds = array();
     foreach ($records as $record) {
         if (isset($record->memberroles)) {
             $contactIds = array_merge($contactIds, $record->memberroles->contact_id);
         }
     }
     if (count($contactIds) > 0) {
         $contacts = Addressbook_Controller_Contact::getInstance()->getMultiple($contactIds);
     }
     foreach ($records as $list) {
         if (isset($record->memberroles)) {
             foreach ($list->memberroles as $memberrole) {
                 $contact = $contacts->getById($memberrole->contact_id);
                 if ($contact) {
                     $memberrole->contact_id = $contact;
                 }
                 $listRole = $listRoles->getById($memberrole->list_role_id);
                 if ($listRole) {
                     $memberrole->list_role_id = $listRole;
                 }
             }
         }
     }
 }
 /**
  * returns array with the filter settings of this filter group
  *
  * @param  bool $_valueToJson resolve value for json api?
  * @return array
  */
 public function toArray($_valueToJson = false)
 {
     if (is_string($this->_value)) {
         $this->_value = Addressbook_Controller_ListRole::getInstance()->get($this->_value)->toArray();
     }
     return parent::toArray($_valueToJson);
 }
 /**
  * the singleton pattern
  *
  * @return Addressbook_Controller_ListRole
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Addressbook_Controller_ListRole();
     }
     return self::$_instance;
 }
 /**
  * save list role
  *
  * @param $recordData
  * @return array
  */
 public function saveListRole($recordData)
 {
     return $this->_save($recordData, Addressbook_Controller_ListRole::getInstance(), 'ListRole');
 }
 /**
  * generates path for the contact
  *
  * - we add to the path:
  *      - lists contact is member of
  *      - we add list role memberships
  *
  * @param Tinebase_Record_Abstract $record
  * @return Tinebase_Record_RecordSet
  */
 public function generatePathForRecord($record)
 {
     $result = new Tinebase_Record_RecordSet('Tinebase_Model_Path');
     // fetch all groups and role memberships and add to path
     $listIds = Addressbook_Controller_List::getInstance()->getMemberships($record);
     foreach ($listIds as $listId) {
         /** @var Addressbook_Model_List $list */
         $list = Addressbook_Controller_List::getInstance()->get($listId);
         $listPaths = $this->_getPathsOfRecord($list);
         if (count($listPaths) === 0) {
             // add self
             $listPaths->addRecord(new Tinebase_Model_Path(array('path' => $this->_getPathPart($list), 'shadow_path' => '/' . $list->getId(), 'record_id' => $list->getId(), 'creation_time' => Tinebase_DateTime::now())));
         }
         foreach ($listPaths as $listPath) {
             if (count($list->memberroles) > 0) {
                 foreach ($list->memberroles as $role) {
                     $rolePath = clone $listPath;
                     if ($role->contact_id === $record->getId()) {
                         $role = Addressbook_Controller_ListRole::getInstance()->get($role->list_role_id);
                         $rolePath->path .= $this->_getPathPart($role);
                         $rolePath->shadow_path .= '/' . $role->getId();
                         $rolePath->record_id = $role->getId();
                         $result->addRecord($rolePath);
                     }
                 }
             } else {
                 $result->addRecord($listPath);
             }
         }
     }
     foreach ($result as $listPath) {
         $listPath->path .= $this->_getPathPart($record);
         $listPath->shadow_path .= '/' . $record->getId();
         $listPath->record_id = $record->getId();
     }
     return $result;
 }