public static function addAssociatedDocuments(&$docs, $type, $is_main, $data_fields = array(), $i18n_fields = array()) { if (count($docs) == 0 || empty($data_fields) && empty($i18n_fields)) { return array(); } $modules = c2cTools::Type2Modules($type); $main_module = $modules['main']; $linked_module = $modules['linked']; if (empty($main_module) || empty($linked_module)) { return array(); } $doc_ids = array(); foreach (array_keys($docs) as $key) { $id = $docs[$key]['id']; $doc_ids[] = $id; $docs[$id] = $docs[$key]; unset($docs[$key]); } // retrieve associations if ($main_module == $linked_module) { $associations = Association::countAll($doc_ids, $type); $module = $main_module; } elseif ($is_main) { $associations = Association::countAllLinked($doc_ids, $type); $module = $linked_module; } else { $associations = Association::countAllMain($doc_ids, $type); $module = $main_module; } if (count($associations) == 0) { return array(); } $linked_ids = array(); foreach ($associations as $assoc) { if ($is_main) { $doc_id = $assoc['main_id']; $linked_id = $assoc['linked_id']; } else { $doc_id = $assoc['linked_id']; $linked_id = $assoc['main_id']; } $linked_ids[] = $linked_id; if (isset($docs[$doc_id]['linked_docs'])) { $docs[$doc_id]['linked_docs'] = array_merge($docs[$doc_id]['linked_docs'], array($linked_id)); } else { $docs[$doc_id]['linked_docs'] = array($linked_id); } } $linked_ids = array_unique($linked_ids); $conditions = array(); foreach ($linked_ids as $id) { $conditions[] = '?'; } // retrieve info on the linked docs (name etc) $model = c2cTools::module2model($module); $q = Doctrine_Query::create()->from("{$model} m")->where('m.id IN ( ' . implode(', ', $conditions) . ' )', $linked_ids); if (!in_array('id', $data_fields)) { $data_fields[] = 'id'; } $q->addSelect('m.' . implode(', m.', $data_fields)); $has_name = false; if (!empty($i18n_fields)) { if (in_array('name', $i18n_fields)) { if (!in_array('search_name', $i18n_fields)) { $i18n_fields[] = 'search_name'; $i18n_fields[] = 'culture'; } $has_name = true; } $q->addSelect('mi.' . implode(', mi.', $i18n_fields))->leftJoin('m.' . $model . 'I18n mi'); } $linked_docs_info = $q->execute(array(), Doctrine::FETCH_ARRAY); if ($has_name) { $user_prefered_langs = sfContext::getInstance()->getUser()->getCulturesForDocuments(); $linked_docs_info = Language::getTheBest($linked_docs_info, $model); } // add linked docs info into $docs foreach ($docs as $doc_id => $doc) { if (isset($doc['linked_docs'])) { foreach ($doc['linked_docs'] as $key => $linked_doc_id) { $docs[$doc_id]['linked_docs'][$key] = $linked_docs_info[$linked_doc_id]; } } } }