Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

New Module to Create/Edit/Delete DataObject rows in CMS (ss v2.2.2)


Reply

3 Posts   1934 Views

Avatar
swaiba

3 December 2009 at 2:47am (Last edited: 4 December 2009 12:36am), Forum Moderator, 1796 Posts

Hi,

I am trying to get a simple new Module into the CMS (to edit several tables of data, maybe more in the future). I have been reading the example in the silverstripe book and I would like to create something very similar (with as little code as possible) to maintain the data in several reference tables. I'd like to be using ModelAdmin, but due to 2.2.2 I think using GenericDataAdmin is the best option.

At the moment I have a new DataObject called PromoCodes and a module called myadmin. The CMS display the new module in the top menu bar... but the left and right main sections are not close to my desire yet. Currently the left area shows a search form that when submitted shows the results, but not into the right area. And the right area shows the contents of the table (SELECT * FROM) but when a record is clicked it just refresehs the screen.

The left area I would like (if you have the book I am looking at 6.3 Using ModelAdmin for Data Management) to show this table/dataobject as part of a list or one of a set of tabs. Then selecting that table updates the right side with the 'search results' (select from table), then from this view a row could be viewed, editted or deleted. In the case of a view/edit I would like to have the right area become a single generic record editor. This is a wish, but anything that would accomplish maintaining this required data in the simplist code possible would be greatly appreciated.

I have included my current code and ask very politely if someone could please help me with this :-)

mysite/code/PromoCodes.php

class PromoCodes extends DataObject
{
   static $db = array(
      'ID' => 'Int',
      'PromoCode' => 'Text',
      'ExipryDate' => 'Date',
      'IsLimted' => 'Boolean',
      'Limit' => 'Int',
      'Type' => "Enum(array('Percent', 'Fixed Value'))",
      'DiscountAmount' => 'Int'
   );
}

myadmin/_config.php

Object::addStaticVars('LeftAndMain', array('extra_menu_items' => array('Admin' => 'admin/myadmin/')));
Director::addRules(100, array('admin/myadmin/$Action/$ID' => 'MyAdmin'));

myadmin/code/MyAdmin.php

class MyAdmin extends GenericDataAdmin
{
   public static $data_type = 'PromoCodes';
   
   public static $result_columns = array('Promotional Code' => 'PromoCode',
                     'Exipry Date' => 'ExipryDate',
                     'Is Limted?' => 'IsLimted',
                     'Limit' => 'Limit',
                     'Type' => 'Type',
                     'Discount Amount' => 'DiscountAmount');
   
   function performSearch()
   {
      return DataObject::get('PromoCodes');
   }
   
   function getSearchFields()
   {
      return new FieldSet(
         new TextField('PromoCode', "PromoCode", "test"),
         new CurrencyField('ExipryDate', "ExipryDate", "test")
      );
   }
   
   function getLink()
   {
      if (!isset($action))
         $action="";
      return "admin/myadmin/$action";
   }
}

myadmin/templates/MyAdmin_right.ss

<% if EditForm %>
   $EditForm
<% else %>
   $Results
<% end_if %>

<p id="statusMessage" style="visibility:hidden"></p>

myadmin/templates/MyAdmin_left.ss

<h2>Admin</h2>
<div id="treepanes" style="overflow-y: auto;">
      <div id="sitetree_holder">   
         <div id="sitetree_ul">
            $SearchForm
         </div>
      </div>
</div>

Avatar
swaiba

4 December 2009 at 12:25am (Last edited: 4 December 2009 12:28am), Forum Moderator, 1796 Posts

Hello again,

Sorry to hassle you nice folks, but I could really use some help here please. :)

I would upgrade if my predecessor had not customized the 'core' code so much.

Any help on how to add a module in 2.2.2 that will simply edit refernce data in DataObjects (that have already been created and all the site related functionality is there) would be very much appreciated - I've been on this for two days and it seems ridicuslous that it is so difficult. :(

Barry

Avatar
Nicolaas

8 December 2009 at 4:08am Forum Moderator, 213 Posts

Hi Barry

There are a couple of options

* use the standard form functionality and build something custom (should take only a few hours).
* integrate modeladmin into your version (might be a nightmare, but worth a try)
* debug your current code into something that works

Between those three options I am sure there is hope ;-)

Best option: UPGRADE!