owen2345
6/24/2014 - 4:47 PM

php code snippet

php code snippet

 
        <div class='panel_listado simplebox'>
            <div class="titleh"><h3>List Company</h3></div>
            <div class="body">            
                <table id='listado_Company'>
                    <thead>
                        <tr>
                            
                            <th>Company name</th>
                            <th>Email company</th>
                            <th>Status company</th>
                            <th>Description company</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php  if(count($Companys)): ?>
                            <?php  foreach($Companys as $Company): ?>
                                <tr>
                                    <td><?php  echo $Company->name ?></td>
                                    <td><?php  echo $Company->email ?></td>
                                    <td><?php  echo $Enableds[$Company->enabled] ?></td>
                                    <td><?php  echo $Company->description ?></td>
                                    <td class='actions'>
                                        <a href='<?php echo ROOT_PATH ?>Company/edit/<?php echo $Company->id ?>' class='editar hg-yellow' title='Edit item'>Edit</a>
                                        <a href='<?php echo ROOT_PATH ?>Company/delete/<?php echo $Company->id ?>' title='Delete item' onclick="var d = confirm('Are you sure delete this Item?'); return d;" class='eliminar hg-red'>Delete</a>
                                    </td>
                                </tr>
                            <?php  endforeach ?>
                        <?php  else: ?>
                            <tr>
                                <td colspan='99'>Not found Company items</td>
                            </tr>
                        <?php  endif ?>
                    </tbody>
                </table>
                <div class='panel_controls padding20'>
                    <a id='btn_registrar' class='button-green' href='<?php echo ROOT_PATH ?>Company/create'>Create new</a>
                </div>
            </div>
        </div>
            
<?php
class Task extends FD_ManageModel
{	
    var $alias_of_atributes = array();
    var $fd_rules = array();
	var $fd_primary_key = 'id_task';
    
	var $id_task; 
	var $id_prioritytask; 
	var $id_typetask; 
	var $id_statustask; 
	var $id_sprint; 
	var $id_backlog; 
	var $starttime_task; 
	var $endtime_task; 
	var $title_task; 
	var $descr_task; 
	var $isextratime_task; 
	var $owner_task; 
	var $stimatehour_task; 
	var $workedhour_task; 
	var $continueof_task; 
	var $weight_task; 
	var $createat_task; 
    	var $error_message = '';
	
	function Task($id_task = '', $id_prioritytask = '', $id_typetask = '', $id_statustask = ID_OPEN_TASK, $id_sprint = '', $id_backlog = '', $starttime_task = '', $endtime_task = '', $title_task = '', $descr_task = '', $isextratime_task = '', $owner_task = '', $stimatehour_task = '', $workedhour_task = '', $continueof_task = '', $weight_task = '', $createat_task = '')
	{
		
		$this->id_task = $id_task;
		$this->id_prioritytask = $id_prioritytask;
		$this->id_typetask = $id_typetask;
		$this->id_statustask = $id_statustask;
		$this->id_sprint = $id_sprint;
		$this->id_backlog = $id_backlog;
		$this->starttime_task = $starttime_task;
		$this->endtime_task = $endtime_task;
		$this->title_task = $title_task;
		$this->descr_task = $descr_task;
		$this->isextratime_task = $isextratime_task;
		$this->owner_task = $owner_task;
		$this->stimatehour_task = $stimatehour_task;
		$this->workedhour_task = $workedhour_task;
		$this->continueof_task = $continueof_task;
		$this->weight_task = $weight_task;
		$this->createat_task = $createat_task;
	}
    
    /**
     * return total hours worked
     */
    function getHours()
    {
        $hours = 0;
        /*foreach($this->references("fase_changed") as $FC)
        {
            $hours += $FC->hoursdedicated_fasechanged;
        }
        return $hours;*/
        foreach($this->references("hour_work") as $Work)
            $hours += $Work->qty_hourwork;
                
        foreach($this->getChildren() as $Child)
            $hours += $Child->getHours();
                
        return $hours;
    }
    
    function getParent()
    {
        $FD = getInstance();
        return $FD->Connection->DB->get_object_by_id("task", $this->owner_task);
    }
    
