ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DB insert after new entry

September 15, 2011 12:48pm

Subscribe [2]
  • #1 / Sep 15, 2011 12:48pm

    adisys

    23 posts

    Hello,

    I have this channel I’ve setup where users post “work interventions” using SafeCracker.
    There are two date-time fields there: intervention_start_date & intervention_end_date

    Based on this two fields, I have a PHP script that does some calculations and outputs two values.

    I need to store these values together with the new entry in the DB.

    How should I go about doing this?

    I was thinking maybe there is a way to append my PHP function to the new entry call. Is this possible? Can my function get passed the “intervention_start_date” & “intervention_end_date” variables from the new entry, then do the calculations and insert the additional output in the DB?

    Thanks so much!

  • #2 / Sep 15, 2011 10:08pm

    Dan Decker

    7338 posts

    Hello adisys,

    To do what you are looking to do, you might think about writing an extension. EE extensions “listen” for hooks to fire at execution time. When a certain hook is called, it will run your extension. This hook would be the one you would be interested in for this purpose.

    The extension could also handle all the mathematic operations as well before inserting the value into your custom field.

    Here are some additional resources to get you started:
    Extensions Development
    and
    Pkg.io to help jumpstart all the necessary items to write an extension. Let me know if you have any questions along the way.

    Cheers,

  • #3 / Sep 21, 2011 11:13am

    adisys

    23 posts

    Thank you Dan,

    I created an extensions using Pkg.io, but nothing much going on inside for now…
    Do you know of an extension that works similar to what I am trying to achieve?

    I am interested in just an example of how I would:
    - get the custom field value I am interested in
    - after I apply the calculations post the new value in another custom field for that entry

    I tried finding extensions on Devot-EE using the “entry_submission_ready” HOOK but none came up: http://devot-ee.com/hooks/ee/entry_submission_ready

    Thanks so much!

  • #4 / Sep 21, 2011 12:30pm

    adisys

    23 posts

    I got it working but only if the new entry is posted from the CP and not from SafeCracker.

    Is there another hook I need to use to get this working with SafeCracker?

    my code so far:

    public function activate_extension()
        {
            // Setup custom settings in this array.
            $this->settings = array();
            
            $data = array(
                'class'        => __CLASS__,
                'method'    => 'oc_overtime',
                'hook'        => 'entry_submission_ready',
                'settings'    => serialize($this->settings),
                'version'    => $this->version,
                'enabled'    => 'y'
            );
    
            $this->EE->db->insert('extensions', $data);            
            
        }    
    
        // ----------------------------------------------------------------------
        
        /**
         * oc_overtime
         *
         * @param 
         * @return 
         */
        public function oc_overtime()
        {
            // Add Code for the entry_submission_ready hook here.  
            if ($this->EE->api_channel_entries->data['channel_id'] == '37') {
                
            $oc_new .= $this->EE->api_channel_entries->data['field_id_231'];
            $oc_new .= "XX";
            $this->EE->api_channel_entries->data['field_id_231'] = $oc_ticket_new;        
            }
        }

    As you can see this is only basic right now.

  • #5 / Sep 22, 2011 12:38am

    Dan Decker

    7338 posts

    adisys,

    Excellent progress! Congratulations! You might have a look at safecracker’s hooks if that’s the route you are needing to go.

    Cheers,

  • #6 / Sep 22, 2011 5:55am

    adisys

    23 posts

    Dan thanks, its not much but gets the job done.

    I have been trying to get my code to fire on entry submissions via SafeCracker, but with no success so far. Do you spot the problem?

    public function activate_extension()
        {
            // Setup custom settings in this array.
            $this->settings = array();
            
            $hooks = array(
                'entry_submission_ready'    => 'oc_overtime',
                'safecracker_submit_entry_start'    => 'oc_overtime_safecracker',
            );
    
            foreach ($hooks as $hook => $method)
            {
                $data = array(
                    'class'        => __CLASS__,
                    'method'    => $method,
                    'hook'        => $hook,
                    'settings'    => serialize($this->settings),
                    'version'    => $this->version,
                    'enabled'    => 'y'
                );
    
                $this->EE->db->insert('extensions', $data);            
            }
        }    
    
        // ----------------------------------------------------------------------
        
        /**
         * oc_overtime
         *
         * @param 
         * @return 
         */
        public function oc_overtime()
        {
            // Add Code for the entry_submission_ready hook here.  
            if ($this->EE->api_channel_entries->data['channel_id'] == '37') {
                
            $oc_ticket_new .= $this->EE->api_channel_entries->data['field_id_231'];
            $oc_ticket_new .= "XX";
            $this->EE->api_channel_entries->data['field_id_231'] = $oc_ticket_new;        
            }
        }
    
        public function oc_overtime_safecracker()
        {
            // Add Code for the safecracker_submit_entry_start hook here.  
        
            if ($this->EE->input->post('channel_id') == '37') {
                
            $oc_ticket_new .= $this->EE->input->post('field_id_231');
            $oc_ticket_new .= "XX";
            $_POST['field_id_231'] = $oc_ticket_new;
            }
        }

    I disable and enable the extension when adding the new hook, but still not working from the front-end with safecracker :( maybe it has something to do with the Priority param?

  • #7 / Sep 22, 2011 8:32am

    adisys

    23 posts

    Tried with this but still no luck :(

    public function oc_overtime_safecracker($sc)
        {
            if ($sc->channel['channel_id'] == '37') {
            $oc_ticket_new .= $_POST['field_id_231'];
            $oc_ticket_new .= "XX";
            $_POST['field_id_231'] = $oc_ticket_new;
            }
                
        }
    if ($sc->channel['channel_id'] == '37')

    This one works, but the rest don’t.

    Later edit:

    Got it working using $_POST[‘field_name’] as opposed to $_POST[‘field_id’].
    But do I need to sanitize this _POST fields before I operate with them? If yes than how?

  • #8 / Sep 22, 2011 7:00pm

    Dan Decker

    7338 posts

    adisys,

    I see a typo, you are assigning your hooks to $hooks but in:

    $data = array(
                    'class'        => __CLASS__,
                    'method'    => $method,
                    'hook'        => $hook,
                    'settings'    => serialize($this->settings),
                    'version'    => $this->version,
                    'enabled'    => 'y'
                );

    You are assigning ‘hook’ to $hook , not the lack of the ‘s’ Try:

    $data = array(
                    'class'        => __CLASS__,
                    'method'    => $method,
                    'hook'        => $hooks,
                    'settings'    => serialize($this->settings),
                    'version'    => $this->version,
                    'enabled'    => 'y'
                );

    And see if that sets you straight.
    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases