예제 #1
0
    protected void Behaviour_Save(object sender, EventArgs e)
    {
        try
        {
            BXUser.DemandOperations(BXRoleOperation.Operations.FileManage);
            if (string.IsNullOrEmpty(mPath))
                throw new InvalidOperationException("Path is not defined!");

            bool isNew = AboutCreationNewFile;

            if (!isNew && !BXSecureIO.FileExists(mPath))
                throw new InvalidOperationException(string.Format("Could not find file by path specified: '{0}'!", mPath));

            //if(!BXPath.IsPage(mPath))
            //    throw new InvalidOperationException(string.Format("Specified file name is not allowed for page: '{0}'!", mPath));

			if (!IsPathFileExtensionAllowed())
				Close(string.Format(GetMessage("FILE_NAME_HAS_NOT_ALLOWED_EXTENSION"), VirtualPathUtility.GetFileName(mPath)), BXDialogGoodbyeWindow.LayoutType.Error, -1);

            if (!isNew)
            {
                Encoding contentEncoding = mContentEncoding != null ? mContentEncoding : BXConfigurationUtility.DefaultEncoding;

				if(Request.QueryString["noundo"] == null)
				{
					BXUndoPageModificationOperation undoOperation = new BXUndoPageModificationOperation();
					undoOperation.FileVirtualPath = mPath;
					undoOperation.FileEncodingName = contentEncoding.WebName;
					undoOperation.FileContent = BXSecureIO.FileReadAllText(mPath, contentEncoding);

					BXUndoInfo undo = new BXUndoInfo();
					undo.Operation = undoOperation;
					undo.Save();

					BXDialogGoodbyeWindow goodbye = new BXDialogGoodbyeWindow(string.Format(
						GetMessageRaw("OPERATION_HAS_BEEN_COMPLETED_SUCCESSFULLY_UNDO"), 
						string.Concat(undo.GetClientScript(), " return false;"), 
						"#"), -1, BXDialogGoodbyeWindow.LayoutType.Success);
					BXDialogGoodbyeWindow.SetCurrent(goodbye);
				}

                int i = 0;
                while (BXSecureIO.FileExists(mPath + "." + i))
                    i++;
                string tempFile = mPath + "." + i;

                string content = mMode == VisualPageEditorMode.Standard ? VisualEditor.Content : TextEditor.Text;

                BXSecureIO.FileMove(mPath, tempFile);

                if (IsPathFileExtensionPage())
                    BXSecureIO.SaveAspx(mPath, content, null, contentEncoding);
                else
                    BXSecureIO.FileWriteAllText(mPath, content, contentEncoding);

                BXSecureIO.FileDelete(tempFile);
            }
            else
            {
                if (BXSecureIO.FileExists(mPath))
                    Close(string.Format(GetMessageRaw("FILE_ALREADY_EXISTS"), mPath));

                string content = mMode == VisualPageEditorMode.Standard ? VisualEditor.Content : TextEditor.Text;
                Encoding contentEncoding = mContentEncoding != null ? mContentEncoding : BXConfigurationUtility.DefaultEncoding;

				DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath(mDirectoryPath));
				if(!di.Exists)
					di.Create();

				FileInfo fi = new FileInfo(HostingEnvironment.MapPath(mPath));
				using(FileStream fs = fi.Open(FileMode.CreateNew, FileAccess.Write, FileShare.None))
					using(StreamWriter sw = new StreamWriter(fs, contentEncoding))
						sw.Write(content);
            }

            string returnUrl = ReturnUrl;
            if (string.IsNullOrEmpty(returnUrl))
				returnUrl = IsPathFileExtensionPage() ? (!string.IsNullOrEmpty(mQueryString) ? string.Concat(BXSite.GetUrlForPath(mPath, null), "?", mQueryString) : BXSite.GetUrlForPath(mPath, null)) : string.Empty;
            Redirect(returnUrl, string.Empty, BXDialogGoodbyeWindow.LayoutType.Success);        
		}
        catch (System.Threading.ThreadAbortException)
        {
            //...игнорируем, вызвано Reload();
        }
        catch (Exception ex)
        {
            ShowError(ex.Message);
        }
    }
    protected void Behaviour_Save(object sender, EventArgs e)
    {
        BXUser.DemandOperations(BXRoleOperation.Operations.FileManage);
		BXSecureIO.DemandWrite(TargetFileAppRelPath);
        try 
        {
			BXUndoPageModificationOperation undoOperation = new BXUndoPageModificationOperation();
			undoOperation.FileVirtualPath = TargetFileAppRelPath;
			undoOperation.FileEncodingName = Encoding.UTF8.WebName;
			undoOperation.FileContent = BXSecureIO.FileReadAllText(TargetFileAppRelPath, Encoding.UTF8);

			BXUndoInfo undo = new BXUndoInfo();
			undo.Operation = undoOperation;
			undo.Save();

            BXPageProxy pageProxy = BXPageProxy.Create(TargetFileAppRelPath);
            BXComponent component = Component;
            Bitrix.Components.BXComponentProxy componentProxy = pageProxy.ResolveControlId(component.ID);
            foreach (KeyValuePair<string, BXParam> kv in component.ParamsDefinition)
            {
                if (componentProxy.Parameters.ContainsKey(kv.Key))
                    componentProxy.Parameters.Remove(kv.Key);
                
				BXParam param = kv.Value;
				componentProxy.Parameters.Add(kv.Key, param);
                
				string val;
				if (component.Parameters.TryGetValue(kv.Key, out val))
                {
                    param.SelectedValue = val;
                    param.IsDirty = true;
                }
            }

            if (pageProxy.IsDirty)
                pageProxy.Save();

			BXDialogGoodbyeWindow goodbye = new BXDialogGoodbyeWindow(string.Format(
				GetMessageRaw("OPERATION_HAS_BEEN_COMPLETED_SUCCESSFULLY_UNDO"), 
				string.Concat(undo.GetClientScript(), " return false;"), 
				"#"), -1, BXDialogGoodbyeWindow.LayoutType.Success);
			BXDialogGoodbyeWindow.SetCurrent(goodbye);

            Refresh(string.Empty, BXDialogGoodbyeWindow.LayoutType.Success);
        }
        catch (System.Threading.ThreadAbortException)
        {
            //...игнорируем, вызвано Close();
        }
        catch (Exception exc)
        {
            Close(exc.Message, BXDialogGoodbyeWindow.LayoutType.Error, -1);
        }
    }