5
\$\begingroup\$

I work with a legacy application. This is my attempt to add some sanity in an new feature I want to add.

It's a social application with users, sessions etc. The entire application is run in a procedural fashion. It still uses MySQL, so once this feature is complete, upgrading to PDO will be my next project.

class Groups {


    protected   $_db;
    protected $member_id;

    protected $group_id,
                      $group_title,
                        $group_about,
                        $group_join_us,
                        $group_contact,
                        $group_rss_url,
                        $group_rss_post_limit,
                        $group_privacy;

    protected $group_super_admin;

    protected $group_approve_status,
                        $group_approve_date;

    protected $group_meta_title,
                        $group_meta_description,
                        $group_meta_keywords;


    public function __construct($db, $member_id) { 

        $this->_db = $db;
        $this->member_id = $member_id;
    }

    /** 
     * Set the group title
     *
     * @param str $group_id
     * 
     */
    public function set_group_id($group_id) {

        $this->group_id = $group_id;    

    }

    /** 
     * Set the group title
     *
     * @param int $group_id
     * 
     */
    public function get_group_id() {

        return $this->group_id; 

    }

    /** 
     * Set the group title
     *
     * @param str $group_id
     * 
     */
    public function set_group_title($group_title) {

        $this->group_title = $group_title;  

    }

    /** 
     * get the group title
     *
     * @return int $group_id
     * 
     */
    public function get_group_title() {

        return $this->group_title;

    }

    /** 
     * Set the group about
     *
     * @param str $group_id
     * 
     */
    public function set_group_about($group_about) {

        $this->group_about = $group_about;  

    }

    /** 
     * Get the group about
     *
     * @param str $group_id
     * 
     */
    public function get_group_about() {

        return $this->group_about;  

    }

    /** 
     * Set the group join us
     *
     * @param str $group_join
     * 
     */
    public function set_group_join_us($group_join_us) {

        $this->group_join_us = $group_join_us;

    }

    /** 
     * Get the group join us
     *
     * @return str join us content
     * 
     */
    public function get_group_join_us() {

        return $this->group_join_us;    

    }


    /** 
     * Set the group contact
     *
     * @return str $group_contact
     * 
     */
    public function set_group_contact($group_contact) {

        $this->group_contact = $group_contact;  

    }

    /** 
     * Get the group contact
     *
     * @return str $group_contact
     * 
     */
    public function get_group_contact() {

        return $this->group_contact;    

    }

    /** 
     * Set the group rss
     *
     * @param str $group_rss
     * 
     */
    public function set_group_rss($rss_url) {

        $this->group_rss_url = $rss_url;    

    }

    /** 
     * Get the group rss url
     *
     * @return str $group_rss
     * 
     */
    public function get_group_rss() {

        return $this->group_rss_url;    

    }

    /** 
     * Set the group rss post limit
     *
     * @param int $group_rss
     * 
     */
    public function set_group_rss_post_limit($group_rss_post_limit) {

        $this->group_rss_post_limit = $group_rss_post_limit;    

    }

    /** 
     * get the group rss post limit
     *
     * @return int $rss_post_limit
     * 
     */
    public function get_group_rss_post_limit() {

        return $this->group_rss_post_limit; 

    }


    /** 
     * Set the group description
     *
     * @param str $group_description
     * 
     */
    public function set_group_description($group_description) {

        $this->group_description = $group_description;  

    }

    /** 
     * Get the group description
     *
     * @return str $group_description
     * 
     */
    public function get_group_description() {

        return $this->group_meta_description;   

    }


    /** 
     * Set the group keywords
     *
     * @param str $group_keywords
     * 
     */
    public function set_group_keywords($group_keywords) {

        $this->group_meta_keywords = $group_keywords;   

    }

    /** 
     * Get the group keywords
     *
     * @return str $group_keywords
     * 
     */
    public function get_group_keywords() {

        return $this->group_meta_keywords;  

    }

    /** 
     * Set the group meta title
     *
     * @param str $group_keywords
     * 
     */
    public function set_group_meta_title($group_meta_title) {

        $this->group_meta_title = $group_meta_title;    

    }

    /** 
     * Get the group meta title
     *
     * @return str group meta title
     * 
     */
    public function get_group_meta_title() {

        return $this->group_meta_title; 

    }

    /** 
     * Set the group meta title
     *
     * @param tinyint bool 0 / 1 
     * 
     */
    public function set_group_approve_status($group_approve_status) {

        $this->group_approve_status = $group_approve_status;    

    }

    /** 
     * save group status
     *
     * @param tinyint bool 0 / 1 
     * 
     */
    public function update_group_approve_status($group_approve_status) {

        $sql = "UPDATE `groups` SET
                        `approved`  =   ".sql_c(intval($group_approve_status)).",
                        `date_approved` = NOW()
                        WHERE
                        `id`    =   ".sql_c($this->group_id)."";    
        $result = @mysql_query($sql,$this->_db); check_sql(mysql_error(), $sql, 0);

        $this->set_group_approve_status($group_approve_status);

        return $result;

    }


