private function saveAttachment($filePath, $extension) { $attachment = new Attachment(); $attachment->attributes = ['path' => $filePath, 'extension' => $extension]; if ($attachment->save()) { return true; } else { throw new Exception("save attachment to db error " . implode(',', $attachment->firstErrors)); } }
public function actionSearch() { $get = Yii::$app->getRequest()->get(); $val = null; if (isset($get['so'])) { $soarr = explode('_', $get['so']); $val = $soarr[1]; } $query = AvRel::find()->where(['status' => AvRel::STATUS_ACTIVE]); $query->andFilterWhere(['category_id' => $get['cate']])->andFilterWhere(['av_id' => $val]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $avs = $query->offset($pages->offset)->limit($pages->limit)->select('goods_id')->asArray()->all(); $avs = ArrayHelper::getColumn($avs, 'goods_id'); $goods = Goods::find()->where(['status' => Goods::STATUS_ACTIVE])->andWhere(['id' => $avs])->asArray()->all(); $covers = ArrayHelper::getColumn($goods, 'cover'); $covers = Attachment::find()->where(['id' => $covers])->indexBy('id')->asArray()->all(); foreach ($goods as &$v) { if (empty($v['cover'])) { continue; } $co = $covers[$v['cover']]; $v['img'] = $co['path'] . '/tiny_' . $co['name']; } return $this->ajaxReturn(['list' => $goods]); }
/** * Finds the Content model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Attachment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Attachment::find()->andWhere(['cid' => $id])->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function getByRes($res_name, $res_id, $type = '', $out = []) { $rel = self::find()->where(['res_name' => $res_name, 'res_id' => $res_id])->asArray()->all(); $list = Attachment::find()->where(['id' => \yii\helpers\ArrayHelper::getColumn($rel, 'attach_id')])->andFilterWhere(['not in', 'id', $out])->asArray()->all(); $type = $type ? $type . '_' : ''; foreach ($list as $k => &$v) { $v['url'] = $v['path'] . '/' . $type . $v['name']; } return $list; }
/** * Lists all Goods models. * @return mixed */ public function actionIndex() { $searchModel = new GoodsSearch(); $data = $searchModel->so(Yii::$app->request->queryParams, 'wx4ezbhcqq'); $covers = ArrayHelper::map($data['list'], 'id', 'cover'); $urls = \common\models\Attachment::find()->where(['id' => $covers])->asArray()->all(); $res = []; foreach ($urls as $k => $v) { $res[$v['id']] = $v['path'] . '/tiny_' . $v['name']; } foreach ($data['list'] as $k => &$v) { $v['img'] = $v['cover'] ? $res[$v['cover']] : ''; } unset($v); return $this->render('index', ['searchModel' => $searchModel, 'data' => $data]); }
public function getAttachedToLink() { if (Attachment::isStorehouse($this->attached_to)) { return 'На складі'; } if (Attachment::isRepaired($this->attached_to)) { return 'В ремонті'; } if (Attachment::isSoldier($this->attached_to)) { $soldier = Soldier::findOne($this->attached_to_soldier); return Html::a(Html::encode("Службовець: {$soldier->nickname} ({$soldier->name})"), ['/soldier/view', 'id' => $soldier->id]); } if (Attachment::isUnit($this->attached_to)) { $unit = Unit::findOne($this->attached_to_unit); $soldier = Soldier::findOne($unit->soldier); return Html::a(Html::encode("Підрозділ: {$unit->name} ({$soldier->nickname}({$soldier->name}))"), ['/unit/view', 'id' => $unit->id]); } return 'undefined'; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Note::find()->complete()->joinWith('attachment')->with(['author', 'ticket']); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['ticket_id' => $this->ticket_id, 'public' => $this->public]); $query->andFilterWhere(['like', 'body', $this->body]); if ($this->attachment !== '') { $attachment = [Attachment::tableName() . '.file_name' => null]; if ($this->attachment) { $attachment = ['not', $attachment]; } $query->andWhere($attachment); } return $dataProvider; }
<?php /* = $form->field($model, 'status', ['options'=>['class'=>'']])->textInput() */ ?> <?php echo $form->field($model, 'condition', ['options' => ['class' => '']])->dropDownList(Condition::getList()); ?> <?php echo $form->field($model, 'provided_by', ['options' => ['class' => '']])->dropDownList($providers); ?> <?php /* = $form->field($model, 'fixed_by', ['options'=>['class'=>'']])->textInput(['maxlength' => true]) */ ?> </div> <div class="form-group col-md-4"> <?php echo $form->field($model, 'attached_to', ['options' => ['class' => '']])->dropDownList(Attachment::getList()); ?> <?php echo $form->field($model, 'attached_to_unit', ['options' => ['class' => '']])->dropDownList($units); ?> <?php echo $form->field($model, 'attached_to_soldier', ['options' => ['class' => '']])->dropDownList($soldiers); ?> </div> <?php /* = $form->field($model, 'photos', ['options'=>['class'=>'form-group col-md-12']])->textInput(['maxlength' => true]) */ ?> <div class="form-group col-md-12"> <?php echo Html::submitButton($model->isNewRecord ? 'Додати' : 'Зберегти', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
/** * Displays an attachment to a Note * @param integer $id * @param bool $download * @throws NotFoundHttpException If Note does not have an attachment * @throws ServerErrorHttpException If unable to access file */ public function actionAttachment($id, $download = false) { /** @var \common\models\Attachment $model */ if (($model = Attachment::findOne($id)) == null) { throw new NotFoundHttpException('The requested page does not exist.'); } $response = Yii::$app->response; $response->format = Response::FORMAT_RAW; $response->headers->set('Content-Type', $model->mimeType); if ($download) { $response->headers->set('Content-Disposition', 'attachment; filename=' . $model->file_name); } if (!is_resource($response->stream = fopen($model->filePath, 'r'))) { throw new ServerErrorHttpException('File access failed: Permission denied'); } $response->send(); }
/** * @return AttachmentQuery */ public function getAttachment() { return $this->hasOne(Attachment::className(), ['note_id' => 'id'])->inverseOf('note'); }
public function getAttach() { return $this->hasOne(Attachment::className(), ['id' => 'cover']); }
public function imgList() { $request = Yii::$app->request; $size = $request->get('size', 20); $start = $request->get('start', 0); $all = \common\models\Attachment::getImgByUserId(Yii::$app->user->id, $start, $size); $urls = []; foreach ($all['list'] as $k => $v) { $url = $v['path'] . '/' . $v['name']; array_push($urls, ['url' => $url]); } return json_encode(array("state" => "SUCCESS", "list" => $urls, "start" => $start, "total" => $all['count'])); }
/** * 上传文件 * @return array */ public function actionUpload() { Yii::$app->response->format = Response::FORMAT_JSON; //error ['err'=>1,'msg'=>'error message'] //success ['err'=>0,'msg'=>'success message','data'=>['id'=>'','name'=>'','url'=>'','isImage'=>'']] $upload = new Upload(['savePath' => Attachment::SAVE_PATH]); if ($upload->checkFileInfoAndSave()) { //保存到数据库 $attachment = new Attachment(); $attachment->title = $upload->originalFileName; $attachment->text = ['name' => Html::encode($upload->originalFileName), 'path' => Yii::getAlias(Attachment::WEB_URL . $upload->saveRelativePath), 'minetype' => $upload->fileMimeType, 'ext' => $upload->fileExt, 'size' => $upload->filesize]; $attachment->save(false); return ['err' => 0, 'msg' => '上传成功', 'data' => ['id' => $attachment->cid, 'name' => Html::encode($upload->originalFileName), 'url' => Yii::getAlias(Attachment::WEB_URL . $upload->saveRelativePath), 'isImage' => in_array($upload->fileMimeType, Attachment::$imageMineType)]]; } else { return ['err' => 1, 'msg' => $upload->error]; } }