    function getContinueOf()
    {
        $FD = getInstance();
        return $FD->Connection->DB->get_object_by_id("task", $this->continueof_task);
    }
    
    function getChildren()
    {
        $FD = getInstance();
        return $FD->Connection->DB->get_objects("task", "owner_task='".$this->id_task."'");
    }
    
    /**
     * return array of asign_project objects
     */
    function getUsersAsigned()
    {
        $FD = getInstance();
        //return $FD->Connection->DB->get_objects_By_Sql("asign_project", "SELECT * FROM company_user JOIN asign_project ON company_user.id=asign_project.id JOIN task_asigned ON asign_project.id_asign=task_asigned.id_asign where task_asigned.id_task = '".$this->id_task."'");
        return $FD->Connection->DB->get_objects_By_Sql("asign_project", "SELECT * FROM company_user as cu, asign_project as ap, task_asigned as ta where cu.id=ap.id and ap.id_asign=ta.id_asign and ta.id_task = '".$this->id_task."' and ta.isdisable_taskasign=0 group by cu.id");
    }
    
    /**
     * retorna los usuarios que no estan asignados a la subtarea y asignados a la tarea principal
     * retorna un array de Asign_project objects
     * NOTA: solo funciona en subtareas
     */
    function getSubTaskUnAsignedUsers()
    {
        $parentAsigns = $this->getParent()->getUsersAsigned();
        $childAsigns = $this->getUsersAsigned();
        $res = array();
        $tmp = array();
        foreach($childAsigns as $AP)
            $tmp[] = $AP->id_asign;
            
        foreach($parentAsigns as $Asign_project)
        {
            if(!in_array($Asign_project->id_asign, $tmp))
                $res[] = $Asign_project;
        }
        
        return $res;
    }
    
    function getUsersUnAsigned()
    {
        $FD = getInstance();
        $res = array(-9999);
        foreach($this->getUsersAsigned() as $User)
        {
            $res[] = $User->id;
        }
        $Project = $this->getProject();
        if(!$Project)
            dieFastDevel("Error on get UnAsigned Users");
            
        //return $FD->Connection->DB->get_objects_By_Sql("asign_project", "SELECT * FROM company_user JOIN asign_project ON company_user.id=asign_project.id JOIN task_asigned ON asign_project.id_asign=task_asigned.id_asign where asign_project.id_project = '".$Project->id_project."'");
        return $FD->Connection->DB->get_objects_By_Sql("asign_project", "SELECT * FROM company_user as cu, asign_project as ap where cu.id=ap.id and ap.id_project = '".$Project->id_project."' and cu.id!=".join(" and cu.id != ", $res)." group by cu.id");
    }
    
    function getProject()
    {
        $FD = getInstance();
        if($this->owner_task)
            return $FD->Connection->DB->get_object_by_id("task", $this->owner_task)->getProject();
        
        $Sprint = $this->getSprint();
        if($Sprint)
            return $Sprint->foreing_key("project");
        elseif($p1 = $this->foreing_key("backlog"))
        {
            return current($p1->references("project"));
        
        }else
        {   
            fd_log("Task \"".$this->id_task."\", Can't be found the Project");
            return false;
        }
            
    }
    
    function getEnableAsigns()
    {
        return $this->references("task_asigned", "isdisable_taskasign=0");
    }
    
    function disableAsigns()
    {
        foreach($this->references("task_asigned") as $Ta)
        {
            $Ta->setAttr("isdisable_taskasign", 1)->update();
        }
    }
    
    function getAsigns()
    {
        return $this->references("task_asigned", "isdisable_taskasign=0");
    }
    
    function getSprint()
    {
        if($p = $this->getParent())
            return $p->getSprint();
        if($s = $this->foreing_key("sprint"))
            return $s;
        elseif($s1 = $this->foreing_key("backlog"))
        {
            $s2 = current($s1->references("sprint"));
            if($s2)
                return $s2;
        }
    }
    
    function getBacklog()
    {
        return $this->foreing_key("backlog");
    }
    
