3

I'm trying to put together a simple Extension that will update an existing database table when an entries in a specific channel are set to 'open'.

I have been looking at the Ellislab tutorial on creating an extension and have looked at a few others so I have a basic understanding of what's going on but I'd be lying if I said I had a complete grasp at this stage!

So far, I've just got a debug message in the 'meat' of it so I can see if it is actually being called at the right time.

The first problem I'm running up against is getting the extension to activate. When I click "Enable" the CP reports that the extension has been enabled but the in the list of Extensions it still shows as disabled. No errors are shown.

Here's the code so far:

[CODE REMOVED - UPDATED VERSION BELOW]

UPDATE: I realised my error. I needed to include

 ee()->db->insert('extensions', $data);

within the activate_extension method.

UPDATE 2: The extension still doesn't show as "enabled" although it gets written into the extensions table and appears to call the add_credits method when required. Any ideas?

UPDATE 3: I have the extension working albeit with some hardcoded stuff that is only pertinent to this particular project.

However, I still cannot get it to actually show as being Enabled (even though it is) and I cannot get it to create a settings page (I think this is related to not being enabled as the settings link is shown just not active).

Here is the 'finished' code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
This extension is intended to add 'credits' to a users account
It is a custom extension that will take the metre value of an invoice entry and apply the required number of credits to the users account
It requires Cartthrob_credits extension to be installed

*/



class FM_credits_from_entry_ext {


    var $name           = 'FM Credits from entry';
    var $version        = '0.1a';
    var $description    = 'Custom extension that will take the credit value of an invoice entry and apply the required number of credits to the users account';
    var $settings_exist = 'y';
    var $docs_url       = '';

    var $settings       = array();


    /**
     * Constructor
     *
     * @param   mixed   Settings array or empty string if none exist.
     */




    function __construct($settings='') {
        $this->EE =& get_instance();
        $this->settings = $settings;
    } // END





    /** Activate extension **/

    function activate_extension() {

        $this->settings = array(
            'credits_per_metre'   => '1',
        );

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

         ee()->db->insert('extensions', $data);
    } // END



        /**
         * Update Extension
         *
         * This function performs any necessary db updates when the extension
         * page is visited
         *
         * @return  mixed   void on update / false if none
         */
        function update_extension($current = '')
        {
            if ($current == '' OR $current == $this->version)
            {
                return FALSE;
            }

            if ($current < '1.0')
            {
                // Update to version 1.0
            }

            ee()->db->where('class', __CLASS__);
            ee()->db->update(
                        'extensions',
                        array('version' => $this->version)
            );
        } // END




        /**
         * Disable Extension
         *
         * This method removes information from the exp_extensions table
         *
         * @return void
         */
        function disable_extension()
        {
            ee()->db->where('class', __CLASS__);
            ee()->db->delete('extensions');
        } //END




         function settings() {
            $settings = array();

            $settings['credits_per_metre']      = array('i', '', "1");

            return $settings;
         } // END





        /**
         * Add Credits 
         *
         * This function will fire when an entry is submitted/updates
         * If the entry is in the invoices channel then it will check the status
         * If the entry is being set to Approved (from another status) then it will grab the value of the metres purchased field
         * and add create a database row in the Cartthrob_credits_credits table for the logged in user
         * This is really rudimentary and probably won't port to other sites
         *
         */

        function add_credits($entry_id, $meta, $data) {

            $metres = $data['field_id_74'];

            $customer = $meta['author_id'];
            $status = $meta['status'];

            // check that this entry is in fact pending
            // we don't want to add points more than once
            // we are going to use the status to trigger the points addition
            // I can see a potential problem here if an approved entry is edited
            // may need an additional flag to say these points are already added
            // or a hook for the start of editing that only calls the add credits method
            // if the status is changed from pending to approved
            if($status == 'Approved') {

                if ($this->EE->db->table_exists('exp_cartthrob_credits_credits')) {   
                    $this->EE->load->helper('data_formatting'); 

                    $credits_per_metre = sanitize_number($this->settings['credits_per_metre']); 

                    $credits_to_add = $metres * $credits_per_metre;

                    $table_name = 'exp_cartthrob_credits_credits';

                    $query = 'SELECT * FROM '.$table_name.' WHERE member_id = '.$customer.' LIMIT 0,1';
                    $result = ee()->db->query($query);

                    if($result->num_rows()!==0) {

                        $credits = $result->row('credits');
                        $new_credit_total = $credits + $credits_to_add;

                        $query = 'UPDATE '.$table_name.' SET credits = '.$new_credit_total.' WHERE member_id = '.$customer;
                        $result = ee()->db->query($query);
                    } else {

                        // no customer found, create a row for them

                       $sql = 'INSERT INTO '.$table_name.' VALUES(null, '.$customer.',null, null, null, null, null, '.$credits_to_add.')';

                       $result = ee()->db->query($sql);

                    }


                }
            }
            return true;

        } // END

} // END CLASS

UPDATE: All sorted! The extension wasn't showing as 'Enabled' in the CP because the class name was 'FM_credits_from_entry_ext' with 2 capital letters at the start. Changing that to 'Fm_credits_from_entry_ext' fixed it and allowed access to the settings too.

1
  • I've realised my error here... I hadn't included the bit to actually insert the extension info into the extensions table... doh!
    – foamcow
    Jul 17, 2013 at 9:31

1 Answer 1

3

I managed to fix this.

My problem stemmed from an incorrect class name and missing a critical bit of code which inserted the necessary information into the extensions table.

I needed to insert;

 ee()->db->insert('extensions', $data);

within the activate_extension method

The extension wasn't showing as 'Enabled' in the CP because the class name was 'FM_credits_from_entry_ext' with 2 capital letters at the start. Changing that to 'Fm_credits_from_entry_ext' fixed it and allowed access to the settings too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.