Subscribers

edited November 2011 in Modules
I have two tables within the database"

One is called fuel_subscribers with the fields: id, subscriber_emai,l subscriber_active
The other table is called: fuel_subscriber_emails with: id, email_to, email_message

An i want it to display a dropdown box from the fuel_subscribers table with all the email addresses already in there, so within my model i have this currently that shows the form:

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

class Email_subscribers_model extends Base_module_model {
//public $required = array('subscriber_email');
function __construct() {
parent::__construct('fuel_subscriber_emails');
}

}

class Subscriber_model extends Base_module_record {

}
[/code]

So what would i need to put in this method in order to get the data from fuel_subscribers in a drop down, then send that email selected from the drop down to send an email?

Hope this make sense and hope someone can help!

Thankx.

Comments

  • edited 7:21PM
    You will first need to add a form_fields() method to your model to manipulate the fields for your form like so:
    ... function form_fields($values = array(), $related = array()) { // get the default fields first $fields = parent::form_fields($values, $related); // get the options list which is just a name/value associative array // the first option is the value of the field and the second option is the label // the third option is the where clause to apply which you may or may not want $email_options = $this->options_list('subscriber_email', 'subscriber_email', array('subscriber_active' => 'yes'); // now change the default field from a text field to a dropdown passing it the $email_options $fields['subscriber_email'] = array('type' => 'select', 'options' => $email_options, 'first_option' => 'Select an email address...'); return $fields; } ....

    Then, to send the email, you can use one of the model hook methods on your "Email_subscribers_model " class such as "on_after_save" or "on_after_post" to send the email perhaps using the CI email class. The on_after_post may work best because it is not linked to the saving of a record and is specifically called in the admin whereas, on_after_save is called after ever time a record is saved no matter where you are saving it from. Therefore, the code would be executed every time it is saved.

    For a list of the hooks, you can see them here at the bottom:
    http://www.getfuelcms.com/user_guide/libraries/my_model

    The CI email class documentation can be found here
    http://codeigniter.com/user_guide/libraries/email.html
  • edited 7:21PM
    Right ok i fully undertsand that but where in my code above should i place this?
  • edited 7:21PM
    In the Email_subscribers_model
  • edited 7:21PM
    So my full code looks like this:

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

    class Email_subscribers_model extends Base_module_model {
    public $required = array('email_to');

    function __construct() {
    parent::__construct('fuel_subscriber_emails');
    }

    function form_fields($values = array(), $related = array())
    {
    // get the default fields first
    $fields = parent::form_fields($values, $related);

    // get the options list which is just a name/value associative array
    // the first option is the value of the field and the second option is the label
    // the third option is the where clause to apply which you may or may not want
    $email_options = $this->options_list('subscriber_email', 'subscriber_email', array('subscriber_active' => 'yes');

    // now change the default field from a text field to a dropdown passing it the $email_options
    $fields['subscriber_email'] = array('type' => 'select', 'options' => $email_options, 'first_option' => 'Select an email address...');
    return $fields;


    }

    class Subscriber_model extends Base_module_model {

    }

    But sadly this still doesn't build a drop down box, would I have to declare the select function to the db and select the right table?
  • edited 7:21PM
    What does it display instead and you are referring to the form in the Admin correct?
  • edited 7:21PM
    I have done it all again with this code and now having trouble joining two tables:

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

    class Subscriber_model extends Base_module_model {
    //public $required = array('subscriber_email');
    function __construct() {
    parent::__construct('fuel_subscribe');
    }

    function form_fields($values = array(), $related = array())
    {
    // get the default fields first
    $fields = parent::form_fields($values, $related);

    // Select Subscribers Table & Join
    $fields = $this->db->select('*');
    $fields = $this->db->from('fuel_subscribers'); //THIS JOIN ISNT WORKING
    $fields = $this->db->join('subscriber_email', 'fuel_subscribers.id = subscriber_email.id');

    // get the options list which is just a name/value associative array
    $email_options = $this->options_list('subscriber_email', 'subscriber_email', array('subscriber_active' => 'TRUE'));

    $group_options = $this->options_list('subscriber_groups', 'subscriber_groups');

    // now change the default field from a text field to a dropdown passing it the $email_options
    $fields['subscriber_email'] = array('type' => 'select', 'options' => $email_options, 'first_option' => 'Select an email address...');
    $fields['subscriber_groups'] = array('type' => 'select', 'options' => $group_options, 'first_option' => 'Select a group...');
    return $fields;

    }

    function on_before_validate($values=array()) {
    return $values;
    }
    }

    class Subscriber_item_model extends Base_module_record {

    }

    I have a fuel_subscribers table & fuel_subscribe table both with

    id
    subscriber_email
    subscriber_groups enum('Sales','Marketing','Customer','Distributers')
    message <- This only in fuel_subscribe table
    subscriber_active
  • edited 7:21PM
    By the way this is the error I get for the above:

    Error Number: 1146

    Table 'int.subscriber_email' doesn't exist

    SELECT *, subscriber_email, subscriber_email FROM (`fuel_subscribers`, fuel_subscribe) JOIN `subscriber_email` ON `fuel_subscribers`.`id` = `subscriber_email`.`id` WHERE `subscriber_active` = 'TRUE' ORDER BY `subscriber_email` asc

    Filename: core/MY_Model.php

    Line Number: 685
  • edited 7:21PM
    Assigning $fields to the value of what is returned by the $this->db calls will nullify the default values for $fields above... I'd recommend removing this:
    // Select Subscribers Table & Join $fields = $this->db->select('*'); $fields = $this->db->from('fuel_subscribers'); //THIS JOIN ISNT WORKING $fields = $this->db->join('subscriber_email', 'fuel_subscribers.id = subscriber_email.id');
    Also, this line:
    $fields = $this->db->from('fuel_subscribers'); //
    Is it "fuel_subscribers" OR "fuel_subscribe"? The __constructor uses the latter.

    Are you wanting to join the tables or do you actually want to import the other model and just use that model instead of joining the tables?
  • edited 7:21PM
    Thanks, I actually did it another way and works fine.

    My next question would be im building a full/massive e-commerce site for my client and wondered how eaasy this would be and can you expand on on CI by using/adding controller/function/params, for example:

    Can you still expand on fuelCMS by using/adding your own contollers to control certain things along with extending the classes?
  • edited 7:21PM
    You should be able to use Controllers just like you can with normal CI site. We've built them before using an advanced module that contains simple modules for the products, categories, orders, etc.
Sign In or Register to comment.