    function getUrl()
    {
        return ROOT_PATH."sprint/view/".$this->getSprint()->id_sprint."?task_id=".$this->id_task;
    }
    
    function getLink()
    {
        return "<a href='".$this->getUrl()."' title=''>$this->title_task</a>";
    }
    
    function toSprintBacklog($id_sprint)
    {
        $FD = getInstance();
        $Sprint = $FD->Connection->DB->get_object_by_id("sprint", $id_sprint);
        $this->id_sprint = null;
        $this->id_backlog = @$Sprint->id_backlog;
        $this->update();
        $Current_phasechanged = current($this->references("fase_changed", "current_fasechanged = 1"));
        if($Current_phasechanged)
        {
            $this->create_object("fase_changed", array("current_fasechanged"=>1, "previousid_fasechanged"=>$Current_phasechanged->id_fasechanged))->save();
            $Current_phasechanged->setAttr("current_fasechanged", 0)->update();
        }
        
        /** log */
        $FD->DB->create_object("log",
            array(
                "type_log"=>"task",
                "title_log"=>"Task \"$this->title_task\" moved to Sprint backlog.",
                "value_log"=>serialize($this),
                "action_log"=>"update",
                "id_project"=>isset($Sprint->id_project) ? $Sprint->id_project : $this->getSprint()->id_sprint,
                "id_sprint"=>$this->getSprint()?$this->getSprint()->id_sprint:"",
                "id_task"=>$this->getParent()?$this->getParent()->id_task:$this->id_task
            )
        )->save();
    }
    
    function backlogToFase($id_sprint, $id_fase)
    {
        $this->id_backlog = null;
        $this->id_sprint = $id_sprint;
        $this->update();
        
        $this->checkAsModified(null, "Backlog to in progress.");
        
        $Phase_changed = $this->create_object("fase_changed", $_POST)->setAttr("id_fase", $id_fase)->save();
        
        /** log */
        $FD = getInstance();
        $FD->DB->create_object("log", array("type_log"=>"task", "title_log"=>"Task \"$this->title_task\" moved to ".$Phase_changed->getPhaseName(), "value_log"=>serialize($this), "action_log"=>"update", "id_project"=>$this->getProject()->id_project, "id_sprint"=>$this->getSprint()?$this->getSprint()->id_sprint:"", "id_task"=>$this->getParent()?$this->getParent()->id_task:$this->id_task))->save();
    }
    
    function toProjectBacklog($id_project_backlog)
    {        
        $this->id_backlog = $id_project_backlog;
        $this->update();
        
        $FD = getInstance();
        $FD->DB->create_object("log", array("type_log"=>"task", "title_log"=>"Task \"$this->title_task\" moved to Project Backlog", "value_log"=>serialize($this), "action_log"=>"update", "id_project"=>$this->getProject()->id_project, "id_sprint"=>$this->getSprint()?$this->getSprint()->id_sprint:"", "id_task"=>$this->getParent()?$this->getParent()->id_task:$this->id_task))->save();
        return $this;
    }
    
    /**
     * mark the task as modified
     */
    function checkAsModified($date = null, $type_modif = "")
    {
        $Parent = $this->getParent();
        if(!$Parent)
            $Parent = $this;
        
        if(!$date)
            $date = date("Y-m-d");
        
        $TChanged = $Parent->getTChangeByDate($date);
        if(!$TChanged)
            $Parent->create_object("task_changes", array("date_taskchanges"=>$date, "type_taskchanges"=>$type_modif." -- ".$date))->save();
    }
    
    /**
     * find TChanged in this date
     * return Task changed
     */
    function getTChangeByDate($date)
    {
        $Parent = $this->getParent();
        if(!$Parent)
            $Parent = $this;
        return current($Parent->references("task_changes", "date_taskchanges = '$date'"));
    }
    
