/**
  * Return the selected constants in an formatted array (see getConstantsByRank to see the format)
  *
  * @param array            $selection The constant you want to select
  * @param string           $type      'form' or 'graph'
  * @param CMbObject|string $host      Host from which we'll get the configuration
  *
  * @return array
  */
 static function selectConstants($selection, $type = 'form', $host = null)
 {
     if ($host) {
         $show_cat_tabs = CConstantesMedicales::getHostConfig("show_cat_tabs", $host);
     } else {
         $show_cat_tabs = CConstantesMedicales::getConfig("show_cat_tabs");
     }
     $constants_by_rank = self::getRanksFor($type, $host);
     $list_constants = CConstantesMedicales::$list_constantes;
     // Keep only valid constant names
     $constants_by_rank = array_intersect_key($constants_by_rank, $list_constants);
     $constants_by_rank = CMbArray::flip($constants_by_rank);
     ksort($constants_by_rank);
     $result = array();
     foreach ($constants_by_rank as $_rank => $_constants) {
         if ($_rank === -1) {
             continue;
         }
         foreach ($_constants as $_constant) {
             if (strpos($_constant, "_") === 0) {
                 continue;
             }
             if ($show_cat_tabs) {
                 $_type = $list_constants[$_constant]["type"];
                 if (!array_key_exists($_type, $result)) {
                     $result[$_type] = array();
                 }
                 if (!in_array($_constant, $selection)) {
                     $rank = -1;
                 } else {
                     $rank = $_rank;
                 }
                 if (!array_key_exists($rank, $result[$_type])) {
                     $result[$_type][$rank] = array();
                 }
                 $result[$_type][$rank][] = $_constant;
             } else {
                 if (!array_key_exists('all', $result)) {
                     $result['all'] = array();
                 }
                 if (!in_array($_constant, $selection)) {
                     $rank = -1;
                 } else {
                     $rank = $_rank;
                 }
                 if (!array_key_exists($rank, $result['all'])) {
                     $result['all'][$rank] = array();
                 }
                 $result['all'][$rank][] = $_constant;
             }
         }
     }
     foreach ($result as $_type => $_ranks) {
         if (array_key_exists(-1, $result[$_type])) {
             $unselected_constants = $result[$_type][-1];
             unset($result[$_type][-1]);
             $result[$_type]["hidden"] = $unselected_constants;
         }
         if (array_key_exists(-1, $result[$_type])) {
             unset($result[$_type][-1]);
         }
     }
     return $result;
 }
Example #2
0
 public function testFlip()
 {
     $this->assertEquals(array("val" => array("key")), $this->stub->flip(array("key" => "val")));
     $this->assertEquals(array("val" => array("key", "key2")), $this->stub->flip(array("key" => "val", "key2" => "val")));
 }