SilverStripe get half of image width/height - image

how can I get calculate with image height and width? I am trying to center an image like described in the first answer:
How to make an image center (vertically & horizontally) inside a bigger div
That's what I have tried so far:
<% control ProfileImage %>
<% if getOrientation = 1 %>
<img src="$URL" style="margin-left: -{$Width/2)}px" class="portrait"/>
<% else_if getOrientation = 2 %>
<img src="$URL" class="landscape"/>
<% end_if %>
<% end_control %>
The output of the decisive line is:
<img src="/assets/Uploads/picture.jpg" style="margin-left: -{154/2)}px" class="portrait"/>
Then I tried to write an own function:
class Profile extends Page{
//...
function GetHalfWidth(){
return ($this->ProfileImage->Width)/2;
}
//...
}
But I get the following error when I try to view the page in the frontend:
[Notice] Trying to get property of non-object

To get at the width of your image in the model you can change your function to read:
function GetHalfWidth() {
$width = $this->obj('ProfileImage')->Width;
return $width / 2;
}
However, if you are trying to reference that method form within the control loop of ProfileImage you'll need to 'jump out' and use $Top.GetHalfWidth
I would think a better way to handle it would be to define it as a method of your ProfileImage, if ProfileImage was subclassed from Image...
In Profile.php
<?php
class Profile extends Page {
...
public static $has_one = array(
'ProfileImage' => 'Profile_Image'
);
...
}
class Profile_Controller extends Page_Controller {
...
}
class Profile_Image extends Image {
function GetHalfWidth() {
$width = $this->Width;
return $width / 2;
}
}

Related

How to get image url in .ascx?

I have an image field in Sitecore from which I need its url. In code behind I have one item:
Item contactSlide = itemHelper.GetItemByPath("/sitecore/content/AMM/Resources/Slides/Slide Contact");
In the .ascx it looks like this:
<div class="contact-section grid-padding-big" style="background-image: url(img/bg.jpg)">
I need to replace img/bg.jpg with the url of the image field of the item.
In repeaters I normally have a method in the code behind for this:
public string GetImageUrl(Item item)
{
Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)item.Fields["Bild"]);
if (imgField.MediaItem != null)
{
string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imgField.MediaItem);
return mediaUrl;
}
return "";
}
And in the .ascx I call it like this:
<div style="background-image:url(<%# GetImageUrl((Sitecore.Data.Items.Item) Container.DataItem) %>)"></div>
This works when using this method while in repeaters. But now I have a single item. How can I pass the item to the method to get its image url? Or is there another way to do this?
You can use code in a .ascx within <%= and %>, so you could simply do this:
<div style="background-image:url(<%= GetImageUrl(itemHelper.GetItemByPath("/sitecore/content/AMM/Resources/Slides/Slide Contact")) %>)"></div>
You can also define a method in your code behind to directly get the url from the item you have already resolved:
public string GetBackgroundImageUrl()
{
Item contactSlide = itemHelper.GetItemByPath("/sitecore/content/AMM/Resources/Slides/Slide Contact");
return this.GetImageUrl(contactSlide);
}
And call this in the .ascx:
<div style="background-image:url(<%= GetBackgroundImageUrl() %>)"></div>

Silverstripe Sessions or URL Parameters

I'm trying to figure out how to pass a value from my .ss page to my controller for a custom search filter that I've built. The idea is you click on this image or a form button and then the page will set a session variable and refresh itself. Upon page load the page loads different information depending on what it reads in the session variable. I can accomplish the same thing with URL Parameters but there are no examples online that I could find that would show me how to do this.
Basically I have this as my php:
class ArticleHolder_Controller extends Page_Controller {
public function ValidateType(){
if(isset($_SESSION['mySearchTag']) && !empty($_SESSION['mySearchTag'])) {
$tag = $_SESSION['mySearchTag'];
}
else{
$tag='News';
}
$filter = $this::get()->filter('Filters:PartialMatch', $tag)->First();
if ($filter == NULL){
return NULL;
}
else{
$_SESSION['mySearchTag']=$tag;
return $this->PaginatedPages();
}
}
public function PaginatedPages(){
$paginatedItems = new PaginatedList($this->filterArticles($_SESSION['mySearchTag']), $this->request);
$paginatedItems->setPageLength(3);
return $paginatedItems;
}
public function filterArticles($tag){
return ArticlePage::get()->filter('category:PartialMatch', $tag)->sort('Date DESC');
}
}
my .ss looks like this:
<% if ValidateType() %>
<ul>
<% loop $PaginatedPages %>
<li>
<div class="article">
<h2>$Title</h2>
<h3>$Date</h3>
<img class="indent" src="$Photo.link" alt="image"/>
<p class="indent">$Teaser</p>
</div>
</li>
<% end_loop %>
</ul>
<% include Pagination %>
<% else %>
<p>SORRY NO RESULTS WERE FOUND</p>
<% end_if %>
This code works as is. What I cannot figure out how to now add a clickable button on .ss page that will reload the page and set a session variable value.
If I can achieve this with url parameters then that can work too, I just need to know how to set them in the .ss page and how to retrieve them in the php.
Create a SetFilter function that will take the URL parameter ID and set it to your session variable:
public function SetFilter() {
if($this->request->param('ID')) {
$_SESSION['mySearchTag'] = $this->request->param('ID');
}
return array();
}
Make sure your SetFilter function is added to the $allowed_actions of your controller:
static $allowed_actions = array (
'SetFilter'
);
This function is called by your page link followed by /SetFilter/[your-filter].
In your template you would create a link to create this filter like so:
Filter articles by example