    /*
     * get all Task changes
     */
    function getTChanges()
    {
        $Parent = $this->getParent();
        if(!$Parent)
            $Parent = $this;
        return $Parent->references("task_changes");
    }
    
    
    function faseToFase($id_fase_to)
    {
        $this->checkAsModified(null, "Fase to Fase");
        
        $FD = getInstance();
        $Phase_changed = $this->create_object("fase_changed", array("id_fase"=>$id_fase_to, "id"=>$FD->Session->getUser()->id))->save();
        
        /** log */
        $FD = getInstance();
        $FD->DB->create_object("log", array("type_log"=>"task", "title_log"=>"Task \"$this->title_task\" moved to ".$Phase_changed->getPhaseName(), "value_log"=>serialize($this), "action_log"=>"update", "id_project"=>$this->getProject()->id_project, "id_sprint"=>$this->getSprint()?$this->getSprint()->id_sprint:"", "id_task"=>$this->getParent()?$this->getParent()->id_task:$this->id_task))->save();
        
        return $Phase_changed;
    }
    
    function sendNotifications($action = "save", $text = "")
    {
        $Main = $Parent = $this->getParent();
        if(!$Parent)
            $Main = $this;
        $Users = $Main->getUsersAsigned();
        $Project = $Main->getProject();
        foreach($Users as $User)
        {
            switch($action)
            {
                case "save":
                    if($Parent)
                        $User->foreing_key("company_user")->add_notification("Task child created", "A new child task for task \"$Parent->title_task\" on project \"$Project->title_project\" has registered:<br><p>$this->descr_task</p>", $Main->getUrl()."&tab=subtasks&id=".$this->id_task);
                    else
                        $User->foreing_key("company_user")->add_notification("Task created", "The task \"$this->title_task\" on project \"$Project->title_project\" has registered:<br><p>$this->descr_task</p>", $Main->getUrl());
                break;
                
                case "update":
                    if($Parent)
                        $User->foreing_key("company_user")->add_notification("Task child updated", "The child task for task \"$Parent->title_task\" on project \"$Project->title_project\" has updated:<br><p>$this->descr_task</p>", $Main->getUrl()."&tab=subtasks&id=".$this->id_task);
                    else
                        $User->foreing_key("company_user")->add_notification("Task updated", "The task \"$this->title_task\" on project \"$Project->title_project\" has updated:<br><p>$this->descr_task</p>", $Main->getUrl());
                break;
                
                case "delete":
                    if($Parent)
                        $User->foreing_key("company_user")->add_notification("Task child removed", "The child task for task \"$Parent->title_task\" on project \"$Project->title_project\" has removed:<br><p>$this->descr_task</p>", $Main->getUrl()."&tab=subtasks");
                    else
                        $User->foreing_key("company_user")->add_notification("Task removed", "The task \"$this->title_task\" on project \"$Project->title_project\" has removed:<br><p>$this->descr_task</p>", $Main->getSprint()->getURL());
                break;
                
                case "files":
                    $User->foreing_key("company_user")->add_notification("Task files updated", "The files for task \"$Main->title_task\" on project \"$Project->title_project\" has updated:<br> $text", $Main->getUrl()."&tab=files");
                break;
            }
            
        }
    }
    
    function getFiles()
    {
        return $this->references("document");
    }
    
    function onUpdate()
    {
        
    }
    
    function onDelete()
    {
        $FD = getInstance();
        foreach($FD->Connection->DB->get_objects("task", "owner_task='".$this->id_task."'") as $SubTask)
            $SubTask->delete();
        
        foreach($FD->Connection->DB->get_objects("task", "continueof_task='".$this->id_task."'") as $TaskContinueOf)
            $TaskContinueOf->delete();
        
        foreach($this->references("feedback") as $Feedback)
            $Feedback->delete();
        foreach($this->references("document") as $Document)
            $Document->delete();
        foreach($this->references("task_asigned") as $Ta)
            $Ta->delete();
        foreach($this->references("fase_changed") as $FC)
            $FC->delete();
        
    }
    
    function afterDelete()
    {
        
    }
    
    /**
     * Function for continue a Task in other sprint.
     * pending clone children tasks
    ****/
    function continueIn()
    {
        $Clone = $this;
        $Clone->title_task = "CONTINUE: ".$this->title_task;
        $Clone->continueof_task = $this->id_task;
        unset($Clone->id_task);
        $Clone->id_backlog = $this->getProject()->id_backlog;
        $Clone->id_sprint = null;
        $Clone->save();
    }
    
