static function calculateItemMarkups($items, $params)
 {
     global $globalcats;
     global $globalnoroute;
     $globalnoroute = !is_array($globalnoroute) ? array() : $globalnoroute;
     $db = JFactory::getDBO();
     $user = JFactory::getUser();
     $aids = JAccess::getAuthorisedViewLevels($user->id);
     // **************************************
     // Get configuration about markups to add
     // **************************************
     // Get addcss parameters
     $mu_addcss_cats = $params->get('mu_addcss_cats', array('featured'));
     $mu_addcss_cats = FLEXIUtilities::paramToArray($mu_addcss_cats);
     $mu_addcss_acclvl = $params->get('mu_addcss_acclvl', array('needed_acc', 'obtained_acc'));
     $mu_addcss_acclvl = FLEXIUtilities::paramToArray($mu_addcss_acclvl);
     $mu_addcss_radded = $params->get('mu_addcss_radded', 0);
     $mu_addcss_rupdated = $params->get('mu_addcss_rupdated', 0);
     // Calculate addcss flags
     $add_featured_cats = in_array('featured', $mu_addcss_cats);
     $add_other_cats = in_array('other', $mu_addcss_cats);
     $add_no_acc = in_array('no_acc', $mu_addcss_acclvl);
     $add_free_acc = in_array('free_acc', $mu_addcss_acclvl);
     $add_needed_acc = in_array('needed_acc', $mu_addcss_acclvl);
     $add_obtained_acc = in_array('obtained_acc', $mu_addcss_acclvl);
     // Get addtext parameters
     $mu_addtext_cats = $params->get('mu_addtext_cats', 1);
     $mu_addtext_acclvl = $params->get('mu_addtext_acclvl', array('no_acc', 'free_acc', 'needed_acc', 'obtained_acc'));
     $mu_addtext_acclvl = FLEXIUtilities::paramToArray($mu_addtext_acclvl);
     $mu_addtext_radded = $params->get('mu_addtext_radded', 1);
     $mu_addtext_rupdated = $params->get('mu_addtext_rupdated', 1);
     // Calculate addtext flags
     $add_txt_no_acc = in_array('no_acc', $mu_addtext_acclvl);
     $add_txt_free_acc = in_array('free_acc', $mu_addtext_acclvl);
     $add_txt_needed_acc = in_array('needed_acc', $mu_addtext_acclvl);
     $add_txt_obtained_acc = in_array('obtained_acc', $mu_addtext_acclvl);
     $mu_add_condition_obtainded_acc = $params->get('mu_add_condition_obtainded_acc', 1);
     $mu_no_acc_text = JText::_($params->get('mu_no_acc_text', 'FLEXI_MU_NO_ACC'));
     $mu_free_acc_text = JText::_($params->get('mu_free_acc_text', 'FLEXI_MU_NO_ACC'));
     // *******************************
     // Prepare data needed for markups
     // *******************************
     // a. Get Featured categories and language filter their titles
     $featured_cats_parent = $params->get('featured_cats_parent', 0);
     $disabled_cats = $params->get('featured_cats_parent_disable', 1) ? array($featured_cats_parent) : array();
     $featured_cats = array();
     if ($add_featured_cats && $featured_cats_parent) {
         $where[] = isset($globalcats[$featured_cats_parent]) ? 'id IN (' . $globalcats[$featured_cats_parent]->descendants . ')' : 'parent_id = ' . $featured_cats_parent;
         if (!empty($disabled_cats)) {
             $where[] = 'id NOT IN (' . implode(", ", $disabled_cats) . ')';
         }
         // optionally exclude category root of featured subtree
         $query = 'SELECT c.id' . ' FROM #__categories AS c' . (count($where) ? ' WHERE ' . implode(' AND ', $where) : '');
         $db->setQuery($query);
         $featured_cats = $db->loadColumn();
         $featured_cats = $featured_cats ? array_flip($featured_cats) : array();
         foreach ($featured_cats as $featured_cat => $i) {
             $featured_cats_titles[$featured_cat] = JText::_($globalcats[$featured_cat]->title);
         }
     }
     // b. Get Access Level names (language filter them)
     if ($add_needed_acc || $add_obtained_acc) {
         $access_names = flexicontent_db::getAccessNames();
     }
     // c. Calculate creation time intervals
     if ($mu_addcss_radded) {
         $nowdate_secs = time();
         $ra_timeframes = trim($params->get('mu_ra_timeframe_intervals', '24h,2d,7d,1m,3m,1y,3y'));
         $ra_timeframes = preg_split("/\\s*,\\s*/u", $ra_timeframes);
         $ra_names = trim($params->get('mu_ra_timeframe_names', 'FLEXI_24H_RA , FLEXI_2D_RA , FLEXI_7D_RA , FLEXI_1M_RA , FLEXI_3M_RA , FLEXI_1Y_RA , FLEXI_3Y_RA'));
         $ra_names = preg_split("/\\s*,\\s*/u", $ra_names);
         $unit_hour_map = array('h' => 1, 'd' => 24, 'm' => 24 * 30, 'y' => 24 * 365);
         $unit_word_map = array('h' => 'hours', 'd' => 'days', 'm' => 'months', 'y' => 'years');
         $unit_text_map = array('h' => 'FLEXI_MU_HOURS', 'd' => 'FLEXI_MU_DAYS', 'm' => 'FLEXI_MU_MONTHS', 'y' => 'FLEXI_MU_YEARS');
         foreach ($ra_timeframes as $i => $timeframe) {
             $unit = substr($timeframe, -1);
             if (!isset($unit_hour_map[$unit])) {
                 echo "Improper timeframe ': " . $timeframe . "' for recently added content, please fix in configuration";
                 continue;
             }
             $timeframe = (int) $timeframe;
             $ra_css_classes[$i] = '_item_added_within_' . $timeframe . $unit_word_map[$unit];
             $ra_timeframe_secs[$i] = $timeframe * $unit_hour_map[$unit] * 3600;
             $ra_timeframe_text[$i] = @$ra_names[$i] ? JText::_($ra_names[$i]) : JText::_('FLEXI_MU_ADDED') . JText::sprintf($unit_text_map[$unit], $timeframe);
         }
     }
     // d. Calculate updated time intervals
     if ($mu_addcss_rupdated) {
         $nowdate_secs = time();
         $ru_timeframes = trim($params->get('mu_ru_timeframe_intervals', '24h,2d,7d,1m,3m,1y,3y'));
         $ru_timeframes = preg_split("/\\s*,\\s*/u", $ru_timeframes);
         $ru_names = trim($params->get('mu_ru_timeframe_names', 'FLEXI_24H_RU , FLEXI_2D_RU , FLEXI_7D_RU , FLEXI_1M_RU , FLEXI_3M_RU , FLEXI_1Y_RU , FLEXI_3Y_RU'));
         $ru_names = preg_split("/\\s*,\\s*/u", $ru_names);
         $unit_hour_map = array('h' => 1, 'd' => 24, 'm' => 24 * 30, 'y' => 24 * 365);
         $unit_word_map = array('h' => 'hours', 'd' => 'days', 'm' => 'months', 'y' => 'years');
         $unit_text_map = array('h' => 'FLEXI_MU_HOURS', 'd' => 'FLEXI_MU_DAYS', 'm' => 'FLEXI_MU_MONTHS', 'y' => 'FLEXI_MU_YEARS');
         foreach ($ru_timeframes as $i => $timeframe) {
             $unit = substr($timeframe, -1);
             if (!isset($unit_hour_map[$unit])) {
                 echo "Improper timeframe ': " . $timeframe . "' for recently updated content, please fix in configuration";
                 continue;
             }
             $timeframe = (int) $timeframe;
             $ru_css_classes[$i] = '_item_updated_within_' . $timeframe . $unit_word_map[$unit];
             $ru_timeframe_secs[$i] = $timeframe * $unit_hour_map[$unit] * 3600;
             $ru_timeframe_text[$i] = @$ru_names[$i] ? JText::_($ru_names[$i]) : JText::_('FLEXI_MU_UPDATED') . JText::sprintf($unit_text_map[$unit], $timeframe);
         }
     }
     // **********************************
     // Create CSS markup classes per item
     // **********************************
     $public_acclvl = 1;
     foreach ($items as $item) {
         $item->css_markups = array();
         //$item->categories = isset($item->categories) ? $item->categories : array();
         // Category markups
         if ($add_featured_cats || $add_other_cats) {
             foreach ($item->categories as $item_cat) {
                 $is_featured_cat = isset($featured_cats[$item_cat->id]);
                 if ($is_featured_cat && !$add_featured_cats) {
                     continue;
                 }
                 // not adding featured cats
                 if (!$is_featured_cat && !$add_other_cats) {
                     continue;
                 }
                 // not adding other cats
                 if (in_array($item_cat->id, $globalnoroute)) {
                     continue;
                 }
                 // non-linkable/routable 'special' category
                 $item->css_markups['itemcats'][] = '_itemcat_' . $item_cat->id;
                 $item->ecss_markups['itemcats'][] = ($is_featured_cat ? ' mu_featured_cat' : ' mu_normal_cat') . ($mu_addtext_cats ? ' mu_has_text' : '');
                 $item->title_markups['itemcats'][] = $mu_addtext_cats ? $is_featured_cat ? $featured_cats_titles[$item_cat->id] : (isset($globalcats[$item_cat->id]) ? $globalcats[$item_cat->id]->title : '') : '';
             }
         }
         // recently-added Timeframe markups
         if ($mu_addcss_radded) {
             $item_timeframe_secs = $nowdate_secs - strtotime($item->created);
             $mr = -1;
             foreach ($ra_timeframe_secs as $i => $timeframe_secs) {
                 // Check if item creation time has surpassed this time frame
                 if ($item_timeframe_secs > $timeframe_secs) {
                     continue;
                 }
                 // Check if this time frame is more recent than the best one found so far
                 if ($mr != -1 && $timeframe_secs > $ra_timeframe_secs[$mr]) {
                     continue;
                 }
                 // Use current time frame
                 $mr = $i;
             }
             if ($mr >= 0) {
                 $item->css_markups['timeframe'][] = $ra_css_classes[$mr];
                 $item->ecss_markups['timeframe'][] = ' mu_ra_timeframe' . ($mu_addtext_radded ? ' mu_has_text' : '');
                 $item->title_markups['timeframe'][] = $mu_addtext_radded ? $ra_timeframe_text[$mr] : '';
             }
         }
         // recently-updated Timeframe markups
         if ($mu_addcss_rupdated) {
             $item_timeframe_secs = $nowdate_secs - strtotime($item->modified);
             $mr = -1;
             foreach ($ru_timeframe_secs as $i => $timeframe_secs) {
                 // Check if item creation time has surpassed this time frame
                 if ($item_timeframe_secs > $timeframe_secs) {
                     continue;
                 }
                 // Check if this time frame is more recent than the best one found so far
                 if ($mr != -1 && $timeframe_secs > $ru_timeframe_secs[$mr]) {
                     continue;
                 }
                 // Use current time frame
                 $mr = $i;
             }
             if ($mr >= 0) {
                 $item->css_markups['timeframe'][] = $ru_css_classes[$mr];
                 $item->ecss_markups['timeframe'][] = ' mu_ru_timeframe' . ($mu_addtext_rupdated ? ' mu_has_text' : '');
                 $item->title_markups['timeframe'][] = $mu_addtext_rupdated ? $ru_timeframe_text[$mr] : '';
             }
         }
         // Get item's access levels if this is needed
         if ($add_free_acc || $add_needed_acc || $add_obtained_acc) {
             $all_acc_lvls = array();
             $all_acc_lvls[] = $item->access;
             $all_acc_lvls[] = $item->category_access;
             $all_acc_lvls[] = $item->type_access;
             $all_acc_lvls = array_unique($all_acc_lvls);
         }
         // No access markup
         if ($add_no_acc && !$item->has_access) {
             $item->css_markups['access'][] = '_item_no_access';
             $item->ecss_markups['access'][] = $add_txt_no_acc ? ' mu_has_text' : '';
             $item->title_markups['access'][] = $add_txt_no_acc ? $mu_no_acc_text : '';
         }
         // Free access markup, Add ONLY if item has a single access level the public one ...
         if ($add_free_acc && $item->has_access && count($all_acc_lvls) == 1 && $public_acclvl == reset($all_acc_lvls)) {
             $item->css_markups['access'][] = '_item_free_access';
             $item->ecss_markups['access'][] = $add_txt_free_acc ? ' mu_has_text' : '';
             $item->title_markups['access'][] = $add_txt_free_acc ? $mu_free_acc_text : '';
         }
         // Needed / Obtained access levels markups
         if ($add_needed_acc || $add_obtained_acc) {
             foreach ($all_acc_lvls as $all_acc_lvl) {
                 if ($public_acclvl == $all_acc_lvl) {
                     continue;
                 }
                 // handled separately above
                 $has_acclvl = in_array($all_acc_lvl, $aids);
                 if (!$has_acclvl) {
                     if (!$add_needed_acc) {
                         continue;
                     }
                     // not adding needed levels
                     $item->css_markups['access'][] = '_acclvl_' . $all_acc_lvl;
                     $item->ecss_markups['access'][] = ' mu_needed_acclvl' . ($add_txt_needed_acc ? ' mu_has_text' : '');
                     $item->title_markups['access'][] = $add_txt_needed_acc ? $access_names[$all_acc_lvl] : '';
                 } else {
                     if (!$add_obtained_acc) {
                         continue;
                     }
                     // not adding obtained levels
                     if ($mu_add_condition_obtainded_acc == 0 && !$item->has_access) {
                         continue;
                     }
                     // do not add obtained level markups if item is inaccessible
                     $item->css_markups['access'][] = '_acclvl_' . $all_acc_lvl;
                     $item->ecss_markups['access'][] = ' mu_obtained_acclvl' . ($add_txt_obtained_acc ? ' mu_has_text' : '');
                     $item->title_markups['access'][] = $add_txt_obtained_acc ? $access_names[$all_acc_lvl] : '';
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public function onDisplayFieldValue(&$field, $item, $values = null, $prop = 'display')
 {
     if (!in_array($field->field_type, self::$field_types)) {
         return;
     }
     static $all_langs = null;
     static $cat_links = array();
     static $acclvl_names = null;
     $remove_space = $field->parameters->get('remove_space', 0);
     $pretext = FlexicontentFields::replaceFieldValue($field, $item, $field->parameters->get('pretext', ''), 'pretext');
     $posttext = FlexicontentFields::replaceFieldValue($field, $item, $field->parameters->get('posttext', ''), 'posttext');
     $separatorf = $field->parameters->get('separatorf', 1);
     $opentag = FlexicontentFields::replaceFieldValue($field, $item, $field->parameters->get('opentag', ''), 'opentag');
     $closetag = FlexicontentFields::replaceFieldValue($field, $item, $field->parameters->get('closetag', ''), 'closetag');
     // Microdata (classify the field values for search engines)
     $itemprop = $field->parameters->get('microdata_itemprop');
     if ($pretext) {
         $pretext = $remove_space ? $pretext : $pretext . ' ';
     }
     if ($posttext) {
         $posttext = $remove_space ? $posttext : ' ' . $posttext;
     }
     switch ($separatorf) {
         case 0:
             $separatorf = ' ';
             break;
         case 1:
             $separatorf = '<br />';
             break;
         case 2:
             $separatorf = '&nbsp;|&nbsp;';
             break;
         case 3:
             $separatorf = ',&nbsp;';
             break;
         case 4:
             $separatorf = $closetag . $opentag;
             break;
         case 5:
             $separatorf = '';
             break;
         default:
             $separatorf = '&nbsp;';
             break;
     }
     $props_type = $field->parameters->get('props_type');
     switch ($props_type) {
         case 'language':
             if ($all_langs === null) {
                 $all_langs = FLEXIUtilities::getLanguages($hash = 'code');
             }
             $lang_data = $all_langs->{$item->language};
             $field->{$prop} = @$lang_data->title_native ? $lang_data->title_native : $lang_data->name;
             break;
         case 'alias':
             $field->{$prop} = $item->{$props_type};
             break;
         case 'category':
             $link_maincat = $field->parameters->get('link_maincat', 1);
             if ($link_maincat) {
                 $maincatid = isset($item->maincatid) ? $item->maincatid : $item->catid;
                 // maincatid is used by item view
                 if (!isset($cat_links[$maincatid])) {
                     $maincat_slug = $item->maincatid ? $item->maincatid . ':' . $item->maincat_alias : $item->catid;
                     $cat_links[$maincatid] = JRoute::_(FlexicontentHelperRoute::getCategoryRoute($maincat_slug));
                 }
             }
             $maincat_title = !empty($item->maincat_title) ? $item->maincat_title : 'catid: ' . $item->catid;
             $field->{$prop} = $link_maincat ? '<a class="fc_coreprop fc_maincat link_' . $field->name . '" href="' . $cat_links[$maincatid] . '">' . $maincat_title . '</a>' : $maincat_title;
             break;
         case 'access':
             if ($acclvl_names === null) {
                 $acclvl_names = flexicontent_db::getAccessNames();
             }
             $field->{$prop} = isset($acclvl_names[$item->access]) ? $acclvl_names[$item->access] : 'unknown access level id: ' . $item->access;
             break;
         default:
             $field->{$prop} = $props_type;
             break;
     }
     if (strlen($field->{$prop})) {
         $field->{$prop} = $opentag . $pretext . $field->{$prop} . $posttext . $closetag;
     }
 }