/// <summary>
        /// Renders the preview.
        /// </summary>
        /// <param name="output">The output.</param><param name="item">The item.</param>
        private static void RenderPreview(HtmlTextWriter output, Item item)
        {
            Assert.ArgumentNotNull(output, "output");
            Assert.ArgumentNotNull(item, "item");
            MediaItem       mediaItem    = item;
            MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();

            shellOptions.AllowStretch    = false;
            shellOptions.BackgroundColor = Color.White;
            shellOptions.Language        = item.Language;
            shellOptions.Thumbnail       = true;
            shellOptions.UseDefaultIcon  = true;
            shellOptions.Width           = 192;
            shellOptions.Height          = 192;
            string mediaUrl = MediaManager.GetMediaUrl(mediaItem, shellOptions);

            output.Write("<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            output.Write("<tr><td align=\"center\" height=\"100%\">");
            output.Write("<div class=\"scPreview\">");
            output.Write("<img src=\"" + mediaUrl + "\" class=\"scPreviewImage\" border=\"0\" alt=\"\" />");
            output.Write("</div>");
            output.Write("<div class=\"scPreviewHeader\">");
            output.Write(item.DisplayName);
            output.Write("</div>");
            output.Write("</td></tr>");
            if (!(MediaManager.GetMedia(MediaUri.Parse(mediaItem)) is ImageMedia || mediaItem.MimeType == "image/svg+xml"))
            {
                output.Write("</table>");
            }
            else
            {
                output.Write("<tr><td class=\"scProperties\">");
                output.Write("<table border=\"0\" class=\"scFormTable\" cellpadding=\"2\" cellspacing=\"0\">");
                output.Write("<col align=\"right\" />");
                output.Write("<col align=\"left\" />");
                output.Write("<tr><td>");
                output.Write(Translate.Text("Alternate text:"));
                output.Write("</td><td>");
                output.Write("<input type=\"text\" id=\"AlternateText\" value=\"{0}\" />",
                             HttpUtility.HtmlEncode(mediaItem.Alt));
                output.Write("</td></tr>");
                output.Write("<tr><td>");
                output.Write(Translate.Text("Width:"));
                output.Write("</td><td>");
                output.Write("<input type=\"text\" id=\"Width\" value=\"{0}\" />",
                             HttpUtility.HtmlEncode(mediaItem.InnerItem["Width"]));
                output.Write("</td></tr>");
                output.Write("<tr><td>");
                output.Write(Translate.Text("Height:"));
                output.Write("</td><td>");
                output.Write("<input type=\"text\" id=\"Height\" value=\"{0}\" />",
                             HttpUtility.HtmlEncode(mediaItem.InnerItem["Height"]));
                output.Write("</td></tr>");
                output.Write("</table>");
                output.Write("</td></tr>");
                output.Write("</table>");
                SheerResponse.Eval("scAspectPreserver.reload();");
            }
        }
        /// <summary>Handles a click on the OK button.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The arguments.</param>
        /// <remarks>
        /// When the user clicks OK, the dialog is closed by calling
        /// the <see cref="M:Sitecore.Web.UI.Sheer.ClientResponse.CloseWindow">CloseWindow</see> method.
        /// </remarks>
        protected override void OnOK(object sender, EventArgs args)
        {
            Assert.ArgumentNotNull(sender, ExtensionMethods.nameof(() => sender));
            Assert.ArgumentNotNull((object)args, ExtensionMethods.nameof(() => args));
            string str = this.Filename.Value;

            if (str.Length == 0)
            {
                SheerResponse.Alert("Select a media item.");
            }
            else
            {
                Item root = this.DataContext.GetRoot();
                if (root != null)
                {
                    Item rootItem = root.Database.GetRootItem();
                    if (rootItem != null && root.ID != rootItem.ID)
                    {
                        str = FileUtil.MakePath(root.Paths.Path, str, '/');
                    }
                }
                MediaItem mediaItem = (MediaItem)this.DataContext.GetItem(str, this.ContentLanguage, Sitecore.Data.Version.Latest);
                if (mediaItem == null)
                {
                    SheerResponse.Alert("The media item could not be found.");
                }
                else if (!(MediaManager.GetMedia(MediaUri.Parse((Item)mediaItem)) is ImageMedia))
                {
                    SheerResponse.Alert("The selected item is not an image. Select an image to continue.");
                }
                else
                {
                    MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();
                    shellOptions.Language = this.ContentLanguage;
                    string text  = !string.IsNullOrEmpty(HttpContext.Current.Request.Form["AlternateText"]) ? HttpContext.Current.Request.Form["AlternateText"] : mediaItem.Alt;
                    Tag    image = new Tag("img");
                    this.SetDimensions(mediaItem, shellOptions, image);
                    image.Add("Src", MediaManager.GetMediaUrl(mediaItem, shellOptions));
                    image.Add("Alt", StringUtil.EscapeQuote(text));
                    image.Add("_languageInserted", "true");
                    if (this.Mode == "webedit")
                    {
                        SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(image.ToString()));
                        base.OnOK(sender, args);
                    }
                    else
                    {
                        SheerResponse.Eval("TinyMCEEditor.InsertImage.scClose(" + StringUtil.EscapeJavascriptString(image.ToString()) + ")");
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Instructs the renderer to render the media URL only, not the image control or page editor control
        /// </summary>
        /// <returns>The field's media url</returns>
        public string UrlOnly()
        {
            var imageField = (Sitecore.Data.Fields.ImageField) this.Item.Fields[this.FieldName];

            if (imageField != null && imageField.MediaItem != null)
            {
                // TODO: add support for more media option attributes, for UrlOnly rendering
                ////var mediaUrlOptions = MediaUrlOptions.GetThumbnailOptions(imageField.MediaItem);
                var mediaUrlOptions = MediaUrlOptions.GetShellOptions();
                mediaUrlOptions.Width  = this.Attributes.ContainsKey("width") ? (int)this.Attributes["width"] : mediaUrlOptions.Width;
                mediaUrlOptions.Height = this.Attributes.ContainsKey("height") ? (int)this.Attributes["height"] : mediaUrlOptions.Height;
                return(MediaManager.GetMediaUrl(imageField.MediaItem, mediaUrlOptions));
            }
            else
            {
                return(string.Empty);
            }
        }
        /// <summary>
        /// Renders the list view item.
        /// </summary>
        /// <param name="output">The output.</param><param name="item">The child.</param>
        private static void RenderListviewItem(HtmlTextWriter output, Item item)
        {
            Assert.ArgumentNotNull(output, "output");
            Assert.ArgumentNotNull(item, "item");
            MediaItem mediaItem = item;

            output.Write(
                "<a href=\"#\" class=\"scTile\" onclick=\"javascript:return scForm.postEvent(this,event,'Listview_Click(&quot;" +
                item.ID + "&quot;)')\" >");
            output.Write("<div class=\"scTileImage\">");

            if (item.TemplateID == TemplateIDs.Folder || item.TemplateID == TemplateIDs.TemplateFolder || item.TemplateID == TemplateIDs.MediaFolder)
            {
                new ImageBuilder
                {
                    Src    = item.Appearance.Icon,
                    Width  = 48,
                    Height = 48,
                    Margin = "24px 24px 24px 24px"
                }.Render(output);
            }
            else
            {
                MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();
                shellOptions.AllowStretch    = false;
                shellOptions.BackgroundColor = Color.White;
                shellOptions.Language        = item.Language;
                shellOptions.Thumbnail       = true;
                shellOptions.UseDefaultIcon  = true;
                shellOptions.Width           = 96;
                shellOptions.Height          = 96;
                string mediaUrl = MediaManager.GetMediaUrl(mediaItem, shellOptions);
                output.Write("<img src=\"" + mediaUrl + "\" class=\"scTileImageImage\" border=\"0\" alt=\"\" />");
            }

            output.Write("</div>");
            output.Write("<div class=\"scTileHeader\">");
            output.Write(item.DisplayName);
            output.Write("</div>");
            output.Write("</a>");
        }
Пример #5
0
 /// <summary>
 /// Handles the Change event.
 ///
 /// </summary>
 /// <param name="message">The message.</param>
 protected override void DoChange(Message message)
 {
     Assert.ArgumentNotNull(message, "message");
     base.DoChange(message);
     if (string.IsNullOrEmpty(Value))
     {
         ClearImage();
     }
     else
     {
         XmlValue.SetAttribute("mediapath", Value);
         var path = Value;
         if (!path.StartsWith("/sitecore", StringComparison.InvariantCulture))
         {
             path = "/sitecore/media library" + path;
         }
         var mediaItem = (MediaItem)Client.ContentDatabase.GetItem(path);
         if (mediaItem != null)
         {
             var str = mediaItem.InnerItem.Paths.Path;
             if (str.StartsWith("/sitecore/media library", StringComparison.InvariantCulture))
             {
                 str = str.Substring("/sitecore/media library".Length);
             }
             var shellOptions = MediaUrlOptions.GetShellOptions();
             var mediaUrl     = MediaManager.GetMediaUrl(mediaItem, shellOptions);
             XmlValue.SetAttribute("mediaid", mediaItem.ID.ToString());
             XmlValue.SetAttribute("mediapath", str);
             XmlValue.SetAttribute("src", Images.GetUncachedImageSrc(mediaUrl));
         }
         else
         {
             XmlValue.SetAttribute("mediaid", string.Empty);
             XmlValue.SetAttribute("src", string.Empty);
         }
         Update();
         SetModified();
     }
     SheerResponse.SetReturnValue(true);
 }
Пример #6
0
 /// <summary>Gets the media URL.</summary>
 /// <param name="item">The item.</param>
 /// <returns>The media URL.</returns>
 /// <contract>
 ///   <requires name="item" condition="not null" />
 ///   <ensures condition="not null" />
 /// </contract>
 private string GetMediaUrl(Item item)
 {
     Assert.ArgumentNotNull((object)item, ExtensionMethods.nameof(() => item));
     return(MediaManager.GetMediaUrl((MediaItem)item, MediaUrlOptions.GetShellOptions()));
 }
 private string GetMediaUrl(Item item)
 {
     Assert.ArgumentNotNull(item, "item");
     return(MediaManager.GetMediaUrl(item, MediaUrlOptions.GetShellOptions()));
 }
Пример #8
0
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                ItemDetails itemDetails;

                //Parse the JSON
                using (var sr = new StreamReader(context.Request.InputStream))
                {
                    itemDetails = JsonConvert.DeserializeObject <ItemDetails>(sr.ReadToEnd());
                }

                var editingLanguage = !string.IsNullOrEmpty(itemDetails.Language) ? LanguageManager.GetLanguage(itemDetails.Language) : LanguageManager.DefaultLanguage;

                var itemVersion = !string.IsNullOrEmpty(itemDetails.Version) ? Sitecore.Data.Version.Parse(itemDetails.Version) : Sitecore.Data.Version.Parse(1);

                //Load the item
                var currentItem = Context.ContentDatabase.GetItem(new ID(itemDetails.ItemId), editingLanguage, itemVersion);

                using (new EditContext(currentItem, false, false))
                {
                    currentItem["__Updated"] = DateUtil.IsoNow;
                    currentItem.Fields["__Updated by"].SetValue(Context.User.Name, true);

                    foreach (var clientFieldValue in itemDetails.Fields)
                    {
                        var field = currentItem.Fields[new ID(clientFieldValue.FieldId)];

                        if (!string.IsNullOrEmpty(clientFieldValue.Value))
                        {
                            switch (field.Type)
                            {
                            case "Date":
                            case "Datetime":
                                field.Value = DateUtil.ToIsoDate(DateUtil.ParseDateTime(clientFieldValue.Value, System.DateTime.MinValue));
                                break;

                            case "Droptree":
                                //The value for the tree is: [Source ID]|[Path to the item]
                                var values = clientFieldValue.Value.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                                var sourceItem = currentItem.Database.GetItem(new ID(values[0]));

                                if (sourceItem != null)
                                {
                                    if (values.Length == 1)
                                    {
                                        field.Reset();
                                    }
                                    else
                                    {
                                        var selectedItemName = values[1].Split('/').LastOrDefault();

                                        //Get Source Path then add the Path to the item
                                        var itemPath = sourceItem.Parent.Parent.Paths.Path + "/" + values[1];

                                        //Remove the display name from the item path
                                        itemPath = itemPath.Replace(selectedItemName, "");

                                        //Get The Parent Tree Item
                                        var dropTreeSelectedItemParent = sourceItem.Database.GetItem(itemPath);

                                        var childItem = (from i in dropTreeSelectedItemParent.Children
                                                         where i.DisplayName == selectedItemName
                                                         select i).FirstOrDefault();

                                        if (childItem != null)
                                        {
                                            field.Value = childItem.ID.ToString();
                                        }
                                    }
                                }

                                break;

                            case "File":
                            case "Image":
                                var       mediaPath = "/sitecore/Media Library" + clientFieldValue.Value;
                                MediaItem mediaItem = currentItem.Database.GetItem(mediaPath);

                                var xmlValue = new XmlValue("", field.Type.ToLower());

                                if (mediaItem != null)
                                {
                                    var shellOptions = MediaUrlOptions.GetShellOptions();
                                    var mediaUrl     = MediaManager.GetMediaUrl(mediaItem, shellOptions);

                                    xmlValue.SetAttribute("mediaid", mediaItem.ID.ToString());
                                    xmlValue.SetAttribute("mediapath", mediaItem.MediaPath);
                                    xmlValue.SetAttribute("src", mediaUrl);
                                }
                                else
                                {
                                    xmlValue.SetAttribute("mediaid", string.Empty);
                                    xmlValue.SetAttribute("mediapath", string.Empty);
                                    xmlValue.SetAttribute("src", string.Empty);
                                }

                                field.Value = xmlValue.ToString();

                                break;

                            default:
                                field.Value = clientFieldValue.Value;
                                break;
                            }
                        }
                        else
                        {
                            field.Reset();
                        }
                    }

                    if (!string.IsNullOrEmpty(itemDetails.NameFormatString))
                    {
                        var displayName = BuildName(itemDetails.NameFormatString, currentItem).Trim();
                        var name        = ItemUtil.ProposeValidItemName(displayName, "Unnamed item");

                        string uniqueName = ItemUtil.GetUniqueName(currentItem.Parent, name);

                        if (!string.IsNullOrEmpty(name))
                        {
                            currentItem["__Display name"] = displayName;
                            currentItem.Name = uniqueName;
                        }
                    }
                }

                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    displayName = string.IsNullOrEmpty(currentItem["__Display name"]) ? currentItem.Name : currentItem["__Display name"]
                }));
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Fatal("Error saving sub-item", ex, this);

                context.Response.StatusCode = 500;
                context.Response.Write(string.Format("Exception {0}({1}):\n{2}", ex.Message, ex.GetType().Name, ex.StackTrace));
            }
        }
        /// <summary>
        /// Handles a click on the OK button.
        ///
        /// </summary>
        /// <param name="sender">The sender.</param><param name="args">The arguments.</param>
        /// <remarks>
        /// When the user clicks OK, the dialog is closed by calling
        ///             the <see cref="M:Sitecore.Web.UI.Sheer.ClientResponse.CloseWindow">CloseWindow</see> method.
        ///
        /// </remarks>
        protected override void OnOK(object sender, EventArgs args)
        {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(args, "args");

            string str = Filename.Value;

            if (str.Length == 0)
            {
                SheerResponse.Alert("Select a media item.");
            }
            else
            {
                Item root     = DataContext.GetRoot();
                Item rootItem = root?.Database.GetRootItem();

                if (rootItem != null && root.ID != rootItem.ID)
                {
                    str = FileUtil.MakePath(root.Paths.Path, str, '/');
                }

                MediaItem mediaItem = DataContext.GetItem(str, ContentLanguage, Sitecore.Data.Version.Latest);
                if (mediaItem == null)
                {
                    SheerResponse.Alert("The media item could not be found.");
                }
                else if (!(MediaManager.GetMedia(MediaUri.Parse(mediaItem)) is ImageMedia || mediaItem.MimeType == "image/svg+xml"))
                {
                    SheerResponse.Alert("The selected item is not an image. Select an image to continue.");
                }
                else
                {
                    MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();
                    shellOptions.Language = ContentLanguage;
                    string text = !string.IsNullOrEmpty(HttpContext.Current.Request.Form["AlternateText"])
                        ? HttpContext.Current.Request.Form["AlternateText"]
                        : mediaItem.Alt;

                    if (mediaItem.MimeType == "image/svg+xml")
                    {
                        string svgImage = GetSVGImage(mediaItem, shellOptions);

                        if (Mode == "webedit")
                        {
                            SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(svgImage));
                            base.OnOK(sender, args);
                        }
                        else
                        {
                            SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(svgImage) + ")");
                        }
                    }
                    else
                    {
                        Tag image = new Tag("img");
                        SetDimensions(mediaItem, shellOptions, image);
                        image.Add("Src", MediaManager.GetMediaUrl(mediaItem, shellOptions));
                        image.Add("Alt", StringUtil.EscapeQuote(text));
                        image.Add("_languageInserted", "true");

                        if (Mode == "webedit")
                        {
                            SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(image.ToString()));
                            base.OnOK(sender, args);
                        }
                        else
                        {
                            SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(image.ToString()) + ")");
                        }
                    }
                }
            }
        }