    function getCurrentPhase()
    {
        $tmp = $this->references("fase_changed", "current_fasechanged=1 and id_fase is not null");
        if(count($tmp))
            return current($tmp);
    }
    
    function getPhasesChanged($Fasechanged = null, $res = array())
    {
        if(!$Fasechanged)
            $Fasechanged = $this->getCurrentPhase();
        $From = $Fasechanged->getPreviousFase();
        $res[] = $Fasechanged;
        if($From)
            return $this->getPhasesChanged($From, $res);
        return $res;
    }
    
    function listUsers($join = ", ")
    {
        $res = array();
        foreach($this->getAsigns() as $Asign)
        {
            $User = $Asign->foreing_key('asign_project')->foreing_key("company_user");
            $res[] = "<a title='" . $User->name . "'>".$User->getAvatar(20,20)."</a>";
        }
        return join($join, $res);
    }
    
    function getActivities($quantity = 10)
    {
        $FD = getInstance();
        $res = array();
        foreach($FD->DB->get_objects("log", "type_log='task' or type_log='comment'", "createat_log DESC", "0, 1000") as $Log)
        {
            $Obj = @ (object) unserialize(base64_decode($Log->value_log));
            if(!$Obj)
                continue;
            switch($Log->type_log)
            {
                case "task":
                    if($Log->id_task == $this->id_task || @$Obj->owner_task == $this->id_task)
                        $res[] = $Log;
                break;
                
                case "comment":    
                    if($Log->id_task == $this->id_task)
                        $res[] = $Log;
                break;
            }
            if(count($res) == $quantity)
                break;
        }
        return $res;
    }
    
    function getComments()
    {
        return $this->references("feedback", null, "createat_feedback DESC");
    }
    
    /**
     * check if user is asigned to this task
     * $type_return: asign_project/task_asigned
     * return Asign_project object/false
     */
    function isAsigned($id_user, $type_return = "asign_project")
    {
        $FD = getInstance();
        $res = $FD->DB->get_objects_By_Sql($type_return, "select * from asign_project as ap, task_asigned as ta where ap.id='$id_user' and ta.id_asign=ap.id_asign and ta.id_task=$this->id_task and ta.isdisable_taskasign=0");
        if(count($res))
            return current($res);
        else
            return false;
    }
    
    /**
     * return Task_asigned object/false
     */
    function getTaskAsigned($id_user)
    {
        $Parent = $this->getParent();
        if(!$Parent)
            $Parent = $this;
        $FD = getInstance();
        $res = $FD->DB->get_objects_By_Sql("task_asigned", "select ta.* from asign_project as ap, task_asigned as ta where ap.id='$id_user' and ta.id_asign=ap.id_asign and ta.id_task=$Parent->id_task");
        if(count($res))
            return current($res);
        else
            return false;
    }
    
    /**
     * Check if task is in sprint backlog
     * return false if task not asigned to sprint yet
     * return Sprint object if task is already asigned
     */
    function isInSprintBacklog()
    {
        if($Backlog = $this->foreing_key("backlog"))
            return current($Backlog->references("sprint"));
        return false;
    }
    
    function printPhase($texto = false)
    {
        if($texto)
        {
            if($Sprint = $this->isInSprintBacklog())
                return "In Sprint Backlog";
            elseif($Phase = $this->getCurrentPhase())
                return $Phase->foreing_key("fase")->foreing_key("fase_available")->name_faseava;
            else
                return "In Product Backlog";
        }
        elseif($Sprint = $this->isInSprintBacklog())
            return "<a title='Go to sprint' href='".$Sprint->getUrl()."#tasks_combination'>In Sprint Backlog</a>";
        elseif($Phase = $this->getCurrentPhase())
            return "<a title='Go to sprint' href='".$this->getSprint()->getUrl()."#sprint_timeline'>".$Phase->foreing_key("fase")->foreing_key("fase_available")->name_faseava."</a>";
        else
            return "In Product Backlog";
    }
    
