Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » how to connect two values ?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 1448 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    how to connect two values ? Link to this post

    hi

    i have two dropdownfields

    static $db = array(
    'calendar_month' => "Enum('01,02,03,04,05,06,07,08,09,10,11,12','01')",
    'calendar_year => "Enum('2009,2010,2011','2009')"
    'calendar' => 'text'
    );

    static $has_one = array(
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Main", new DropdownField('calendar_month','Select month',singleton('PlOpiniePage')->dbObject('calendar_month')->enumValues()),Content);
    $fields->addFieldToTab("Root.Content.Main", new DropdownField('calendar_year','Select year',singleton('PlOpiniePage')->dbObject('calendar_year')->enumValues()),Content);
    return $fields;
    }

    how to connect these two values to one and put it into 'calendar' field ?

    01 + 2009 = 012009

  • UncleCheese
    Avatar
    4085 Posts

    Re: how to connect two values ? Link to this post

    A few things come to mind. You could try CompositeDateField. That may get you closer to what you're trying to do. I'm also wondering why you need to store the concatonated string as a database field in the first place. Why not just render it with a template function? Worst case, use onAfterWrite() to write the field manually using the post data.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to connect two values ? Link to this post

    hmmm

    i need 'calendar_month' field to print the names of the months in the different languages like this:

    <% if calendar_month == 1 %>
    january
    <% else if calendar_month == 2 %>
    february
    <% else if calendar_month == 3 %>

    .....

    and the same in polish, german, spain language

    i need 'calendar' field with values 012009, 022009, 032009 .... to compare it in the next/prev link

    public function nextPager() {
    $where = "ParentID = ($this->ParentID + 0) AND Calendar > ($Calendar->Sort + 0 )";
    $pages = DataObject::get("SiteTree", $where, "Calendar", "", 1);
    if($pages) {
    foreach($pages as $page) {
    return $page;
    }
    }
    }

    i cant compere these format YYYY-MM-DD which are generating by CompositeDateField

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to connect two values ? Link to this post

    thx
    done it

    public function onAfterWrite() {
    $cp = DataObject::get_by_id('PlOpiniePage',$this->ID);
    $cp->Calendar = $this->Calendar_moth . $this->Calendar_year;
    $cp->write();
    return parent::onAfterWrite();
    }

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to connect two values ? Link to this post

    ehhhh
    now when i click GO button to create new page, the page is creating but CMS doesn't refresh automaticly
    i must click F5 to refresh browser

    and the same problem is with Save & Publish button
    Save & Publish thinking is never ending ...

    what is wrog with this onAfterWrite()? ? ?

  • UncleCheese
    Avatar
    4085 Posts

    Re: how to connect two values ? Link to this post

    It seems to me you're working way too hard, here. First of all, if you need the months in different languages, just use

    strftime('%B', $timestamp);

    It's built into PHP.

    Second, I have no idea what's going on here:

    ublic function nextPager() {
    $where = "ParentID = ($this->ParentID + 0) AND Calendar > ($Calendar->Sort + 0 )";
    $pages = DataObject::get("SiteTree", $where, "Calendar", "", 1);
    if($pages) {
    foreach($pages as $page) {
    return $page;
    }
    }
    }

    Why + 0? And why are you relying on consecutive increments of ParentID? What happens if you delete one? Will this function break?

    Why are you using DataObject::get() when you have a limit of 1? Why not DataObject::get_one("SiteTree", $where);

    And why are you going into a loop only to return a value in the first iteration? What you have there is the same as:

    return $pages->First();

    Have you seen the EventCalendar module? You may be reinventing the wheel, here.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to connect two values ? Link to this post

    don't ask me why
    here is the tutorial of this prev/next function
    http://doc.silverstripe.com/doku.php?id=recipes:previousornext&s=previouspager

  • UncleCheese
    Avatar
    4085 Posts

    Re: how to connect two values ? Link to this post

    I cannot for the life of me understand why someone would do it that way and I question why it's in the Wiki.

    If I understand correctly, you're trying to step through your pages in order of their Calendar value. I think this should work fine for you:

    public function nextPager() {
    return DataObject::get_one("SiteTree", "ParentID = $this->ParentID AND Calendar > $this->Calendar", true, "Calendar ASC");
    }

    public function nextPager() {
    return DataObject::get_one("SiteTree", "ParentID = $this->ParentID AND Calendar < $this->Calendar", true, "Calendar DESC");
    }

    1448 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.