    /** 
     * Get the group meta title
     *
     * @return str group meta title
     * 
     */
    public function get_group_approve_status() {

        return $this->group_approve_status; 

    }

    /** 
     * Set the group super admin
     *
     * @param int $super_admin_id
     * 
     */
    public function set_group_super_admin($super_admin_id) {

        $this->group_super_admin = $super_admin_id;

    }

    /** 
     * Get the group super admin
     *
     * @return int super admin id
     * 
     */
    public function get_group_super_admin() {

        return $this->group_super_admin;    

    }

    /** 
     * Set the group group privacy
     *
     * @param int $private_group
     * 
     */
    public function set_group_privacy($group_privacy) {

        $this->group_privacy = $group_privacy;

    }

    /** 
     * Get the group privacy status
     *
     * @return int group privacy
     * 
     */
    public function get_group_privacy() {

        return $this->group_privacy;    

    }


    /** 
     * save new group
     *
     * @param 
     * 
     */
    public function insert_new_group() {

        $sql = "INSERT INTO `groups` SET
                        `added_by_member_id`    =   ".sql_c($this->member_id).",
                        `date_added`                    = NOW(),
                        `super_admin_id`            =   ".sql_c($this->member_id).",
                        `private_group`             =   ".intval($this->group_privacy).", ## All groups are private by default
                        `title`                             =   '".sql_c($this->group_title)."',
                        `about_us`                      =   '".mysql_real_escape_string($this->group_about)."',
                        `join_us`                           =   '".mysql_real_escape_string($this->group_join_us)."',
                        `contact_us`                    =   '".mysql_real_escape_string($this->group_contact)."',
                        `rss_url`                           =   '".sql_c($this->group_rss_url)."',
                        `rss_post_limit`            =   ".sql_c($this->group_rss_post_limit).",
                        `meta_title`                    =   '".sql_c($this->group_title)."',
                        `meta_keywords`             =   '', ##keywords can come later
                        `meta_description`      =   '".sql_c($this->group_description)."'
                        ";  
        $result = @mysql_query($sql,$this->_db); check_sql(mysql_error(), $sql, 0);

        $group_id = mysql_insert_id();

        $this->set_group_id($group_id);

        return $result;

    } 

    /** 
     * build existing group by id
     *
     * @param str $group_id
     * 
     */
    public function fetch_group_by_id($group_id) {

        $sql = "SELECT * FROM `groups` WHERE `id` = ".sql_c($group_id)."
                        AND (`temp` != 'on' OR `temp` IS NULL)
                        AND (`backup` != 'on' OR `backup` IS NULL)";
        $result = @mysql_query($sql,$this->_db); check_sql(mysql_error(), $sql, 0);

        $list = mysql_fetch_array($result);

        $this->set_group_id($list['id']);

        //set all group properties from this group
        $this->set_group_title($list['title']);

        $this->set_group_about($list['about_us']);

        $this->set_group_join_us($list['join_us']);

        $this->set_group_contact($list['contact_us']);

        $this->set_group_rss($list['rss_url']);

        $this->set_group_rss_post_limit($list['rss_post_limit']);

        $this->set_group_description($list['meta_description']);        

        $this->set_group_keywords($list['meta_keywords']);

        $this->set_group_meta_title($list['meta_title']);

        $this->set_group_approve_status($list['approved']);

        $this->set_group_super_admin($list['super_admin_id']);

        $this->set_group_privacy($list['private_group']);



    }   



    //$group_contact
} //EO class Group

This being my first steps into OOP? I would appreciate some basic advice and tips. My problem is that, although I understand the OOP syntax, its hard for me to build anything practical with it. Maybe it's just lack of experience.

\$\endgroup\$
4
\$\begingroup\$

Yes, you are most certainly on the right path, and as a first step, it's very good. Some pointers for the future.

  • Don't use mysql_* - I know you said you're already planning to. I'm mainly saying this for future users.
  • Objects work best with other objects - If you have groups, it makes sense to have a User class, etc.
  • Code style - Make sure your spacing and indentation are uniform throughout the file, and throughout the application in general.
  • Naming - By the name Groups I understand that this class is a collection of single Group objects, that doesn't seem to be the case, so this class should be called Group.
  • Separation of concerns - Your Group shouldn't know where it came from. It shouldn't be aware of the database, or know how to fetch itself. Another object, GroupMapper should be responsible for that, and have methods like fetchGroupDetails($group).

None of the above issues are critical (except for mysql_* maybe), you're doing a good job!

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks for the reply. I will keep those points in mind as I expand this feature. \$\endgroup\$ – Andy Jun 10 '14 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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