/** * สร้าง record(document) ของ document ใหม่ จากข้อมูลที่ระบุ * @param int $entity * @param string $refId * @param string $documentPath path ของไฟล์ต้นทาง * @param string $mode รูปแบบการ move file [upload, cp] * @return void|Document */ public function newItem($entity, $refId, $documentPath, $documentName, $mode = 'upload') { if (!file_exists($documentPath)) { return; } $model = Entity::getInstance($entity, $refId); if ($model == null) { return; } $itemNo = \yii::$app->db->createCommand("select max(itemNo) from Document\n \t\t\twhere type=:type and refId=:id", ['type' => $entity, 'id' => $refId])->queryScalar(); $itemNo++; $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension $mime = finfo_file($finfo, $documentPath); finfo_close($finfo); $document = new Document(); $document->type = $entity; $document->refId = $refId; $document->itemNo = $itemNo; $document->status = Workflow::STATUS_PUBLISHED; $document->mime = $mime; $document->fileName = $documentName; $arr = explode(".", $documentName); $extension = $arr[count($arr) - 1]; $document->srcPath = date('Y/m/d', strtotime($model->createTime)) . "/{$refId}_{$itemNo}.{$extension}"; $retries = 0; do { try { if ($document->save()) { break; } } catch (Exception $ex) { $document->itemNo++; $document->srcPath = date('Y/m/d', strtotime($model->createTime)) . "/{$refId}_{$document->itemNo}.{$extension}"; $retries++; usleep(1000); } } while ($retries < 10); if ($retries >= 10) { $document = null; } if (!empty($document)) { $destination = $document->getFullPath(); if (!is_readable(dirname($destination))) { mkdir(dirname($destination), 0755, true); } if ($mode == 'cp') { copy($documentPath, $destination); } elseif ($mode == 'mv') { rename($documentPath, $destination); } else { move_uploaded_file($documentPath, $destination); } } return $document; }
public function getPreview($options = array()) { if (empty($this->previewEntity)) { $model = new Media(); $media = $model->findByParams(array(Media::ENCODE_ENTITY => Entity::TYPE_WIDGET, Media::ENCODE_ID => $this->id, Media::ENCODE_ITEMNO => $this->previewRefId)); if ($media == null) { return null; } $imgTag = Html::img(\Yii::getAlias('@web') . '/media/' . $media->getPublishUri($options), array()); return $imgTag; } else { $model = Entity::getInstance($this->previewEntity, $this->previewRefId); if ($model != null) { return $model->getPreview($options); } } }
private static function _entityDecoder($attributes) { /* check social facebook instagram twitter */ $itemNo = NULL; switch ($attributes['type']) { case 'document': $itemNo = $attributes['itemno']; $entityType = Entity::mapType($attributes['type']); $entity = Document::findOne(['type' => 5, 'refId' => $attributes['id'], 'itemNo' => $itemNo]); break; default: $entityType = Entity::mapType($attributes['type']); $entity = Entity::getInstance($entityType, $attributes['id']); break; } if (empty($entity)) { return; } switch ($attributes['type']) { case 'document': $arr = explode(".", $entity->srcPath); $extension = $arr[count($arr) - 1]; $thumbnail = $extension . ".png"; $thumbnailUrl = Url::toRoute(['/global/img/' . $thumbnail]); $str = "<entity type=\"{$attributes['type']}\" data-itemno=\"{$attributes['itemno']}\" data-id=\"{$attributes['id']}\">"; $str .= '<img src="' . $thumbnailUrl . '" alt="" width="30" height="30">'; break; default: $str = "<entity type=\"{$attributes['type']}\" data-id=\"{$attributes['id']}\">" . $entity->getPreview(array(Media::ENCODE_WIDTH => 100)); break; } switch ($entityType) { case Entity::TYPE_PERSON: $caption = $entity->getDisplayName(); break; case Entity::TYPE_DOCUMENT: $caption = $entity->caption; break; default: $caption = $entity->title; } $str .= "<p class=\"caption\">" . Entity::$arrTitle[$entityType] . ": {$caption}</p></entity>"; return $str; }
private static function _entityDecoder($attributes, $matches) { if (isset($attributes['type'])) { switch ($attributes['type']) { case 'document': $entityType = Entity::mapType($attributes['type']); $itemNo = $attributes['data-itemno']; $entityType = Entity::mapType($attributes['type']); $entity = Document::findOne(['type' => 5, 'refId' => $attributes['data-id'], 'itemNo' => $itemNo]); break; default: $entityType = Entity::mapType($attributes['type']); $entity = Entity::getInstance($entityType, $attributes['data-id']); break; } } if (empty($entity)) { return; } switch ($entityType) { case Entity::TYPE_DOCUMENT: $result = self::_documentReplace($entity, $attributes); break; case Entity::TYPE_GALLERY: $result = self::_galleryReplace($entity, $attributes); break; case Entity::TYPE_FEED: $result = self::_feedReplace($entity, $attributes); break; case Entity::TYPE_QUOTE: $result = self::_quoteReplace($entity, $attributes); break; default: $result = $entity->getPreview(array(Media::ENCODE_WIDTH => 532)); break; } return $result; }
public function actionView() { $request = \Yii::$app->request; $enc = new TrEnc(\Yii::$app->params['crypto'][0][0], \Yii::$app->params['crypto'][0][1]); $encrypted = substr($request->get('key'), 0, strrpos($request->get('key'), '.')); $params = $enc->decode($encrypted); $flag = $request->get('falg', ''); if ($flag) { switch ($flag) { case '@': $this->displayParams($params); break; } return; } $model = new Document(); $document = $model->findByParams($params); // check for view permission if ($document == null) { throw new HttpException(404); } $entity = Entity::getInstance($document->type, $document->refId); if ($entity == null) { throw new HttpException(404); } $filePath = \Yii::$app->params['document.basePath'] . Entity::$arrType[$document->type] . '/' . $document->srcPath; if (!is_readable($filePath)) { $filePath = \Yii::$app->params['document.basePath2'] . Entity::$arrType[$document->type] . '/' . $document->srcPath; } if (!file_exists($filePath)) { throw new HttpException(404); } $response = \Yii::$app->getResponse(); $response->headers->set('Content-Type', $document->mime); $response->format = \yii\web\Response::FORMAT_RAW; echo file_get_contents($filePath); }