/** * Returns HTML <select> element containing the specified list, or portion of the list. * * @param mixed $pm_list_name_or_id * @param string $ps_name * @param array $pa_attributes * @param array $pa_options Array of options. Valid options include: * childrenOnlyForItemID = if set only items below item_id in the list item hierarchy are returned. Default (null) is to return all items in the list. * directChildrenOnly = if set only items with item_id=childrenOnlyForItemID as parent in the list item hierarchy are returned. Default (null) is to return all items in the list. * nullOption = if set then a "null" (no value) option is available labeled with the value passed in this option * additionalOptions = an optional array of options that will be passed through to caHTMLSelect; keys are display labels and values are used as option values * value = if set, the <select> will have default selection set to the item whose *value* matches the option value. If none is set then the first item in the list will be selected * key = ca_list_item field to be used as value for the <select> element list; can be set to either item_id or item_value; default is item_id * width = the display width of the list in characters or pixels * limitToItemsWithID = An optional array of list item_ids. Item_ids not in the array will be omitted from the returned list. * omitItemsWithID = An optional array of list item_ids. Item_ids in the array will be omitted from the returned list. * disableItemsWithID = An optional array of list item_ids. Item_ids in the array will be disabled in the returned list. * * limitToItemsRelatedToCollections = an array of collection_ids or collection idno's; returned items will be restricted to those attached to the specified collections * limitToItemsRelatedToCollectionWithRelationshipTypes = array of collection type names or type_ids; returned items will be restricted to those attached to the specified collectionss with the specified relationship type * limitToListIDs = array of list_ids to restrict returned items to when using "limitToItemsRelatedToCollections" * * indentForHierarchy = indicate hierarchy with indentation. [Default is true] * transaction = transaction to perform database operations within. [Default is null] * * @return string - HTML code for the <select> element; empty string if the list is empty */ public static function getListAsHTMLFormElement($pm_list_name_or_id, $ps_name, $pa_attributes = null, $pa_options = null) { $t_list = new ca_lists(); if ($o_trans = caGetOption('transaction', $pa_options, null)) { $t_list->setTransaction($o_trans); } if (!is_array($pa_options)) { $pa_options = array(); } if (!(isset($pa_options['limitToItemsRelatedToCollection']) && is_array($pa_options['limitToItemsRelatedToCollections']))) { $vn_list_id = $t_list->_getListID($pm_list_name_or_id); $t_list->load($vn_list_id); } $vn_root_id = isset($pa_options['childrenOnlyForItemID']) && $pa_options['childrenOnlyForItemID'] ? $pa_options['childrenOnlyForItemID'] : null; $va_disabled_item_ids = caGetOption('disableItemsWithID', $pa_options, null); $vs_render_as = isset($pa_options['render']) ? $pa_options['render'] : ''; $vn_sort_type = $t_list->get('default_sort'); if ($vs_render_as == 'yes_no_checkboxes' && $vn_sort_type == __CA_LISTS_SORT_BY_LABEL__) { $vn_sort_type = __CA_LISTS_SORT_BY_IDENTIFIER__; // never allow sort-by-label when rendering as yes/no checkbox } if (!in_array($vs_render_as, array('lookup', 'horiz_hierbrowser', 'vert_hierbrowser'))) { if (isset($pa_options['limitToItemsRelatedToCollections']) && is_array($pa_options['limitToItemsRelatedToCollections'])) { $t_collection = new ca_collections(); $va_collection_ids = array(); foreach ($pa_options['limitToItemsRelatedToCollections'] as $vn_collection_id) { if ($vn_collection_id && !is_numeric($vn_collection_id)) { if ($vn_collection_id = $t_collection->load(array('idno' => $vn_collection_id))) { $va_collection_ids[] = $vn_collection_id; } } else { if ($vn_collection_id) { $va_collection_ids[] = $vn_collection_id; } } } if (sizeof($va_collection_ids)) { $qr_collections = $t_list->makeSearchResult('ca_collections', $va_collection_ids, array('restrictToRelationshipTypes' => isset($pa_options['limitToItemsRelatedToCollectionWithRelationshipTypes']) ? $pa_options['limitToItemsRelatedToCollectionWithRelationshipTypes'] : null)); $va_item_ids = array(); while ($qr_collections->nextHit()) { $va_list_items = $qr_collections->get('ca_list_items', array('returnAsArray' => true)); foreach ($va_list_items as $vn_rel_id => $va_list_item) { $va_item_ids[$vn_rel_id] = $va_list_item['item_id']; } } if ($va_limit_to_listIDs = isset($pa_options['limitToListIDs']) && is_array($pa_options['limitToListIDs']) ? $pa_options['limitToListIDs'] : null) { // for some reason the option comes back as array(0 => null) if no list is selected in UI // -> have to make sure to catch this case here if (sizeof($va_limit_to_listIDs) == 1 && empty($va_limit_to_listIDs[0])) { $va_limit_to_listIDs = null; } } if (is_array($va_limit_to_listIDs) && sizeof($va_item_ids)) { // filter out items from tables we don't want $qr_list_items = $t_list->makeSearchResult("ca_list_items", array_values($va_item_ids)); while ($qr_list_items->nextHit()) { if (!in_array($qr_list_items->get('ca_list_items.list_id'), $va_limit_to_listIDs)) { if (is_array($va_k = array_keys($va_item_ids, $qr_list_items->get('ca_list_items.item_id')))) { foreach ($va_k as $vs_k) { unset($va_list_items[$vs_k]); } } } } } } } else { $va_list_items = $t_list->getItemsForList($pm_list_name_or_id, array_merge($pa_options, array('returnHierarchyLevels' => caGetOption('indentForHierarchy', $pa_options, true), 'item_id' => $vn_root_id, 'extractValuesByUserLocale' => true, 'sort' => $vn_sort_type))); } } if (!is_array($va_list_items)) { $va_list_items = array(); } $va_options = array(); $va_disabled_options = array(); if (!isset($pa_options['value'])) { $pa_options['value'] = null; } if (!isset($pa_options['key'])) { $pa_options['key'] = 'item_id'; } if (!in_array($pa_options['key'], array('item_id', 'item_value'))) { $pa_options['key'] = 'item_id'; } if (!isset($pa_options['limitToItemsWithID']) || !is_array($pa_options['limitToItemsWithID']) || !sizeof($pa_options['limitToItemsWithID'])) { $pa_options['limitToItemsWithID'] = null; } if (!isset($pa_options['omitItemsWithID']) || !is_array($pa_options['omitItemsWithID']) || !sizeof($pa_options['omitItemsWithID'])) { $pa_options['omitItemsWithID'] = null; } if (isset($pa_options['nullOption']) && $pa_options['nullOption'] && $vs_render_as != 'checklist') { $va_options[''] = $pa_options['nullOption']; } $va_colors = array(); $vn_default_val = null; foreach ($va_list_items as $vn_item_id => $va_item) { if (is_array($pa_options['limitToItemsWithID']) && !in_array($vn_item_id, $pa_options['limitToItemsWithID'])) { continue; } if (is_array($pa_options['omitItemsWithID']) && in_array($vn_item_id, $pa_options['omitItemsWithID'])) { continue; } $va_options[$va_item[$pa_options['key']]] = str_repeat(' ', intval($va_item['LEVEL']) * 3) . ' ' . $va_item['name_singular']; if (!$va_item['is_enabled'] || is_array($va_disabled_item_ids) && in_array($vn_item_id, $va_disabled_item_ids)) { $va_disabled_options[$va_item[$pa_options['key']]] = true; } $va_colors[$vn_item_id] = $va_item['color']; if ($va_item['is_default']) { $vn_default_val = $va_item[$pa_options['key']]; } // get default value if ($va_item['is_default'] && !isset($pa_options['nullOption'])) { // set default if needed, but only if there's not a null option set if (!is_array($pa_options['value']) && (!isset($pa_options['value']) || !strlen($pa_options['value']))) { $pa_options['value'] = $vn_default_val; } else { if (is_array($pa_options['value']) && !sizeof($pa_options['value'])) { $pa_options['value'] = array(0 => $vn_default_val); } } } } if (isset($pa_options['additionalOptions']) && is_array($pa_options['additionalOptions'])) { $va_options = array_merge($va_options, array_flip($pa_options['additionalOptions'])); } $pa_options['disabledOptions'] = $va_disabled_options; switch ($vs_render_as) { case 'radio_buttons': if (!sizeof($va_options)) { return ''; } // return empty string if list has no values $vn_c = 0; $vn_i = 0; $vs_buf = "<table>\n"; foreach ($va_options as $vm_value => $vs_label) { if ($vn_c == 0) { $vs_buf .= "<tr>"; } $va_attributes = array('value' => $vm_value); if (isset($va_disabled_options[$vm_value]) && $va_disabled_options[$vm_value]) { $va_attributes['disabled'] = 1; } $va_attributes['value'] = $vm_value; $va_attributes['id'] = $ps_name . '_' . $vn_i; if ($pa_options['value'] == $vm_value) { $va_attributes['checked'] = '1'; } if (isset($pa_options['readonly']) && $pa_options['readonly']) { $va_attributes['disabled'] = 1; } $vs_buf .= "<td>" . caHTMLRadioButtonInput($ps_name, $va_attributes, $pa_options) . " {$vs_label}</td>"; $vn_c++; if ($vn_c >= $pa_options['maxColumns']) { $vn_c = 0; $vs_buf .= "</tr>\n"; } $vn_i++; } if ($vn_c != 0) { $vs_buf .= "</tr>\n"; } $vs_buf .= "</table>"; return $vs_buf; break; case 'yes_no_checkboxes': if (!sizeof($va_options)) { return ''; } // return empty string if list has no values $vn_c = 0; $vb_is_checked = false; if (!$pa_options['value']) { $pa_options['value'] = (string) $vn_default_val; } foreach ($va_options as $vm_value => $vs_label) { if (strlen($vm_value) == 0) { continue; } // don't count null values when calculating the first value for the yes/no switch ($vn_c) { case 0: if ($pa_options['value'] === (string) $vm_value) { $vb_is_checked = true; } $pa_attributes['value'] = $pa_options['value'] = $vm_value; $pa_options['label'] = $vs_label; break; case 1: $pa_options['returnValueIfUnchecked'] = $vm_value; break; default: // exit break 2; } $vn_c++; } if ($vb_is_checked) { $pa_attributes['checked'] = 1; } if (isset($pa_options['readonly']) && $pa_options['readonly']) { $pa_attributes['disabled'] = 1; } return caHTMLCheckboxInput($ps_name, $pa_attributes, $pa_options); break; case 'checklist': if (!sizeof($va_options)) { return ''; } // return empty string if list has no values $vn_c = 0; $vs_buf = "<table>\n"; foreach ($va_options as $vm_value => $vs_label) { if ($vn_c == 0) { $vs_buf .= "<tr>"; } $va_attributes = array('value' => $vm_value); if (isset($va_disabled_options[$vm_value]) && $va_disabled_options[$vm_value]) { $va_attributes['disabled'] = 1; } if (isset($pa_options['readonly']) && $pa_options['readonly']) { $va_attributes['disabled'] = 1; } if (is_array($pa_options['value']) && in_array($vm_value, $pa_options['value'])) { $va_attributes['checked'] = '1'; } $vs_buf .= "<td>" . caHTMLCheckboxInput($ps_name . '_' . $vm_value, $va_attributes, $pa_options) . " {$vs_label}</td>"; $vn_c++; if ($vn_c >= $pa_options['maxColumns']) { $vn_c = 0; $vs_buf .= "</tr>\n"; } } if ($vn_c != 0) { $vs_buf .= "</tr>\n"; } $vs_buf .= "</table>"; return $vs_buf; break; case 'lookup': $vs_value = $vs_hidden_value = ""; if (caGetOption('forSearch', $pa_options)) { if ($vs_val_id = caGetOption('value', $pa_options)) { $vs_value = $t_list->getItemFromListForDisplayByItemID($pm_list_name_or_id, $vs_val_id); $vs_hidden_value = $vs_val_id; } } else { $vs_value = "{" . $pa_options['element_id'] . "_label}"; $vs_hidden_value = "{" . $pa_options['element_id'] . "}"; } $vs_buf = caHTMLTextInput($ps_name . '_autocomplete', array('width' => isset($pa_options['width']) && $pa_options['width'] > 0 ? $pa_options['width'] : 300, 'height' => isset($pa_options['height']) && $pa_options['height'] > 0 ? $pa_options['height'] : 1, 'value' => $vs_value, 'maxlength' => 512, 'id' => $ps_name . "_autocomplete", 'class' => 'lookupBg')) . caHTMLHiddenInput($ps_name, array('value' => $vs_hidden_value, 'id' => $ps_name)); if ($pa_options['request']) { $vs_url = caNavUrl($pa_options['request'], 'lookup', 'ListItem', 'Get', array('list' => ca_lists::getListCode($vn_list_id), 'noInline' => 1, 'noSymbols' => 1, 'max' => 100)); } else { // hardcoded default for testing. $vs_url = '/index.php/lookup/ListItem/Get'; } $vs_buf .= '</div>'; $vs_buf .= "\n\t\t\t\t\t<script type='text/javascript'>\n\t\t\t\t\t\tjQuery(document).ready(function() {\n\t\t\t\t\t\t\tjQuery('#{$ps_name}_autocomplete').autocomplete({\n\t\t\t\t\t\t\t\t\tsource: '{$vs_url}', minLength: 3, delay: 800, html: true,\n\t\t\t\t\t\t\t\t\tselect: function(event, ui) {\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\tif (parseInt(ui.item.id) > 0) {\n\t\t\t\t\t\t\t\t\t\t\tjQuery('#{$ps_name}').val(ui.item.id);\n\t\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t\tjQuery('#{$ps_name}_autocomplete').val('');\n\t\t\t\t\t\t\t\t\t\t\tjQuery('#{$ps_name}').val('');\n\t\t\t\t\t\t\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t</script>\n\t\t\t\t"; return $vs_buf; break; case 'horiz_hierbrowser': case 'horiz_hierbrowser_with_search': case 'vert_hierbrowser': $va_width = caParseFormElementDimension($pa_options['width'] ? $pa_options['width'] : $pa_options['width']); if ($va_width['type'] != 'pixels' && $va_width['dimension'] < 250) { $va_width['dimension'] = 500; } $vn_width = $va_width['dimension'] . 'px'; $va_height = caParseFormElementDimension($pa_options['height']); if ($va_height['type'] != 'pixels' && $va_height['dimension'] < 100) { $va_height['dimension'] = 200; } $vn_height = $va_height['dimension'] . 'px'; $t_root_item = new ca_list_items(); $t_root_item->load(array('list_id' => $vn_list_id, 'parent_id' => null)); JavascriptLoadManager::register("hierBrowser"); $vs_buf = "<div style='width: {$vn_width}; height: {$vn_height};'><div id='{$ps_name}_hierarchyBrowser{n}' style='width: 100%; height: 100%;' class='" . ($vs_render_as == 'vert_hierbrowser' ? 'hierarchyBrowserVertical' : 'hierarchyBrowser') . "'>\n\t\t\t\t\t<!-- Content for hierarchy browser is dynamically inserted here by ca.hierbrowser -->\n\t\t\t\t</div><!-- end hierarchyBrowser -->\t</div>"; $vs_buf .= "\t<script type='text/javascript'>\n\t\tjQuery(document).ready(function() { \n\t\t\tvar oHierBrowser = caUI.initHierBrowser('{$ps_name}_hierarchyBrowser{n}', {\n\t\t\t\tuiStyle: '" . ($vs_render_as == 'vert_hierbrowser' ? 'vertical' : 'horizontal') . "',\n\t\t\t\tlevelDataUrl: '" . caNavUrl($pa_options['request'], 'lookup', 'ListItem', 'GetHierarchyLevel', array('noSymbols' => 1)) . "',\n\t\t\t\tinitDataUrl: '" . caNavUrl($pa_options['request'], 'lookup', 'ListItem', 'GetHierarchyAncestorList') . "',\n\t\t\t\t\n\t\t\t\tselectOnLoad : true,\n\t\t\t\tbrowserWidth: " . (int) $va_width['dimension'] . ",\n\t\t\t\t\n\t\t\t\tclassName: '" . ($vs_render_as == 'vert_hierbrowser' ? 'hierarchyBrowserLevelVertical' : 'hierarchyBrowserLevel') . "',\n\t\t\t\tclassNameContainer: '" . ($vs_render_as == 'vert_hierbrowser' ? 'hierarchyBrowserContainerVertical' : 'hierarchyBrowserContainer') . "',\n\t\t\t\t\n\t\t\t\teditButtonIcon: \"" . caNavIcon($pa_options['request'], __CA_NAV_BUTTON_RIGHT_ARROW__) . "\",\n\t\t\t\tdisabledButtonIcon: \"" . caNavIcon($pa_options['request'], __CA_NAV_BUTTON_DOT__) . "\",\n\t\t\t\tinitItemID: '{" . $pa_options['element_id'] . "}',\n\t\t\t\tdefaultItemID: '" . $t_list->getDefaultItemID() . "',\n\t\t\t\tuseAsRootID: '" . $t_root_item->getPrimaryKey() . "',\n\t\t\t\tindicatorUrl: '" . $pa_options['request']->getThemeUrlPath() . "/graphics/icons/indicator.gif',\n\t\t\t\t\n\t\t\t\tcurrentSelectionDisplayID: '{$ps_name}_browseCurrentSelectionText{n}',\n\t\t\t\tonSelection: function(item_id, parent_id, name, display) {\n\t\t\t\t\tjQuery('#{$ps_name}').val(item_id);\n\t\t\t\t}\n\t\t\t});"; if ($vs_render_as == 'horiz_hierbrowser_with_search') { $vs_buf .= "jQuery('#{$ps_name}_hierarchyBrowserSearch{n}').autocomplete(\n\t\t\t\t\t{\n\t\t\t\t\t\tsource: '" . caNavUrl($pa_options['request'], 'lookup', 'ListItem', 'Get', array('list' => ca_lists::getListCode($vn_list_id), 'noSymbols' => 1)) . "', \n\t\t\t\t\t\tminLength: 3, delay: 800,\n\t\t\t\t\t\tselect: function(event, ui) {\n\t\t\t\t\t\t\toHierBrowser.setUpHierarchy(ui.item.id);\t// jump browser to selected item\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t);"; } $vs_buf .= "});\n\t</script>"; if ($vs_render_as == 'horiz_hierbrowser_with_search') { $vs_buf .= "<div class='hierarchyBrowserSearchBar'>" . _t('Search') . ": <input type='text' id='{$ps_name}_hierarchyBrowserSearch{n}' class='hierarchyBrowserSearchBar' name='search' value='' size='20'/></div>"; } if ($vs_render_as != 'vert_hierbrowser') { $vs_buf .= "<div id='{$ps_name}_browseCurrentSelection{n}' class='hierarchyBrowserCurrentSelection'>" . _t("Current selection") . ": <span id='{$ps_name}_browseCurrentSelectionText{n}' class='hierarchyBrowserCurrentSelectionText'>?</span></div>"; } $vs_buf .= caHTMLHiddenInput($ps_name, array('value' => "{" . $pa_options['element_id'] . "}", 'id' => $ps_name)); return $vs_buf; break; case 'text': return caHTMLTextInput($ps_name, $pa_attributes, $pa_options); break; case 'options': return $va_options; break; default: if (!sizeof($va_options)) { return ''; } // return empty string if list has no values if (isset($pa_options['readonly']) && $pa_options['readonly']) { $pa_attributes['disabled'] = 1; } return caHTMLSelect($ps_name, $va_options, $pa_attributes, array_merge($pa_options, array('contentArrayUsesKeysForValues' => true, 'colors' => $va_colors, 'height' => null))); break; } }