    /**
     * return hour_work object/FALSE where its date = $date from user $id_user
     */
    function getHourByDate($date, $id_user)
    {
        $Task_asigned = $this->getTaskAsigned($id_user);
        if(!$Task_asigned)
            return false;
        $date = $date?$date:date("Y-m-d");
        $res = $this->references("hour_work", "date_hourwork = '$date' and id_taskasigned='$Task_asigned->id_taskasigned'");
        if(count($res))
            return current($res);
        else
            return false;
    }
    
    
    /**
     * register a new hour worked to specific task with a user assigned
     * return no_assigned | already_registered | saved | updated
     */
    function registerHour($date, $hour, $user_id, $comment = null, $update_hour = false)
    {
        $Parent = $this->getParent();
        if(!$Parent)
            $Parent = $this;
        
        $TAsign = $Parent->getTaskAsigned($user_id);
        if(!$TAsign)
            return "no_assigned";
        
        $Current_hour = $Parent->getHourByDate($date, $user_id);
        
        if($Current_hour && !$update_hour)
            return "already_registered";
        elseif($Current_hour && $update_hour)
        {
            $Current_hour->setAttr("qty_hourwork", $hour)->update();
            return "updated";
        }else
        {
            $datas["id_taskasigned"] = $TAsign->id_taskasigned;
            $datas["date_hourwork"] = $date;
            $datas["comment_hourwork"] = $comment;
            $datas["qty_hourwork"] = $hour;
            $Parent->create_object("hour_work", $datas)->save();
            return "saved";
        }
    }
    
    /**
     * return hour_work Objects 
     */
    function getHoursWork()
    {
        return $this->references("hour_work");
    }
    
    function canEdit($user)
    {
        if($user->isAdmin() || !$user->isClient())
            return true;
        if($this->getProject()->taskclient_project)
            return true;
            
        return false;
    }
}
?>
<?php
class Company_Controller extends FD_Management
{	
	function __construct()
	{
        parent::__construct();
        $this->useLayout("fastdevelphp/backend");
	}
    
    /**
    * default controller function
    **/
    function index()
    {
        $this->lists();
    }
    
    /**
    * show list items
    **/
    function lists()
    {
        
        $data['Enableds'] = array( "0" => "Enabled",  "1" => "Disabled");
        $data["Companys"] = $this->DB->get_objects("Company");        
        $this->loadView("Company/index", $data);
    }
    
    /**
    * show create form view
    **/
    function create()
    {
        
        $data['Enableds'] = array( "0" => "Enabled",  "1" => "Disabled");
        $data["Company"] = $this->DB->create_object("Company", array('name' => 'Here default value', 'email' => 'Here default value', 'description' => ''));
        $data["action"] = "save";
        $data["submitText"] = "Register";        
        $this->loadView("Company/form", $data);
    }
    
    /**
    * save information into database and then redirect to list items.
    **/
    function save()
    {
        
        $this->DB->create_object("Company", $this->Request->getParams_POST())->save();
        $this->Session->addFlashMessage("action", "Item saved!", 0);
        $this->redirect("Company");
    }
    
    /**
    * show edit form view
    * $id: id_Company (identifier value)
    **/
    function edit($id)
    {
        
        $data['Enableds'] = array( "0" => "Enabled",  "1" => "Disabled");
        $data["Company"] = $Company = $this->DB->get_object_by_id("Company", $id);
        $data["action"] = "update/$Company->id";
        $data["submitText"] = "Update";
        $this->loadView("Company/form", $data);
    }
    
    /**
    * save changes in to database and then redirect to list items.
    * $id: id_Company (identifier value)
    **/
    function update($id)
    {
        
        $this->DB->get_object_by_id("Company", $id)->merge_values($this->Request->getParams_POST())->update();
        $this->Session->addFlashMessage("action", "Item updated!", 0);
        $this->redirect("Company");
    }
    
    /**
    * Delete information from database and then redirect to list items.
    * $id: id_Company (identifier value)
    **/
    function delete($id)
    {
        $Company = $this->DB->get_object_by_id("Company", $id);
        $Company->delete();
        $this->Session->addFlashMessage("action", "Item deleted!", 0);
        $this->redirect("Company");
    }
    
}
?>