Welcome Guest, Not a member yet? Register   Sign In
Ask about library
#1

(This post was last modified: 11-05-2014, 10:33 AM by bobykurniawan.)

Hello from indonesia. I try to make my own library (Haha i don't if it is right or not). Okey first, i'll put the code's

PHP Code:
class Donaturlib {


   function 
is_admin()
    {            
        
$ci     =&get_instance();
            
$cek $ci->donaturlib->security();    
                        if(
$cek != null)
                        {
                                       foreach (
$cek as $key) {
                                       
$username =  $key->username;
                                       }
                                       
$hasil $ci->donaturlib->getPeople($username);
                                       foreach (
$hasil as $levelnya ) {
                                           
$lv $levelnya->level;
                                       }
                                       if (
$lv != 1) {
                                           return 
false;
                                       }
                                       else
                                       {
                                           return 
true;
                                       }
                        }else{
        
$msg="<div class='alert alert-danger alert-dismissible' role='alert'>
  <button type='button' class='close' data-dismiss='alert'>
  <span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>
  <strong>Harap login terlebih dahulu
</div>"
;
        
$ci->session->set_flashdata('donatur',$msg);
        
redirect('welcome','refresh');
                        }

   }
    function 
getPeople($username)
    {
            
$ci     =&get_instance();
            
$query  =   $ci->db->query("select level from user where username ='$username'");
            return 
$query->result();
    }
    function 
checkexist($namadonasi,$tabel1)
    {
            
$ci         =   &get_instance();
            
$fieldpilih    ='*'
            
$hasil         $ci->modeldonatur->pilihdata($tabel1,$namadonasi,$fieldpilih);    
            if(
$hasil!=null)
            {
                return 
true;
            }
            else
            {
                return 
false;
            }
    }
    function 
security()
        {
            
$ci     =&get_instance();
            
$vCuser     $ci->input->cookie('dnt_user');
            
$vCsesi     $ci->input->cookie('dnt_sesi');
            
$tbl='logindata';
            
$fieldpilih='*'
            
$fieldkondisi=array('username'=>$vCuser,'sesi'=>$vCsesi);             
            
$hasil      $ci->modeldonatur->pilihdata($tbl,$fieldkondisi,$fieldpilih);
            
            if(
$hasil!=null)
            {
            
//simpan ke cookie
            
$vCookie1['name']   = 'ik_inv_ck_user';
            
$vCookie1['value']  = $vCuser;
            
$vCookie1['expire'] = '600';
            
$ci->input->set_cookie($vCookie1);
            
            
//simpan ke cookie token/sesi   
            
$vCookie2['name']   = 'ik_inv_ck_sesi';
            
$vCookie2['value']   = $vCsesi;
            
$vCookie2['expire']  = '600';
            
$ci->input->set_cookie($vCookie2);
            }
            
            return 
$hasil;
        }

 } 

The code is working fine, but my real problems are :
1. What is the function of library ?
2. Is my library right ?
3. If right, what the different with models ?

Sory, for my bad english.
Reply
#2

0. Use English identifiers, otherwise it is it is very hard reading code. "Donaturlib" name does not tell me anything about he purpose of this library.

1. See http://www.codeigniter.com/userguide3/ge...aries.html This is the formal, technical explanation about libraries. From your perspective there are two main reasons for making libraries: just to collect code within relatively independent methods; or to use OOP - then you have classes that are aspect focused, the methods serve a concrete functionality. CodeIgniter libraries at the moment follow the singleton pattern (other instances may be created under different property names), they also have a conventional way to access in initialization specific to them configuration options.

2. There is room for criticism about your library. At the moment it is small, but it tends to do many things alone. Try to split the concerns, let this library have a singlepurpose. For example, the getPeople($username) method IMO should be in a separate model that deals with the users. You have HTML code inside the method is_admin(), the place is not right, HTML usually is within views. The method security() seems pretty exotic. It is not clear why do you use cookies directly, also, I don't see encryption/decryption of these cookies.

3. The difference between models and libraries is technical. Models don't have the same access on initialization to the configuration options as libraries have, but models have a property for accessing the database. Semantically there may be no difference, you may implement business logic as a model (usually) or as a library. A shopping cart could be a library or it could be a model.

Here I share my subjective perception, of course.
Reply
#3

Thank for replying. I use bahasa indonesia .

1. Okey, because in 'old place' i have troubled to find the definition

2. What is the IMO things?. The method security is using some randstring.

3. Oh oke i get it.

Do you mind if i ask your email ? so i can send you my script. But, i'll fix my script first.
Reply
#4

Sorry for the acronym,

IMO = "in my opinion"
Reply
#5

(This post was last modified: 11-06-2014, 08:38 PM by bobykurniawan.)

another question from me.

If i want to give 'flashdata' but the flashdata is html like above , where should i put it ?

sorry if i to much asking, i really want to understand it
Reply
#6

This is a good question. I have a little bit dirty approach, but it is practical and saves time.

Within your "views/" folder make a file "feedback_messages.php" with the following sample content:

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

$normal_message = isset($normal_message) && trim($normal_message) != '' $normal_message trim(get_instance()->session->flashdata('normal_message'));
$confirmation_message = isset($confirmation_message) && $confirmation_message != '' $confirmation_message trim(get_instance()->session->flashdata('confirmation_message'));
$warning_message = isset($warning_message) && $warning_message != '' $warning_message trim(get_instance()->session->flashdata('warning_message'));
$error_message = isset($error_message) && $error_message != '' $error_message trim(get_instance()->session->flashdata('error_message'));

?>

                <!-- Feedback messages -->
                <div id="main_feedback_messages" class="clearfix">

<?php

if ($normal_message) {
?>

                        <div class="alert alert-info alert-dismissable centered-block">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <?php echo $normal_message?>
                        </div>

<?php
}

if (
$confirmation_message) {
?>

                        <div class="alert alert-success alert-dismissable centered-block">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <?php echo $confirmation_message?>
                        </div>

<?php
}

if (
$warning_message) {
?>

                        <div class="alert alert-warning alert-dismissable centered-block">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <?php echo $warning_message?>
                        </div>

<?php
}

if (
$error_message) {
?>

                        <div class="alert alert-danger alert-dismissable centered-block">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <?php echo $error_message?>
                        </div>

<?php
}

?>

                </div>

    <script type="text/javascript">
    //<![CDATA[

    function show_main_feedback_message(message, message_type) {

        if (typeof message != 'undefined' && message != '') {

            if (typeof message_type == 'undefined' || message_type == '') {
                message_type = 'normal_message';
            }

            var visual_class = 'alert alert-info alert-dismissable centered-block';

            switch (message_type) {

                case 'confirmation-message':
                case 'confirmation_message':
                    visual_class = 'alert alert-success alert-dismissable centered-block';
                    break;

                case 'warning-message':
                case 'warning_message':
                    visual_class = 'alert alert-warning alert-dismissable centered-block';
                    break;

                case 'error-message':
                case 'error_message':
                    visual_class = 'alert alert-danger alert-dismissable centered-block';
                    break;
            }

            $('#main_feedback_messages').html('<div class="' +  visual_class + '"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> ' +  message + ' </div>');
        }
    }

    function clear_main_feedback_message() {
        $('#main_feedback_messages').html('');
    }

    //]]>
    </script>
                <!-- Feedback messages end --> 

Then include directly this file within your view file that makes the final html layout (the html template of the pages), i.e. Include this file at the desired place within your html template.

This is a widget that shows feedback messages to the user. The messages may come from 3 different sources:
- directly passed into the view variables;
- flash data items
- activation by JavaScript - if your AJAX calls return feedback messages you can show them with this widget too.

You may pass text messages, of course you may pass simple HTML too, but you don't need to pass the design of the text messages again and again many times.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB