ams = array(), $content = '', $only_return_data = false) { if (!$post_id) { global $post; $post_id = $post->ID; } // Get modules data $data = array(); foreach ($module_ids as $module_id) { $module_data = $this->getData($module_id, $post_id, $params); //groups filter if (!empty($params['groups'])) { foreach ($module_data as $key => $d) { if (empty($d['group']) || !in_array($d['group'], $params['groups'])) unset($module_data[$key]); } } // product IDs filter if (!empty($params['products'])) { foreach ($module_data as $key => $d) { if (!in_array($d['unique_id'], $params['products'])) unset($module_data[$key]); } } // hide fields if (!empty($params['hide'])) { foreach ($module_data as $key => $d) { foreach ($params['hide'] as $hide) { if ($hide == 'title') $module_data[$key]['_alt'] = $module_data[$key]['title']; if (isset($d[$hide])) $module_data[$key][$hide] = ''; } } } if ($module_data) $data[$module_id] = $module_data; // shortcoded! if (!isset($params['shortcoded']) || (bool) $params['shortcoded']) Shortcoded::getInstance($post_id)->setShortcodedModule($module_id); } // remove duplicates if (!empty($params['remove_duplicates_by'])) { if ($duplicate_ids = ContentManager::findDuplicateIds($data, $params['remove_duplicates_by'])) { foreach ($data as $module_id => $module_data) { foreach ($module_data as $unique_id => $d) { if (in_array($unique_id, $duplicate_ids)) unset($data[$module_id][$unique_id]); } if (!count($data[$module_id])) unset($data[$module_id]); } } } if (!$data) return; // template if ($params['template'] != 'block_greenshift') { $tpl_manager = BlockTemplateManager::getInstance(); if (empty($params['template']) || !$tpl_manager->isTemplateExists($params['template'])) return; } $template = $params['template']; // next, limit, offset if (!isset($this->block_data_pointer[$post_id])) $this->block_data_pointer[$post_id] = array(); if (!empty($params['next'])) { if (!isset($this->block_data_pointer[$post_id][$template])) $this->block_data_pointer[$post_id][$template] = 0; $data = $this->spliceBlockData($data, $this->block_data_pointer[$post_id][$template], $params['next'], $params['order'], $params['sort']); $count = $this->countBlockData($data); if ($count < $params['next']) $params['next'] = $count; $this->block_data_pointer[$post_id][$template] += $params['next']; } elseif (!empty($params['limit'])) { if (!isset($params['offset'])) $params['offset'] = 0; $data = $this->spliceBlockData($data, $params['offset'], $params['limit'], $params['order'], $params['sort']); $this->block_data_pointer[$post_id][$module_id] = $params['offset'] + $params['limit']; } elseif (!empty($params['order']) || !empty($params['sort'])) $this->spliceBlockData($data, 0, 999999, $params['order'], $params['sort']); if (!$data) return; if (!empty($params['title'])) $title = $params['title']; else $title = ''; if (!empty($params['cols'])) $cols = $params['cols']; else $cols = 0; if ($only_return_data) return $data; return $tpl_manager->render($params['template'], array('data' => $data, 'post_id' => $post_id, 'params' => $params, 'title' => $title, 'cols' => $cols, 'sort' => $params['sort'], 'order' => $params['order'], 'groups' => $params['groups'], 'btn_text' => $params['btn_text'], 'atts' => $params, 'content' => $content)); } private function spliceBlockData($data, $offset, $length, $order = null, $sort = null) { if ($order || $sort) { if (!$sort) $sort = 'price'; if (!$order) $order = 'ask'; if ($sort == 'price' || $sort == 'discount') return $this->spliceBlockDataSorted($data, $offset, $length, $order, $sort); } $results = array(); $count = 0; $results_count = 0; foreach ($data as $module_id => $module_data) { $results[$module_id] = array(); foreach ($module_data as $key => $data) { if ($count < $offset) { $count++; continue; } $results[$module_id][$key] = $data; $count++; $results_count++; if ($results_count >= $length) return $results; } } return $results; } private function spliceBlockDataSorted($data, $offset, $length, $order = 'ask', $sort = 'price') { $all_items = TemplateHelper::sortAllByPrice($data, $order, $sort); $all_items = array_splice($all_items, $offset, $length); $results = array(); foreach ($all_items as $item) { if (!isset($results[$item['module_id']])) $results[$item['module_id']] = array(); $results[$item['module_id']][$item['unique_id']] = $item; } return $results; } private function countBlockData($data) { $count = 0; foreach ($data as $module_id => $module_data) { $count += count($module_data); } return $count; } }