function test_groupBy() { $record =& df_get_record('People', array('PersonID' => 1)); $pubs = $record->getRelatedRecords('Publications', 'all'); $categories = Dataface_Utilities::groupBy('PubType', $pubs); $this->assertEquals(array('Refereed Journal', 'Book Chapter', 'Conference'), array_keys($categories)); $this->assertEquals(64, sizeof($categories['Refereed Journal'])); $this->assertEquals(64, sizeof($categories['Book Chapter'])); $this->assertEquals(63, sizeof($categories['Conference'])); }
/** * Groups an array of Records (or associative arrays) together based on a specific field. * @param array $params Array of parameters * @param Dataface_SkinTool &$smarty Reference to Smarty template engine. * @param array $params[from] The array that is to be grouped. * @param string $params[var] The name of the variable to assign the grouped structure to. * @param string $params[on] The name of the field on which to group the records. * @param string $params[order] A comma-delimited string of order directives to specify the * order in which the records should be displayed. * @param string $params[titles] Titles for the groups in a format similar to css attributes. * */ function group($params, &$smarty) { import('Dataface/Utilities.php'); if (empty($params['from'])) { throw new Exception('group: Please specify a from parameter.', E_USER_ERROR); } if (empty($params['var'])) { throw new Exception('group: Please specify a var parameter.', E_USER_ERROR); } if (empty($params['on'])) { throw new Exception('group: Please specify a field parameter.', E_USER_ERROR); } if (!empty($params['order'])) { $order = explode(',', $params['order']); } else { $order = array(); } if (!empty($params['titles'])) { $titles = array_map('trim', explode(';', $params['titles'])); $titles2 = array(); foreach ($titles as $title) { list($titleKey, $titleValue) = array_map('trim', explode(':', $title)); $titles2[$titleKey] = $titleValue; } } else { $titles2 = array(); } $cats = Dataface_Utilities::groupBy($params['on'], $params['from'], $order, $titles2); $context = array($params['var'] => &$cats); $smarty->assign($context); }