Results 1 to 9 of 9

Thread: useful logic hook.. php newbie

  1. #1
    ispytodd is offline Sugar Community Member
    Join Date
    Dec 2005
    Posts
    12

    Default useful logic hook.. php newbie

    Background: I have virtually no prior programming experience, so please excuse any blatant errors and assumptions I may make. That said, I've decided to try to add a wee bit of basic functionality to our case module via a custom logic hook. Hopefully it can benefit everyone out there. Hopefully I don't annoy the sugar gods.

    Our company relies heavily on Cases. Currently Sugar only tracks when a case is opened and/or modified. For reporting and billing reasons I need to know when a case is closed... I read the wonderful logic_hooks presentation and decided to add a custom field to track case closures (date_closed_c). I hope to use a logic hook to assign a value to date_closed_c whenever a case is closed.

    Problems:
    1) Custom field. I added a custom field (date_closed_c) to the cases module. For some reason the studio only lets you select from a small list of data types. I'd like a DateTime but only 'Date' is an option. I changed the field to my liking in phpMYAdmin. Bad? I know not to 'repair custom fields' in the studio if I make such a change.

    I'd also like date_closed_c to be null until I close a case (and give it a value). When I edit a case it seems to initialize all my custom fields to '0' even if they are not displayed on the edit screen. hurm. I can live with 00-00-0000 00:00:00.

    2)When to apply my logic? I crafted a crude conditional statement to write date_closed_c only when
    -before save
    -case is closed
    -case_closed_c does not exist/is null/is 0?<-- really depends on how i approach problem 1 above.

    3) How the heck do I assign the current DateTime to date_closed_c? TimeDate.php really scared me. I read over alot of code and decided to avoid it all and stick with date("Y-m-d H:i:s"). It works. sortof.

    So here's what I have now (yes, it's embarrassingly simple)
    PHP Code:
    <?php
    require_once('data/SugarBean.php');
    require_once(
    'modules/Cases/Case.php');

    class 
    CaseSetDateClosed {
      function 
    CaseSetDateClosed(&$bean$event$arguments)
      {
        
    // only set the closed date if the case is closed and there was no previous close date.  A case can only be closed once.
        
    if ($event == 'before_save' && $bean->status == 'Closed' && is_null($bean->date_closed_c) )
          {
            
    $bean->date_closed_c date("Y-m-d H:i:s");
          }
      }

    }
    ?>
    Last edited by ispytodd; 2006-11-28 at 09:47 PM.

  2. #2
    kpit's Avatar
    kpit is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Dec 2005
    Location
    Memphis, TN
    Posts
    996

    Default Re: useful logic hook.. php newbie

    I am looking into the date time. I will get back to this post in a day. Someone from the dev team may chime in on this sooner.
    Cheers,

    Max W. Blackmer, Jr.

    Blog
    Phone: +1 (901) 672-2694



  3. #3
    bart2puck is offline Junior Member
    Join Date
    Nov 2005
    Posts
    2

    Default Re: useful logic hook.. php newbie

    Has anyone got any information recently on this?

  4. #4
    ispytodd is offline Sugar Community Member
    Join Date
    Dec 2005
    Posts
    12

    Default Re: useful logic hook.. php newbie

    I didn't forget about this post, will reply soon. Just got real busy and managed to break something after upgrading to 4.5.g. I will be finalizing some things in the near future - probably over the holiday break.

    While this may be a good lesson in logic hooks, I have noticed that the general business logic may be flawed. For our company the date a case is closed could be a misrepresentation of the actual work (ie when a case is closed days after the work is done). I'm considering using notes/activities to log more detail on case activity. Perhaps a more advanced logic hook?

    -T

  5. #5
    kpit's Avatar
    kpit is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Dec 2005
    Location
    Memphis, TN
    Posts
    996

    Default Re: useful logic hook.. php newbie

    I didn't forget it ether. I had similar events going on. I will be working on documentation of the DateTime over the holiday. Mean while happy holidays everone.
    Cheers,

    Max W. Blackmer, Jr.

    Blog
    Phone: +1 (901) 672-2694



  6. #6
    andydreisch's Avatar
    andydreisch is offline Sugar Team Member
    Join Date
    Apr 2005
    Location
    San Jose
    Posts
    2,080

    Default Re: useful logic hook.. php newbie

    Hi ispytodd, you're raising the classic case (so to speak!) where it's more important to know when work ended on the case rather than when the disposition of the case reached a "closed" status.

    So, is it possible to create a new Case Status dropdown (i.e., "Completed") that signifies when work actually ended, distinct from "Closed"?

    In this case your hook will work, except that it need to track the date-time when the "Completed" state is reached rather than the "Closed" state.

    Obviously, you'd need to train your staff to set the "Completed" status appropriately.

    Andy
    Andy Dreisch
    Vice President, Online Team


    Check out our Podcasts!
    Sugar University for training
    Sugar Wiki for developer and user help
    SugarForge for modules, themes, lang packs
    SugarExchange for production-ready extensions
    Enter/view bugs via the Sugar bug tracker

  7. #7
    dtokeefe's Avatar
    dtokeefe is offline Sugar Community Member
    Join Date
    Mar 2005
    Location
    Sao Paulo, Brasil
    Posts
    671

    Default Re: useful logic hook.. php newbie

    For what it's worth, I also needed a custom field of type DateTime and had to make do with Date. It would be nice to have a DateTime option for custom fields.
    David O'Keefe
    Lampada Global Services
    SugarCRM Gold Partner
    USA: +1 908 998-2278
    BR: +55 11 3237-3110
    Skype: dtokeefe
    Email: equipe@lampadaglobal.com
    www.lampadaglobal.com

    Lampada Global delivers enterprise software and offshore programming services to customers around the world.

  8. #8
    oraclehara is offline Junior Member
    Join Date
    Dec 2006
    Posts
    4

    Default Re: useful logic hook.. php newbie

    Quote Originally Posted by ispytodd
    Background: I have virtually no prior programming experience, so please excuse any blatant errors and assumptions I may make. That said, I've decided to try to add a wee bit of basic functionality to our case module via a custom logic hook. Hopefully it can benefit everyone out there. Hopefully I don't annoy the sugar gods.

    Our company relies heavily on Cases. Currently Sugar only tracks when a case is opened and/or modified. For reporting and billing reasons I need to know when a case is closed... I read the wonderful logic_hooks presentation and decided to add a custom field to track case closures (date_closed_c). I hope to use a logic hook to assign a value to date_closed_c whenever a case is closed.

    Problems:
    1) Custom field. I added a custom field (date_closed_c) to the cases module. For some reason the studio only lets you select from a small list of data types. I'd like a DateTime but only 'Date' is an option. I changed the field to my liking in phpMYAdmin. Bad? I know not to 'repair custom fields' in the studio if I make such a change.

    I'd also like date_closed_c to be null until I close a case (and give it a value). When I edit a case it seems to initialize all my custom fields to '0' even if they are not displayed on the edit screen. hurm. I can live with 00-00-0000 00:00:00.

    2)When to apply my logic? I crafted a crude conditional statement to write date_closed_c only when
    -before save
    -case is closed
    -case_closed_c does not exist/is null/is 0?<-- really depends on how i approach problem 1 above.

    3) How the heck do I assign the current DateTime to date_closed_c? TimeDate.php really scared me. I read over alot of code and decided to avoid it all and stick with date("Y-m-d H:i:s"). It works. sortof.

    So here's what I have now (yes, it's embarrassingly simple)
    PHP Code:
    <?php
    require_once('data/SugarBean.php');
    require_once(
    'modules/Cases/Case.php');

    class 
    CaseSetDateClosed {
      function 
    CaseSetDateClosed(&$bean$event$arguments)
      {
        
    // only set the closed date if the case is closed and there was no previous close date.  A case can only be closed once.
        
    if ($event == 'before_save' && $bean->status == 'Closed' && is_null($bean->date_closed_c) )
          {
            
    $bean->date_closed_c date("Y-m-d H:i:s");
          }
      }

    }
    ?>
    Hi, i'm doing something similar to what you're doing. I've created a new field called date_completed_c in Cases to track the closing date. Can you please tell me is the source codes above written as a new .php field in the "Include" folder? Coz my customization doesn't seems to work. I'm using SugarOP Version 4.5.0h

  9. #9
    kpit's Avatar
    kpit is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Dec 2005
    Location
    Memphis, TN
    Posts
    996

    Default Re: useful logic hook.. php newbie

    Is the cusom field being saved or are you getting an unexpected date? There is an automated conversion of dates and datetime to GMT. you must take into account The current users timezone. You must make sure that the date time you set is according to the current users timezone and not nessarly server time which will reflect the wrong user timezone potentially. The date function will return the servers datetime not nessarly the users timezone. The TimeDate Class does take care of this for you.
    Cheers,

    Max W. Blackmer, Jr.

    Blog
    Phone: +1 (901) 672-2694



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •