Menu

Read-only expiration date automatically set.

Martin
2021-06-22
2021-07-01
  • Martin

    Martin - 2021-06-22

    Hi there,

    I need to create a expiration date field, this field should be read only and would be automatically filled up based on date selected (mostly at the time of creation object). So the filed would be calculated to expire, for example 2 weeks from now (Creation of object). Is it possible to do such a field?

    Thanks a lot.

     
  • Pierre Goiffon

    Pierre Goiffon - 2021-06-23

    Hello,

    Yes of course no problem !
    But you'll need to create a custom module for that ! If you don't know how, take a look at the dedicated section in the documentation : Customizing iTop [iTop Documentation]

    There is already in the default datamodel a field that does almost what you want to do : Ticket.last_update.

     
  • Martin

    Martin - 2021-06-23

    Hi,

    thanks for answer, I`ve already found some in documentation that you`ve mentioned, but topic is not clearly describing about fields, what type, or if there should be any relation between them, etc. I tried to overload method as described in topic, but it does not work . I need to create autofilled RO date field, that is calculated based on current time. Could you please correct me what is wrong...

    Thank you

    <?xml version="1.0" encoding="UTF-8"?>
    <itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
      <classes>
        <class id="TEST" _delta="define">
          <fields>
           <field id="date_start" xsi:type="AttributeDateTime" _delta="define">
                      <sql>date_start</sql>
                      <default_value/>
                      <is_null_allowed>true</is_null_allowed>
           </field>
           <field id="end_date" xsi:type="AttributeString" _delta="define">
              <sql>end_date</sql>
              <default_value/>
              <is_null_allowed>true</is_null_allowed>
           </field>
          </fields>
          <methods/>
          <presentation>
            <details>
              <items>
               <item id="col:col1">
                 <rank>10</rank>
                <items>
                 <item id="fieldset:Test">
                   <rank>10</rank>
                   <items>
                    <item id="date_start">
                         <rank>10</rank>
                    </item>
    
                    <item id="end_date">
                         <rank>30</rank>
                    </item>
    
                   </items>
                  </item>
                </items>
              </item>
             </items>
            </details>
          <search>
            <items>
             <item id="end_date">
                <rank>10</rank>
             </item>
            </items>
          </search>
          <list>
            <items>
            </items>
          </list>
         </presentation>
          <methods>
            <method id="OnInsert" _delta="define">
              <static>false</static>
              <access>public</access>
              <type>Overload-DBObject</type>
              <code><![CDATA[ public function OnInsert(){
                             parent::OnInsert();
                             $oStartDate = new DateTime($this->Get('date_start'));
                             $oEndDate = clone $oStartDate;
                             $iDuration = 24*3600*14 ; // 24 hours in seconds, 14 days
                             $oEndDate->modify( '+'.$iDuration.' seconds');
                             $this->Set('end_date',$oEndDate->Format(AttributeDateTime::GetInternalFormat()));
                      }]]>
              </code>
            </method>
          </methods>
        </class>
       </classes>
    <menus>
    
    </menus>
    </itop_design>
    
     

    Last edit: Martin 2021-06-23
  • Pierre Goiffon

    Pierre Goiffon - 2021-06-30

    Hello,

    In the XML you've forwarded, you're creating a new class, is this intended ? Weren't your new fields meant to be added on an existing class ?

    Note that having _delta="define" on the class node means you're adding the whole node and its subnodes, so no need to add more _delta attributes below (there is one more for example on the classes/class/methods/method node)

    Your end_date field is an AttributeString, I don't think this is a good idea : you should persist only typed values, and the conversion to strings should be done only when displaying.

    Also, there are no definitions to set the fields read only. There is a dedicated tutorial for that specific need : Force a field to be read only [iTop Documentation]

    To go on, can you describe precisely the problems you're dealing with ? "it does not work" is a bit vague :D

     
  • Martin

    Martin - 2021-07-01

    Hi, for some reason the methods didnt work no matter what I`ve tried, so I was thinking that something is missing, but eventually I found out that this line was killing it all the time:

     </field>
          </fields>
        <methods/>
          <presentation>
            <details>
              <items>
    

    I already had /methods right after fields and bellow methods were omitted. Now my code fully works and does what I wanted :)

     

    Last edit: Martin 2021-07-01
  • Pierre Goiffon

    Pierre Goiffon - 2021-07-01

    Ok thanks for your feedback !

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.