Professional Documents
Culture Documents
Isys6307 LN10 R1
Isys6307 LN10 R1
OUTLINE MATERI :
1. Introduction
2. Model
3. View
4. Control
5. Dashboard
ISI MATERI
A. Introduction
Seiring dengan perkembangan teknologi, dan banyaknya programmer PHP yang
berkontribusi sehingga terjadi persamaan persepsi diantara programmer maka framework
programming PHP dapat diluncurkan. Framework PHP memiliki sifat MVC yang telah
dijelaskan pada materi sebelumnya.
B. Model
Supaya bahasa pemrograman bisa terkoneksi ke database pada framework
dinamakan model. Berikut contoh source model untuk master barang sebagai berikut:
<?php
/**
* This is the model class for table "barang".
*
* The followings are the available columns in table 'barang':
* @property string $kode_barang
* @property string $nama_barang
* @property integer $satuan
* @property integer $stok_akhir
* @property integer $harga
* @property integer $penggunaan_tahun
* @property integer $safety_stok
* @property integer $rop
* @property integer $eoq
*/
class Barang extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Barang the static model class
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'barang';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those
attributes that
// will receive user inputs.
return array(
array('kode_barang, nama_barang, satuan,
stok_akhir, harga, penggunaan_tahun, safety_stok, rop, eoq',
'required'),
array('satuan, stok_akhir, harga,
penggunaan_tahun, safety_stok, rop, eoq', 'numerical',
'integerOnly'=>true),
array('kode_barang', 'length', 'max'=>25),
array('nama_barang', 'length', 'max'=>50),
// The following rule is used by search().
// Please remove those attributes that should
not be searched.
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and
the related
// class name for the relations automatically
generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'kode_barang' => 'Kode Barang',
'nama_barang' => 'Nama Barang',
'satuan' => 'Satuan',
'stok_akhir' => 'Stok Akhir',
'harga' => 'Harga',
'penggunaan_tahun' => 'Penggunaan/Tahun',
'safety_stok' => 'Safety Stok',
'rop' => 'Rop',
'eoq' => 'Eoq',
);
}
$criteria=new CDbCriteria;
$criteria->compare('kode_barang',$this-
>kode_barang,true);
$criteria->compare('nama_barang',$this-
>nama_barang,true);
$criteria->compare('satuan',$this->satuan);
$criteria->compare('stok_akhir',$this->stok_akhir);
$criteria->compare('harga',$this->harga);
$criteria->compare('penggunaan_tahun',$this-
>penggunaan_tahun);
$criteria->compare('safety_stok',$this->safety_stok);
$criteria->compare('rop',$this->rop);
$criteria->compare('eoq',$this->eoq);
C. View
View pada framework terdiri dari beberapa file seperti file tampilan utama, file
form, dll. Source code untuk file index.php, sebagai berikut
<?php
$this->breadcrumbs=array(
'Barangs',
);
$this->menu=array(
array('label'=>'Create Barang', 'url'=>array('create')),
array('label'=>'Manage Barang', 'url'=>array('admin')),
);
?>
<h1>Barangs</h1>
$this->breadcrumbs=array(
'Barangs'=>array('index'),
$model->kode_barang,
);
$this->menu=array(
array('label'=>'List Barang', 'url'=>array('index')),
array('label'=>'Create Barang', 'url'=>array('create')),
array('label'=>'Update Barang', 'url'=>array('update',
'id'=>$model->kode_barang)),
array('label'=>'Delete Barang', 'url'=>'#',
'linkOptions'=>array('submit'=>array('delete','id'=>$model-
D. Control
Supaya data yang diinput melalui view memiliki validitas yang baik dan sesuai
dengan atribut tabelnya, diperlukan control. Salah satu control yang dibuat untuk barang
sebagai berikut:
<?php
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access
control for CRUD operations
'postOnly + delete', // we only allow
deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl'
filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to
perform 'index' and 'view' actions
/**
* Displays a particular model.
* @param integer $id the ID of the model to be
displayed
*/
public function actionManage()
{
$this->layout = 'columna';
$model=new Barang('search');
$model->unsetAttributes(); // clear any
default values
if(isset($_GET['Barang']))
$model->attributes=$_GET['Barang'];
$this->render('manage',array(
'model'=>$model,
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be
redirected to the 'view' page.
*/
public function actionCreate()
{
$this->layout = 'columna';
$model=new Barang;
$bp=YII::app()->db->createCommand("SELECT
BIAYA_PEMESANAN FROM RULES LIMIT 1")->queryScalar();
$bpe=YII::app()->db->createCommand("SELECT
BIAYA_PENYIMPANAN FROM RULES LIMIT 1")->queryScalar();
$lt=YII::app()->db->createCommand("SELECT
LEAD_TIME FROM RULES LIMIT 1")->queryScalar();
// Uncomment the following line if AJAX
validation is needed
// $this->performAjaxValidation($model);
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be
redirected to the 'view' page.
* @param integer $id the ID of the model to be
updated
*/
public function actionUpdate($id)
{
$this->layout = 'columna';
if(isset($_POST['Barang']))
{
$model->attributes=$_POST['Barang'];
$model->attributes=$_POST['Barang'];
$rop = $model->safety_stok + ($model-
>penggunaan_tahun/365) * $lt;
$model->rop = round($rop,
0,PHP_ROUND_HALF_UP);
$r_eoq = ( 2 * $model->penggunaan_tahun
* (($bp/100) * $model->harga)) / (($bpe/100) * $model-
>harga);
$model->eoq = round(sqrt($r_eoq), 0,
PHP_ROUND_HALF_UP);
if($model->save())
$this->redirect(array('manage'));
}
$this->render('update',array(
'model'=>$model,
/**
* Deletes a particular model.
* If deletion is successful, the browser will be
redirected to the 'admin' page.
* @param integer $id the ID of the model to be
deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new
CActiveDataProvider('Barang');
$this->render('index',array(
'dataProvider'=>$dataProvider,
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Barang('search');
$model->unsetAttributes(); // clear any
default values
if(isset($_GET['Barang']))
$model->attributes=$_GET['Barang'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary
key given in the GET variable.
* If the data model is not found, an HTTP
exception will be raised.
* @param integer $id the ID of the model to be
loaded
* @return Barang the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
/**
* Performs the AJAX validation.
* @param Barang $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) &&
$_POST['ajax']==='barang-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
E. Dashboard
Supaya memudahkan dalam pembacaan data lebih mudah dibuat informasi dalam
bentuk grafik atau dashboard. Sourcecodenya sebagai berikut:
<style type="text/css">
body{
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-
serif !important;
}
</style>
<div class="indexx">
<div class="row">
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
<h3><?php $tot=YII::app()->db-
>createCommand("SELECT COUNT(KODE_BARANG) from BARANG")->queryScalar();
echo $tot ?></h3>
<p>Barang</p>
</div>
<div class="icon">
<i class="fa fa-cubes"></i>
</div>
<a href="index.php?r=barang/manage" class="small-box-
footer">
Detail <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-green">
<div class="inner">
<h3><?php $tot2=YII::app()->db-
>createCommand("SELECT COUNT(KODE_BARANG) from BARANG_MASUK")-
>queryScalar();
echo $tot2 ?></h3>
<p>Barang Masuk</p>
</div>
<div class="icon">
<p>Barang Keluar</p>
</div>
<div class="icon">
<i class="fa fa-external-link"></i>
</div>
<a href="index.php?r=barangKeluar/manage"
class="small-box-footer">
Detail <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-red">
<div class="inner">
<h3><?php $tot4=YII::app()->db-
>createCommand("SELECT COUNT(USER_ID) from user")->queryScalar();
echo $tot4?></h3>
<div class="row">
<div class="col-lg-8" style="padding-left:
25px;padding-right: 25px;">
<div class="panel panel-default">
<!-- /.panel-heading -->
<div class="panel-body">
<?php
$sqlbar="SELECT * FROM BARANG";
$rsqlbar=Barang::model()-
>findAllBySql("$sqlbar");
foreach ($rsqlbar as $rb) {
if ($rb->stok_akhir <= $rb->rop){ ?>
<div class="alert alert-warning"
style="margin-bottom: 5px;">
<strong>Warning!</strong> Barang dengan kode
<?php echo '<a href=index.php?r=barang/manage >'.$rb-
>kode_barang.'</a>' ?> harus di order kembali, <a
href="mailto:supplier@gmail.com">Klik</a> untuk email supplier!
</div>
<?php }else{
$sql2="SELECT nama_barang,
stok_akhir AS jumlah FROM BARANG GROUP BY nama_barang";
$rsql2=Barang::model()-
>findAllBySql("$sql2");
foreach($rsql2 as $c=>$ck)
{
$label[$c]=$ck-
>nama_barang;
$nilai[$c]=(int)$ck-
>jumlah;
'legend'=>array('enabled'=>true),
'xAxis'=>array('categories'=>$label,
'title'=>array('text'=>''),),
'yAxis'=> array(
'min'=> 0,
'title'=> array(
'text'=>'jumlah'
),
),
'series' => array(
array('data' => $nilai)
),
'tooltip' =>
array('formatter' => 'js:function(){ return "<b>"+this.point.name+"</b>
:"+this.y; }'),
'tooltip' => array(
'formatter' => 'js:function()
{return "<b>"+ this.x +"</b><br/>"+"Jumlah : "+ this.y; }'
),
'plotOptions'=>array('pie'=>(array(
'allowPointSelect'=>true,
'showInLegend'=>true,
'credits'=>array('enabled'=>false),
)
));
?>
</tr>
</thead>
<!-- /.row -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-8 -->
<div class="col-lg-4">
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-bell fa-fw"></i> 10 Barang
Masuk Terakhir
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="list-group">
<?php
$sqlf='SELECT * FROM BARANG_MASUK ORDER BY
TANGGAL DESC LIMIT 10';
$rsqlf=BarangMasuk::model()-
>findAllBySql("$sqlf");
foreach ($rsqlf as $rsqlf) {
?>
<a href="index.php?r=BarangMasuk/manage"
class="list-group-item" target="_blank">
Gambar 10.1
Doug Bierer, Altaf Hussain, Branko Ajzele. (2016). PHP 7: Real World Application
Development. Published by Packt Publishing Ltd. Livery Place 35 Livery Street. Birmingham
B3 2PB, UK. ISBN 978-1-78712-900-9