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 }); '); } }
/** * save file info in DB * @param char $file_name * @param char $model_name * @param int $model_id * @return int record id * @throws CHttpException */ public function saveToDb($file_name, $model_name, $model_id) { $model = new D2files(); $model->file_name = $file_name; $model->upload_path = 'tttt'; $model->add_datetime = date('Y.m.d H:i:s'); $model->user_id = Yii::app()->user->id; $model->model = $model_name; $model->model_id = $model_id; try { $model->save(); } catch (Exception $e) { throw new CHttpException(500, $e->getMessage()); } return $model->primaryKey; }
/** * get model record files * @param string $model_name model name in format [module name].[model name] * @param int $model_id record is * @param int $type attachment type * @return array d2files models */ public static function getModelRecorFiles($model_name, $model_id, $type = false) { $criteria = new CDbCriteria(); $criteria->compare('model', $model_name); $criteria->compare('model_id', $model_id); if ($type) { $criteria->compare('type_id', $type); } $criteria->compare('deleted', 0); return D2files::model()->findAll($criteria); }
public function loadModel($id) { $m = D2files::model(); // apply scope, if available $scopes = $m->scopes(); if (isset($scopes[$this->scope])) { $m->{$this->scope}(); } $model = $m->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('D2filesModule.crud_static', 'The requested page does not exist.')); } return $model; }
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";