static function getRelation(RelationParamsClass $relationParams, DBQueryParamsClass $QBQueryPrams = null) { if (empty($QBQueryPrams)) { $QBQueryPrams = new DBQueryParamsClass(); } $relationClass = $relationParams->getRightClass(); $relationObj = new $relationClass(); $relationTable = $relationObj->tableName(); // $sql = "SELECT a.* FROM ".$relationTable." a, cat_relations b WHERE b.leftClass='".$relationParams->getLeftClass()."' AND b.rightClass='".$relationParams->getRightClass()."' AND b.leftId='".$relationParams->getLeftId()."' AND a.id = b.rightId LIMIT ".$QBQueryPrams->getLimit(); $sqlDopWhere = $QBQueryPrams->getConditions() ? " ( " . $QBQueryPrams->getConditions() . " ) AND " : ""; $sqlOrder = $QBQueryPrams->getOrderBy() == "id" ? "a.id" : $QBQueryPrams->getOrderBy(); $result = Yii::app()->db->createCommand()->select("a.*")->from($relationTable . " a, cat_relations b ")->where($sqlDopWhere . " ( b.leftClass='" . $relationParams->getLeftClass() . "' AND b.rightClass='" . $relationParams->getRightClass() . "' AND b.leftId='" . $relationParams->getLeftId() . "' AND a.id = b.rightId )", $QBQueryPrams->getParams())->order($sqlOrder)->queryAll(); $list = array(); if (is_array($result) && sizeof($result) > 0) { $obectClass = $relationParams->getRightClass(); foreach ($result as $arrayValue) { $newObject = new $obectClass(); $newObject->setAttributesFromArray($arrayValue); $list[$newObject->id] = $newObject; } } return $list; }
static function addForm($form, $captcha = false, Controller $controller = null) { $fields = $form->attributeLabels(); $relationList = $form->getRelationFields(); $relations = $form->relations(); $fieldType = $form->fieldType(); $requiredFields = $form->getRequiredAttributes(); $placeholder = $form->attributePlaceholder(); $saveFields = $form->getSafeAtributes(); $cout = ""; $classTable = get_class($form); foreach ($fields as $field => $key) { // Если идет добаление элемента но произошла ошибка то воспроизводит введеные значения $paramValue = !empty($_POST[$classTable][$field]) ? $_POST[$classTable][$field] : ""; if (!empty($paramValue) && !$form->{$field}) { $form->{$field} = $paramValue; } // end // Обязаельное поле if (self::find_in_array($field, $requiredFields)) { $requiredAttr = "required"; } else { $requiredAttr = ""; } // Вытаскиваем тип поля $fieldTypeValue = !empty($fieldType[$field]) ? $fieldTypeValue = $fieldType[$field] : ""; if (!in_array($field, $relationList) && (empty($fieldTypeValue) || empty($relations[$fieldTypeValue]))) { // Подсказка к полю if (!empty($placeholder[$field])) { $fieldPlaceholder = $placeholder[$field]; } else { $fieldPlaceholder = ""; } // нужно проверить если данное поле не указанно в списке SAVE то вывести не поля ввода а просто значение $input = ""; if (CCModelHelper::find_in_array($field, $saveFields) || $field == "password2") { if (!empty($fieldType[$field])) { switch ($fieldType[$field]) { case "url": $input = CHtml::activeUrlField($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")) . "<br/><font class='smallGrey'>формат: http://www.sitename.ru</font>"; break; case "email": $input = CHtml::activeEmailField($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); break; case "date": if ((int) $form->{$field} > 0) { $form->{$field} = date("Y-m-d", (int) $form->{$field}); } $input = CHtml::activeDateField($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); break; case "password": $input = CHtml::passwordField($classTable . "[" . $field . "]", '', array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); break; case "checkbox": $input = CHtml::activeCheckBox($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); break; case "visual_textarea": $input = $fieldPlaceholder . CHtml::activeTextArea($form, $field, array("cols" => 50, "rows" => 10, "class" => "tinymce", "placeholder" => $fieldPlaceholder)); break; case "textarea": $input = CHtml::activeTextArea($form, $field, array("cols" => 50, "rows" => 10, "class" => "mceNoEditor", "placeholder" => $fieldPlaceholder, $requiredAttr => "")); break; case "image": if ($form->{$field}) { $catalog = get_class($form); $input = '<img src="../' . ImageHelper::getImage($form->{$field}, 2, $form) . '" width="100" /><br/>'; if (Yii::app()->controller->module->id == "console") { $input .= '<a href="' . SiteHelper::createUrl("/console/catalog/edit", array("id" => $form->id)) . "?catalog=" . $catalog . "&action=img_del&field=" . $field . '">[<?= Yii::t("user", "Удалить") ?>]</a><br/>'; } $input .= CHtml::hiddenField($catalog . "[old_" . $field . "]", $form->{$field}); } $input .= CHtml::activeFileField($form, $field, array($requiredAttr => "")); break; case "file": if ($form->{$field}) { $catalog = get_class($form); $input = '<a href="../' . $form->{$field} . '" target="_blank">' . $form->{$field} . '</a><br/>'; if (Yii::app()->controller->module->id == "console") { $input .= '<a href="' . SiteHelper::createUrl("/console/catalog/edit", array("id" => $form->id)) . "?catalog=" . $catalog . "&action=img_del&field=" . $field . '">[<?= Yii::t("user", "Удалить") ?>]</a><br/>'; } $input .= CHtml::hiddenField($catalog . "[old_" . $field . "]", $form->{$field}); } $input .= CHtml::activeFileField($form, $field, array($requiredAttr => "")); break; case "label": $input = $form->{$field}; break; default: $input = CHtml::activeTextField($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); } } else { $input = CHtml::activeTextField($form, $field, array("placeholder" => $fieldPlaceholder, $requiredAttr => "")); } } else { $input = $form->{$field}; } } else { $input = ""; // проверяем нет ли привязки данного поля к сязи один ко многим if (!empty($fieldTypeValue) && !empty($relations[$fieldTypeValue])) { $relation = $relations[$fieldTypeValue]; } else { $relation = $form->getRelationByField($field); } // привязка один к одному $relationItems = CCModelHelper::getRelationItems($relation, $form); $fieldName = $field; $paramValue = !empty($_POST[$classTable][$fieldName]) ? (int) $_POST[$classTable][$fieldName] : 0; if (sizeof($relationItems) > 0) { if ($relation[0] == CCModel::HAS_MANY || $relation[0] == CCModel::MANY_MANY) { $relationModel = RelationParamsClass::CreateParams()->setLeftId($form->id)->setLeftClass($classTable)->setRightClass($relation[1]); $listValues = SiteHelper::getRelation($relationModel); $input .= '<div class="relationListItems"> <ul>'; foreach ($relationItems as $relationData) { if (!empty($listValues[$relationData->id])) { $checked = "checked=\"checked\""; } else { $checked = ""; } $input .= '<li><input type="checkbox" name="' . $classTable . '[' . $relation[1] . '][]" ' . $checked . ' value="' . $relationData->id . '" id="' . $fieldName . "_" . $relationData->id . '" /><label for="' . $fieldName . "_" . $relationData->id . '">' . $relationData->name . '</li>'; } $input .= ' </ul> </div>'; } else { if (!empty($paramValue) && !$form->{$fieldName}) { $selectedID = $paramValue; } else { $selectedID = ""; } $input .= '<select name="' . $classTable . '[' . $fieldName . ']" class="field_' . $fieldName . '" ' . $requiredAttr . '> <option value=""> --- --- --- </option>'; $input .= self::getRelationListOptions($form, $fieldName, $relationItems, $selectedID); $input .= '</select>'; } } } if (!empty($input)) { if (self::find_in_array($field, $requiredFields)) { $required = " <font class=\"colorRad\">*</font>"; } else { $required = ""; } $cout .= '<tr> <th>' . $fields[$field] . $required . ':</th> <td>' . $input . '</td> </tr>'; } } if (!empty($captcha)) { $cout .= '<tr> <th width="150"> ' . Yii::t("page", "Проверочный код") . ' <font class="redColor">*</font></th> <td id="captchaTD">'; $cout .= $controller->widget('CCaptcha', array('buttonLabel' => '[ ' . Yii::t("page", "обновить") . ' ]'), true); $cout .= "<div id='CCaptchaInput'>" . CHtml::activeTextField($form, 'captcha', array('class' => 'validate[required]', "required" => "")) . "</div>"; $cout .= ' </td> </tr>'; } return $cout; }