public function post($print_response = true) { //if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') { // return $this->delete($print_response); //} // validate create action access // if (!Yii::app()->user->checkAccess($this->options['model_name'] . '.create')) { // throw new CHttpException(403, Yii::t("D2filesModule.model","You are not authorized to perform this action.")); // } D2files::extendedCheckAccess($this->options['model_name'] . '.uploadD2File'); $upload = isset($_FILES[$this->options['param_name']]) ? $_FILES[$this->options['param_name']] : null; // Parse the Content-Disposition header, if available: $file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ? rawurldecode(preg_replace('/(^[^"]+")|("$)/', '', $_SERVER['HTTP_CONTENT_DISPOSITION'])) : null; $file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ? $_SERVER['HTTP_CONTENT_DESCRIPTION'] : null; // Parse the Content-Range header, which has the following form: // Content-Range: bytes 0-524287/2000000 $content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ? preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null; $size = $content_range ? $content_range[3] : null; $info = array(); if ($upload && is_array($upload['tmp_name'])) { // param_name is an array identifier like "files[]", // $_FILES is a multi-dimensional array: foreach ($upload['tmp_name'] as $index => $value) { $sFileName = $upload['name'][$index]; //save to DB $nFileId = $this->saveToDb($sFileName, $this->options['model_name'], $this->options['model_id']); // save file $save_file_name = self::createSaveFileName($nFileId, $sFileName); $info[] = $this->handle_file_upload($upload['tmp_name'][$index], $save_file_name, $size ? $size : $upload['size'][$index], 'dat', $upload['error'][$index], $index, $content_range); if (!empty($info[count($info) - 1]->error)) { $this->deleteFile($nFileId, 'Error: ' . $info[count($info) - 1]->error); } else { $info[count($info) - 1]->name = $sFileName; $info[count($info) - 1]->id = $nFileId; } } } else { // param_name is a single object identifier like "file", // $_FILES is a one-dimensional array: $file_name = $file_name ? $file_name : (isset($upload['name']) ? $upload['name'] : null); $sFileName = $file_name; $nFileId = $this->saveToDb($sFileName, $this->options['model_name'], $this->options['model_id']); $save_file_name = self::createSaveFileName($nFileId, $sFileName); $info[] = $this->handle_file_upload(isset($upload['tmp_name']) ? $upload['tmp_name'] : null, $file_name, $size ? $size : (isset($upload['size']) ? $upload['size'] : $_SERVER['CONTENT_LENGTH']), $file_type ? $file_type : (isset($upload['type']) ? $upload['type'] : $_SERVER['CONTENT_TYPE']), isset($upload['error']) ? $upload['error'] : null, null, $content_range); if (!empty($info[count($info) - 1]->error)) { $this->deleteFile($nFileId, 'Error: ' . $info[count($info) - 1]->error); } else { $info[count($info) - 1]->name = $sFileName; $info[count($info) - 1]->id = $nFileId; } } return $this->generate_response($info, $print_response); }
private function registerClientScripts() { $baseUrl = Yii::app()->baseUrl; //blueimp/jQuery-File-Upload scripts $assetsPath = Yii::getPathOfAlias('vendor.blueimp.jquery-file-upload'); $cs = Yii::app()->getClientScript(); $am = Yii::app()->assetManager; $cs->registerScriptFile($am->publish($assetsPath . '/js/vendor/jquery.ui.widget.js')); $cs->registerScriptFile($am->publish($assetsPath . '/js/jquery.iframe-transport.js')); $cs->registerScriptFile($am->publish($assetsPath . '/js/jquery.fileupload.js')); //page scripts $file_upload_ajax_url = $this->controler->createUrl('upload', array('model_name' => $this->model_name, 'model_id' => $this->model_id)); $file_delete_ajax_url = ''; //if (Yii::app()->user->checkAccess($this->model_name . '.delete')) { if (!$this->readOnly && D2files::extendedCheckAccess($this->model_name . '.deleteD2File', FALSE)) { $file_delete_ajax_url = '+ \'<a href="' . $this->controler->createUrl('deleteFile') . '&id=\'+file.id+\'" rel="tooltip" title="' . Yii::t("D2filesModule.crud_static", "Delete") . '" class="delete" data-toggle="tooltip"><i class="icon-trash"></i></a> \''; } $file_download_ajax_url = $this->controler->createUrl('downloadFile'); $file_editable_url = $this->controler->createUrl('/d2files/d2files/editableSaver'); $comments_row = ''; if (!$this->readOnly && D2files::extendedCheckAccess($this->model_name . '.uploadD2File', false)) { if (!empty($this->files_types)) { $comments_row .= '<tr id="d2cmnt-\'+file.id+\'"><td colspan="3">'; } else { $comments_row .= '<tr id="d2cmnt-\'+file.id+\'"><td colspan="2">'; } $comments_row .= '<a class="notes_editable" href="#" rel="D2files_notes_\'+file.id+\'" data-pk="\'+file.id+\'"></a>'; $comments_row .= '</td></tr>'; } if (!$this->readOnly) { $file_type_js = ''; $file_type_editable = ''; if (!empty($this->files_types)) { $file_type_js = '+ \'<td class="file-type"><a class="type_editable" href="#" rel="D2files_type_id_\'+file.id+\'" data-pk="\'+file.id+\'"></a></td>\''; $t_listData = array(); foreach ($this->files_types as $key => $item) { $t_listData[] = "{'value':" . $key . ",'text':'" . Yii::t('d2files', $item) . "'}"; } $s_listData = implode(',', $t_listData); $file_type_editable = ' $(\'a.type_editable\').editable({ \'name\':\'type_id\', \'title\':\'' . Yii::t("editable.editable", "Select") . ' ' . Yii::t("D2filesModule.model", "Type") . '\', \'url\':\'' . $file_editable_url . '\', \'type\':\'select\', \'emptytext\':\'' . Yii::t("editable.editable", "Empty") . '\', \'params\':{\'scenario\':\'update\'}, \'source\':[' . $s_listData . '] }); '; } Yii::app()->clientScript->registerScript('for_fileupload_' . $this->getId(), ' $("#fileupload_' . $this->getId() . '").hide(); $("#fileupload_' . $this->getId() . '").fileupload({ dataType: "json", url : "' . $file_upload_ajax_url . '", dropZone : "#dropZone_' . $this->getId() . '", done: function (e, data) { $.each(data.result, function (index, file) { if (file.error != undefined && file.error != "") { alert(file.error); return; } var sRow = \'<tr id="d2file-\'+file.id+\'">\' + \'<td>\' + \'<a href="' . $file_download_ajax_url . '&id=\'+file.id+\'" rel="tooltip" title="' . Yii::t("D2filesModule.crud_static", "Download") . '" class="download" data-toggle="tooltip">\' + \'<i class="icon-file-text blue"></i> \' + file.name + \'</a></td>\' ' . $file_type_js . ' + \'<td class="button-column">\' ' . $file_delete_ajax_url . ' + \'</td>\' + \'</tr>\' + \'' . $comments_row . '\' ; if ($("#attachment_list_' . $this->getId() . ' tr").length > 0) { $("#attachment_list_' . $this->getId() . ' tr:last").after(sRow); } else { $("#attachment_list_' . $this->getId() . '").append(sRow); } }); ' . $file_type_editable . ' $(\'a.notes_editable\').editable({ \'name\':\'notes\', \'title\':\'' . Yii::t("D2filesModule.editable", "Enter") . ' ' . Yii::t("D2filesModule.model", "Notes") . '\', \'url\':\'' . $file_editable_url . '\', \'type\':\'textarea\', \'placement\':\'right\', \'emptytext\':\'' . Yii::t("D2filesModule.crud_static", "Add comment") . '\', \'params\':{\'scenario\':\'update\'} }); } }); $("#attachment_list_' . $this->getId() . '").on( "click", "a.delete", function() { if (!confirm("' . Yii::t("D2filesModule.crud", "Do you want to delete this item?") . '")) { return false; } var elTr = $(this).parent().parent(); var cmtTr = $("#d2cmnt-" + elTr.attr("id").split("-")[1]); $.ajax({ type: "POST", url: $(this).attr("href"), success: function(data){ $(elTr).remove(); $(cmtTr).remove(); } }); return false; // stop the browser following the link }); '); } }
public function actionEditableSaver() { $id = Yii::app()->request->getPost('pk'); if (empty($id)) { throw new CHttpException(404, Yii::t("D2filesModule.model", "The requested record does not exist.")); } $m = D2files::model(); $model = $m->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t("D2filesModule.model", "The requested record does not exist.")); } // validate read access if (!$this->performReadValidation($model->model, $model->model_id)) { throw new CHttpException(403, Yii::t("D2filesModule.model", "You are not authorized to perform this action.")); } // validate upload (editable) action access D2files::extendedCheckAccess($model->model . '.uploadD2File'); $es = new EditableSaver('D2files'); // classname of model to be updated $es->update(); }
if (!empty($files_types_list)) { $file_type_html = ''; if ($readOnly) { $file_type_html = $this->widget('EditableField', array('model' => $mfile, 'type' => 'select', 'attribute' => 'type_id', 'url' => Yii::app()->controller->createUrl('/d2files/d2files/editableSaver'), 'source' => $files_types_list, 'placement' => 'left'), true); } elseif ($mfile->type_id) { $file_type_html = $files_types_list[$mfile->type_id]; } $file_type = '<td class="file-type">' . $file_type_html . '</td>'; } $file_delete_ajax_url = ''; if (!$readOnly && D2files::extendedCheckAccess($model . '.deleteD2File', false)) { $delete_url = $this->controler->createUrl('deleteFile', array('id' => $mfile->id), '&'); $file_delete_ajax_url = '<a href="' . $delete_url . '" rel="tooltip" title="' . Yii::t("D2filesModule.crud_static", "Delete") . '" class="delete" data-toggle="tooltip"><i class="icon-trash"></i></a> '; } $file_download_ajax_url = $this->controler->createUrl('downloadFile', array('id' => $mfile->id), '&'); $sFileListHtml .= '<tr id="d2file-' . $mfile->id . '">' . '<td><a href="' . $file_download_ajax_url . '" rel="tooltip" title="' . Yii::t("D2filesModule.crud_static", "Download") . '" class="download" data-toggle="tooltip"><i class="icon-file-text blue"></i> ' . $mfile->file_name . '</a></td>' . $file_type . '<td class="button-column">' . $file_delete_ajax_url . '</td>' . '</tr>'; if (D2files::extendedCheckAccess($model . '.uploadD2File', false)) { $sFileListHtml .= '<tr id="d2cmnt-' . $mfile->id . '"><td colspan="' . $colspan . '">'; $sFileListHtml .= $this->widget('EditableField', array('model' => $mfile, 'attribute' => 'notes', 'url' => Yii::app()->controller->createUrl('/d2files/d2files/editableSaver'), 'emptytext' => Yii::t("D2filesModule.crud_static", "Add comment"), 'placement' => 'right', 'apply' => !$readOnly), true); $sFileListHtml .= '</td></tr>'; } } } $sFileListHtml .= '</table>'; $file_form = '<form method="post" id="d2FileUploadForm_' . $this->getId() . '" name="DataForm" enctype="multipart/form-data">' . '<input id="fileupload_' . $this->getId() . '" type="file" name="files[]" style="display: none;" multiple /> ' . $sFileListHtml . ' </form>'; if (!$readOnly && D2files::extendedCheckAccess($model . '.uploadD2File', false)) { echo "<tr id=\"dropZone_" . $this->getId() . "\" style=\"border: 3px dashed #ccc;\"><th style=\"vertical-align: middle; width: 220px; padding-left:10px;\"><span class=\"bigger-110 bolder\"><i class=\"icon-cloud-upload grey\"></i> {label}</span></th><td>{value}</td></tr>\n"; } echo "<tr><td colspan=\"2\">" . $file_form . "</td></tr>\n";