/**
  * Build the order clause of item listings
  * precedence: $request_var ==> $order ==> $config_param ==> $default_order_col (& $default_order_dir)
  * @access private
  * @return string
  */
 static function buildItemOrderBy(&$params = null, &$order = '', $request_var = 'orderby', $config_param = 'orderby', $i_as = 'i', $rel_as = 'rel', $default_order_col_1st = '', $default_order_dir_1st = '', $sfx = '', $support_2nd_lvl = false)
 {
     // Use global params ordering if parameters were not given
     if (!$params) {
         $params = JComponentHelper::getParams('com_flexicontent');
     }
     $order_fallback = 'rdate';
     // Use as default or when an invalid ordering is requested
     $orderbycustomfield = (int) $params->get('orderbycustomfield' . $sfx, 1);
     // Backwards compatibility, defaults to enabled *
     $orderbycustomfieldid = (int) $params->get('orderbycustomfieldid' . $sfx, 0);
     // * but this needs to be set in order for field ordering to be used
     // 1. If a FORCED -ORDER- is not given, then use ordering parameters from configuration. NOTE: custom field ordering takes priority
     if (!$order) {
         $order = $orderbycustomfield && $orderbycustomfieldid ? 'field' : $params->get($config_param . $sfx, $order_fallback);
     }
     // 2. If allowing user ordering override, then get ordering from HTTP request variable
     $order = $params->get('orderby_override') && ($request_order = JRequest::getVar($request_var . $sfx)) ? $request_order : $order;
     // 3. Check various cases of invalid order, print warning, and reset ordering to default
     if ($order == 'field' && !$orderbycustomfieldid) {
         // This can occur only if field ordering was requested explicitly, otherwise an not set 'orderbycustomfieldid' will prevent 'field' ordering
         echo "Custom field ordering was selected, but no custom field is selected to be used for ordering<br/>";
         $order = $order_fallback;
     }
     if ($order == 'commented') {
         if (!file_exists(JPATH_SITE . DS . 'components' . DS . 'com_jcomments' . DS . 'jcomments.php')) {
             echo "jcomments not installed, you need jcomments to use 'Most commented' ordering OR display comments information.<br/>\n";
             $order = $order_fallback;
         }
     }
     $order_col_1st = $default_order_col_1st;
     $order_dir_1st = $default_order_dir_1st;
     flexicontent_db::_getOrderByClause($params, $order, $i_as, $rel_as, $order_col_1st, $order_dir_1st, $sfx);
     $order_arr[1] = $order;
     $orderby = ' ORDER BY ' . $order_col_1st . ' ' . $order_dir_1st;
     // ****************************************************************
     // 2nd level ordering, (currently only supported when no SFX given)
     // ****************************************************************
     if ($sfx != '' || !$support_2nd_lvl) {
         $orderby .= $order_col_1st != $i_as . '.title' ? ', ' . $i_as . '.title' : '';
         $order_arr[2] = '';
         $order = $order_arr;
         return $orderby;
     }
     $order = '';
     // Clear this, thus force retrieval from parameters (below)
     $sfx = '_2nd';
     // Set suffix of second level ordering
     $order_fallback = 'alpha';
     // Use as default or when an invalid ordering is requested
     $orderbycustomfield = (int) $params->get('orderbycustomfield' . $sfx, 1);
     // Backwards compatibility, defaults to enabled *
     $orderbycustomfieldid = (int) $params->get('orderbycustomfieldid' . $sfx, 0);
     // * but this needs to be set in order for field ordering to be used
     // 1. If a FORCED -ORDER- is not given, then use ordering parameters from configuration. NOTE: custom field ordering takes priority
     if (!$order) {
         $order = $orderbycustomfield && $orderbycustomfieldid ? 'field' : $params->get($config_param . $sfx, $order_fallback);
     }
     // 2. If allowing user ordering override, then get ordering from HTTP request variable
     $order = $request_var && ($request_order = JRequest::getVar($request_var . $sfx)) ? $request_order : $order;
     // 3. Check various cases of invalid order, print warning, and reset ordering to default
     if ($order == 'field' && !$orderbycustomfieldid) {
         // This can occur only if field ordering was requested explicitly, otherwise an not set 'orderbycustomfieldid' will prevent 'field' ordering
         echo "Custom field ordering was selected, but no custom field is selected to be used for ordering<br/>";
         $order = $order_fallback;
     }
     if ($order == 'commented') {
         if (!file_exists(JPATH_SITE . DS . 'components' . DS . 'com_jcomments' . DS . 'jcomments.php')) {
             echo "jcomments not installed, you need jcomments to use 'Most commented' ordering OR display comments information.<br/>\n";
             $order = $order_fallback;
         }
     }
     $order_col_2nd = '';
     $order_dir_2nd = '';
     if ($order != 'default') {
         flexicontent_db::_getOrderByClause($params, $order, $i_as, $rel_as, $order_col_2nd, $order_dir_2nd, $sfx);
         $order_arr[2] = $order;
         $orderby .= ', ' . $order_col_2nd . ' ' . $order_dir_2nd;
     }
     // Order by title after default ordering
     $orderby .= $order_col_1st != $i_as . '.title' && $order_col_2nd != $i_as . '.title' ? ', ' . $i_as . '.title' : '';
     $order = $order_arr;
     return $orderby;
 }