/**
  * 
  * 
  */
 function list_dimension_members($member_id, $context_dimension_id, $object_type_id, $allowed_member_type_ids)
 {
     if ($member_id != 0) {
         $contact_pg_ids = ContactPermissionGroups::getPermissionGroupIdsByContactCSV(logged_user()->getId(), false);
         $member = members::findById($member_id);
         $dimension = Dimensions::getDimensionById($context_dimension_id);
         if ($object_type_id != null) {
             $dimension_object_type_contents = $dimension->getObjectTypeContent($object_type_id);
             foreach ($dimension_object_type_contents as $dotc) {
                 $dot_id = $dotc->getDimensionObjectTypeId();
                 if (is_null($allowed_member_type_ids) || in_array($dot_id, $allowed_member_type_ids)) {
                     $allowed_object_type_ids[] = $dot_id;
                 }
             }
         }
         if ($dimension instanceof Dimension && $member instanceof Member) {
             if (!$dimension->getDefinesPermissions() || $dimension->hasAllowAllForContact($contact_pg_ids)) {
                 $dimension_members = $dimension->getAllMembers(false, "parent_member_id, name", true);
             } else {
                 if ($dimension->hasCheckForContact($contact_pg_ids)) {
                     $member_list = $dimension->getAllMembers(false, "parent_member_id, name", true);
                     $allowed_members = array();
                     foreach ($member_list as $dim_member) {
                         if (ContactMemberPermissions::instance()->contactCanReadMemberAll($contact_pg_ids, $dim_member->getId(), logged_user())) {
                             $allowed_members[] = $dim_member;
                         }
                     }
                     $dimension_members = $allowed_members;
                 }
             }
             $members_to_retrieve = array();
             $association_ids = DimensionMemberAssociations::getAllAssociationIds($member->getDimensionId(), $context_dimension_id);
             if (count($association_ids) > 0) {
                 $associated_members_ids_csv = '';
                 foreach ($association_ids as $id) {
                     $association = DimensionMemberAssociations::findById($id);
                     $children = $member->getAllChildrenInHierarchy();
                     if ($association->getDimensionId() == $context_dimension_id) {
                         $new_csv = MemberPropertyMembers::getAllMemberIds($id, $member_id);
                         $associated_members_ids_csv .= $new_csv != '' ? $new_csv . "," : '';
                         foreach ($children as $child) {
                             $new_csv = MemberPropertyMembers::getAllMemberIds($id, $child->getId());
                             $associated_members_ids_csv .= $new_csv != '' ? $new_csv . "," : '';
                         }
                     } else {
                         $new_csv = MemberPropertyMembers::getAllPropertyMemberIds($id, $member_id) . ",";
                         $associated_members_ids_csv .= $new_csv != '' ? $new_csv . "," : '';
                         foreach ($children as $child) {
                             $new_csv = MemberPropertyMembers::getAllPropertyMemberIds($id, $child->getId());
                             $associated_members_ids_csv .= $new_csv != '' ? $new_csv . "," : '';
                         }
                     }
                 }
                 $associated_members_ids = explode(',', $associated_members_ids_csv);
                 $associated_members_ids = array_unique($associated_members_ids);
             }
             if (isset($associated_members_ids) && count($associated_members_ids) > 0) {
                 foreach ($associated_members_ids as $id) {
                     $associated_member = Members::findById($id);
                     if (in_array($associated_member, $dimension_members)) {
                         $context_hierarchy_members = $associated_member->getAllParentMembersInHierarchy(true);
                         foreach ($context_hierarchy_members as $context_member) {
                             if (!in_array($context_member, $members_to_retrieve) && in_array($context_member, $dimension_members)) {
                                 $members_to_retrieve[$context_member->getName()] = $context_member;
                             }
                         }
                     }
                 }
                 // alphabetical order
                 $members_to_retrieve = array_ksort($members_to_retrieve);
             } else {
                 $members_to_retrieve[] = $dimension_members;
             }
             $membersset = array();
             foreach ($members_to_retrieve as $m) {
                 $membersset[$m->getId()] = true;
             }
             $members = array();
             // Todo adapt this code to call "buildMemberList" - (performance and code improvement)
             foreach ($members_to_retrieve as $m) {
                 if ($m->getArchivedById() > 0) {
                     continue;
                 }
                 if ($object_type_id != null) {
                     $selectable = in_array($m->getObjectTypeId(), $allowed_object_type_ids) ? true : false;
                 }
                 $tempParent = $m->getParentMemberId();
                 $x = $m;
                 while ($x instanceof Member && !isset($membersset[$tempParent])) {
                     $tempParent = $x->getParentMemberId();
                     $x = $x->getParentMember();
                 }
                 if (!$x instanceof Member) {
                     $tempParent = 0;
                 }
                 if ($dot = DimensionObjectTypes::instance()->findOne(array("conditions" => "\n\t\t\t\t\t\tdimension_id = " . $dimension->getId() . " AND\n\t\t\t\t\t\tobject_type_id = " . $m->getObjectTypeId()))) {
                     $memberOptions = $dot->getOptions(true);
                 } else {
                     $memberOptions = '';
                 }
                 /* @var $m Member */
                 $member = array("id" => $m->getId(), "name" => clean($m->getName()), "parent" => $tempParent, "realParent" => $m->getParentMemberId(), "object_id" => $m->getObjectId(), "options" => $memberOptions, "depth" => $m->getDepth(), "iconCls" => $m->getIconClass(), "selectable" => isset($selectable) ? $selectable : false, "dimension_id" => $m->getDimensionId(), "object_type_id" => $m->getObjectTypeId(), "allow_childs" => $m->allowChilds());
                 if ($oid = $m->getObjectId()) {
                     if ($obj = Objects::instance()->findObject($m->getObjectId())) {
                         $editUrl = $obj->getEditUrl();
                     }
                 }
                 // Member Actions
                 if (can_manage_dimension_members(logged_user())) {
                     if ($oid = $m->getObjectId()) {
                         if ($obj = Objects::instance()->findObject($m->getObjectId())) {
                             $editUrl = $obj->getEditUrl();
                         }
                     } else {
                         $editUrl = get_url('member', 'edit', array('id' => $m->getId()));
                     }
                     $member['actions'] = array(array('url' => $editUrl, 'text' => '', 'iconCls' => 'ico-edit'));
                 }
                 $members[] = $member;
             }
             return $members;
         }
         return null;
     } else {
         $members = $this->initial_list_dimension_members($context_dimension_id, $object_type_id, $allowed_member_type_ids);
         return $members;
     }
 }