Silverstripe: Dataobject in Siteconfig output in template

I have a problem similar described on the silverstripe problem: [http://www.silverstripe.org/dataobjectmanager-module-forum/show/19853][1]
I have a working DataObject, wich mainly provides a Title, Caption and Image.
This goes in an has_many, which should provide a slider in the template output.
The CMS part is all done, meaning I can add multiple 'slides' from a tab in the SiteConfig.
Only the output in the template won't seem te happen.
For the codes I use:
SingleSlide extends DataObject:
public function getCMSFields_forPopup()
{
return new FieldSet(
new ImageUploadField('SlideImg', 'Afbeelding van slide'),
new TextField('SlideTitle'),
new TextField('SlideCaption'),
new SimpleSiteTree('SlideLinkID')
);
}
In SiteConfigOverride
$fields->addFieldToTab('Root.SliderA', new ComplexTableField(
$this->owner, 'SliderA', 'SingleASlide',
array('SlideImg' => 'Afbeelding van slide', 'SlideTitle' => 'Titel van Slide', 'SlideCaption' => 'Tekst bij slide', 'SlideLink.Title' => 'Link naar pagina'
)));
All left to do is be able to get the Output from here in the template.
Any help would be great!
Regards,
Kay
You can add a function to the Page_Controller class in Page.php such as below
class Page_Controller extends ContentController {
...
function SingleSlideList() {
return DataObject::get('SingleSlide');
}
...
}
Then in your ss file, you can use something like the following:
<% control SingleSlideList %>
<div class="Image"><% control SlideImg %><% control CroppedImage(880,493) %><img src="$BaseHref$Filename.XML" height="$Height" width="$Width"><% end_control %><% end_control %></div>
<div class="Content">
<h2>$SlideTitle</h2>
<p>$SlideCaption</p>
<p>my link</p>
</div>
<% end_control %>
you will need to change the html to work with what you need.

How can I get access to a variable in a View which was created in a Controller?

How can I get access to a variable in a View which was created in a Controller?
Either put the variable into the Model that you are using for your View
Or use a ViewBag variable - e.g. from http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx
public ActionResult Index()
{
List<string> colors = new List<string>();
colors.Add("red");
colors.Add("green");
colors.Add("blue");
ViewBag.ListColors = colors; //colors is List
ViewBag.DateNow = DateTime.Now;
ViewBag.Name = "Hajan";
ViewBag.Age = 25;
return View();
}
and
<p>
My name is
<b><%: ViewBag.Name %></b>,
<b><%: ViewBag.Age %></b> years old.
<br />
I like the following colors:
</p>
<ul id="colors">
<% foreach (var color in ViewBag.ListColors) { %>
<li>
<font color="<%: color %>"><%: color %></font>
</li>
<% } %>
although hopefully you'll be using Razor :)
You need to send the variable to the view in the ViewModel (the parameter to the View() method) or the TempData dictionary.
You can add it to the ViewData[] dictionary or the (newer) ViewBag dynamic.
In your controller:
ViewData['YourVariable'] = yourVariable;
// or
ViewBag.YourVariable = yourVariable;
In your view:
<%: ViewData["yourVariable"] %>
// or
<%: ViewBag.YourVariable %>

Image display in asp.net mvc

i can't display the image on my aspx view..
i'm using mysql as database
i have this code for my model:
Imports Microsoft.VisualBasic
Imports System.Data
Public Class ClassPhotosConnection
Inherits ClassConnection
Public Function pictureSelect() As DataTable
Return ReadData("SELECT * FROM pictures")
End Function
End Class
for the controller:
Public Class AdministrationController
Inherits Global.System.Web.Mvc.Controller
Private dPhotos As New ClassPhotosConnection
<AcceptVerbs(HttpVerbs.Get)> _
Function Photos() As ActionResult
Dim _photos As DataTable = dPhotos.pictureSelect()
Return View(_photos)
End Function
End Class
for the view:
<div>
<form action="<%url.action("Photos") %>">
<%Using Html.BeginForm%>
<%Dim _photos As datatable = ViewData.Model%>
<%For count As Integer = 0 To _photos.Rows.Count - 1%>
<img src='<%=_photos.Rows(count).Item("picURL") %>' alt="" />
<p>
<%=_photos.Rows(count).Item("picCaption")%>
</p>
<%Next%>
<%End Using%>
</form>
</div>
the only thing that is displayed is the picCaption..it seems that it cannot call src='<%=_photos.Rows(count).Item("picURL") %>'
how else can i display the image?
thank you!
does the picURL contain a relative or absolute reference to the image? If the reference is realtive, you'll probably need to use the Url.Content method to map the path correctly.
<img src='<%= Url.Content("~/" + _photos.Rows(count).Item("picURL")) %>' alt="" />

Resources