public static function drawCol($item, $colName, $params = [], $dataManager = null, $originalCol = '', $originalItem = null) { $modelName = get_class($item); if (!class_exists($modelName)) { return false; } if (!$originalCol) { $originalCol = $colName; } if (!$originalItem) { $originalItem = $item; } $relations = $modelName::relations(); if (strpos($colName, ':') !== false && !empty($relations[substr($colName, 0, strpos($colName, ':'))])) { $rel = substr($colName, 0, strpos($colName, ':')); $col = substr($colName, strpos($colName, ':') + 1); if ($item->{$rel}) { return DataManager::drawCol($item->{$rel}, $col, $params, $dataManager, $originalCol, $originalItem); } else { return 'Не указано'; } } if (!empty($modelName::$cols[$colName]['relation'])) { $type = !empty($relations[$modelName::$cols[$colName]['relation']]['type']) ? $relations[$modelName::$cols[$colName]['relation']]['type'] : 'to'; switch ($type) { case 'relModel': $managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); $count = $count ? $count : 'Нет'; return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count}</a>"; case 'many': $managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); $count = $count ? $count : 'Нет'; return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count}</a>"; default: if ($item->{$modelName::$cols[$colName]['relation']}) { if (\App::$cur->name == 'admin') { $href = "<a href ='/admin/" . str_replace('\\', '/view/', $relations[$modelName::$cols[$colName]['relation']]['model']) . "/" . $item->{$modelName::$cols[$colName]['relation']}->pk() . "'>"; if (!empty($modelName::$cols[$colName]['showCol'])) { $href .= $item->{$modelName::$cols[$colName]['relation']}->{$modelName::$cols[$colName]['showCol']}; } else { $href .= $item->{$modelName::$cols[$colName]['relation']}->name(); } $href .= '</a>'; return $href; } else { return $item->{$modelName::$cols[$colName]['relation']}->name(); } } else { return $item->{$colName}; } } } else { if (!empty($modelName::$cols[$colName]['view']['type'])) { switch ($modelName::$cols[$colName]['view']['type']) { case 'widget': ob_start(); \App::$cur->view->widget($modelName::$cols[$colName]['view']['widget'], ['item' => $item, 'colName' => $colName, 'colParams' => $modelName::$cols[$colName]]); $content = ob_get_contents(); ob_end_clean(); return $content; case 'moduleMethod': return \App::$cur->{$modelName::$cols[$colName]['view']['module']}->{$modelName::$cols[$colName]['view']['method']}($item, $colName, $modelName::$cols[$colName]); case 'many': $managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count} " . \Tools::getNumEnding($count, ['Элемент', 'Элемента', 'Элементов']) . "</a>"; default: return $item->{$colName}; } } elseif (!empty($modelName::$cols[$colName]['type'])) { if (\App::$cur->name == 'admin' && $originalCol == 'name' || $dataManager && !empty($dataManager->managerOptions['colToView']) && $dataManager->managerOptions['colToView'] == $originalCol) { $formName = $dataManager && !empty($dataManager->managerOptions['editForm']) ? $dataManager->managerOptions['editForm'] : 'manager'; $redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem)); return "<a href ='/admin/" . str_replace('\\', '/view/', get_class($originalItem)) . "/{$originalItem->id}?formName={$formName}&redirectUrl={$redirectUrl}'>{$item->{$colName}}</a>"; } elseif (\App::$cur->name == 'admin' && $colName == 'name') { $redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem)); return "<a href ='/admin/" . str_replace('\\', '/view/', get_class($item)) . "/{$item->id}?redirectUrl={$redirectUrl}'>{$item->{$colName}}</a>"; } else { return \Model::resloveTypeValue($item, $colName); } } else { return $item->{$colName}; } } }
/** * Retrn col value with col params and relations path * * @param Model $object * @param string $valuePath * @param boolean $convert * @param boolean $manageHref * @return string */ public static function getColValue($object, $valuePath, $convert = false, $manageHref = false) { if (strpos($valuePath, ':')) { $rel = substr($valuePath, 0, strpos($valuePath, ':')); $param = substr($valuePath, strpos($valuePath, ':') + 1); if (!$object->{$rel}) { $modelName = get_class($object); $relations = $modelName::relations(); if (empty($relations[$rel]['type']) || $relations[$rel]['type'] == 'one') { return $object->{$relations[$rel]['col']}; } return 0; } if (strpos($valuePath, ':')) { return self::getColValue($object->{$rel}, $param, $convert, $manageHref); } else { return $convert ? Model::resloveTypeValue($object->{$rel}, $param, $manageHref) : $object->{$rel}->{$param}; } } else { return $convert ? Model::resloveTypeValue($object, $valuePath, $manageHref) : $object->{$valuePath}; } }