Location: PHPKode > projects > Entier Studio > entierstudio/studio/handlers/ctrl.RepositoryModelResource.php
<?php
/**
 * Entier Studio
 *
 * LICENSE
 *
 *	Copyright 2006 Entier Studio team.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 *
 * @package    entier.studio
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: ctrl.RepositoryModelResource.php 81 2008-01-17 23:08:21Z yannromefort $
 */
//-------------------------------------------------------------------------
// Safe mode
if (!defined('__ENTIER_FRONT_CONTROLLER__')) die(403);
//
//-------------------------------------------------------------------------
//	Post Handler
//
$resource = NULL;
$postback = !(empty($__verb));
$success = true;
$reset = false;
//
switch ($__verb) {
    case INSERT_CMD:
        //
        # Insert
        //
        @include_once (COMPONENTS_DATA . "data.RepositoryModelComponent.php");
        //
        @include_once (COMPONENTS_DATA . "data.RepositoryModelResource.php");
        //
        $resource = new RepositoryModelResource();
        //
        //
        $resource->set_field_value("REPMRETPKID", $userRequest->Parameter("type"));
        $resource->set_field_value("DIRRESOPKID", $userRequest->Parameter("fkid"));
        //
        $resource->set_field_value("REPMRESLINK", $userRequest->Parameter("link"));
        $resource->set_field_value("REPCELEPKID", $userRequest->Parameter("kind"));
        $resource->set_field_value("REPMRESNAME", $userRequest->Parameter("name"));
        $resource->set_field_value("REPMRESCODE", $userRequest->Parameter("code"));
        $resource->set_field_value("REPMRESINAM", $userRequest->Parameter("inam"));
        $resource->set_field_value("REPMRESTEXT", $userRequest->Parameter("text"));
        $resource->set_field_value("REPMRESFLAG", $userRequest->Parameter("flag"));
        $resource->set_field_value("REPMRESSTAT", $userRequest->Parameter("stat"));
        //
        $link = $userRequest->Parameter("link"); // empty quirk
        //
        $success = $resource->insertRow($database, (empty($link) ? FOREIGNKEY : REFLECTKEY));
        $reset = $success;
        //
        
    break;
    case UPDATE_CMD:
        //
        # Update
        //
        @include_once (COMPONENTS_DATA . "data.RepositoryModelResource.php");
        //
        $resource = new RepositoryModelResource($userRequest->Parameter("pkid"));
        $success = $resource->selectRow($database);
        if (true == $success) {
            //
            $reset = ($resource->get_field_value("REPMRESNAME") != trim($userRequest->Parameter("name")));
            //
            // set fields
            $resource->set_field_value("REPMRESNAME", $userRequest->Parameter("name"));
            $resource->set_field_value("REPMRESCODE", $userRequest->Parameter("code"));
            $resource->set_field_value("REPMRESINAM", $userRequest->Parameter("inam"));
            $resource->set_field_value("REPMRESTEXT", $userRequest->Parameter("text"));
            $resource->set_field_value("REPMRESFLAG", $userRequest->Parameter("flag"));
            $resource->set_field_value("REPMRESRANK", $userRequest->Parameter("rank"));
            $resource->set_field_value("REPMRESSTAT", $userRequest->Parameter("stat"));
            //
            $success = $resource->updateRow($database);
            if (true == $success) {
                //
                //	element properties
                if (is_array($userRequest->Parameter("cvals"))) {
                    // Delete all existing properties values
                    @include_once (COMPONENTS_DATA . "data.RepositoryModelResourcePropertyValueSet.php");
                    //
                    $properties = new RepositoryModelResourcePropertyValueSet($userRequest->Parameter("pkid"));
                    if ($properties->deleteSet($database) == true) {
                        //
                        $repos = $userRequest->Parameter("repos");
                        $cvals = $userRequest->Parameter("cvals");
                        $datas = $userRequest->Parameter("datas");
                        //
                        @include_once (COMPONENTS_DATA . "data.RepositoryModelResourcePropertyValue.php");
                        //
                        for ($i = 0;$i < count($cvals);$i++) {
                            if (!empty($cvals[$i]) && (true == $success)) {
                                //
                                $property = new RepositoryModelResourcePropertyValue();
                                //
                                $property->set_field_value("REPMRESPKID", $userRequest->Parameter("pkid"));
                                $property->set_field_value("REPMEPRPKID", $repos[$i]); // Repository model element property
                                $property->set_field_value("DICPVALPKID", $cvals[$i]); // Dictionary property value
                                $property->set_field_value("REPMRPVDATA", $datas[$i]); // Extended value
                                //
                                $success = $property->insertRow($database);
                                //
                                unset($property);
                            }
                        }
                    }
                }
            }
        }
        //
        
    break;
    case DELETE_CMD:
        //
        # Delete
        //
        @include_once (COMPONENTS_DATA . "data.RepositoryModelResource.php");
        //
        $resource = new RepositoryModelResource($userRequest->Parameter("dkey"));
        $success = $resource->selectRow($database);
        if (true == $success) $success = $resource->deleteRow($database);
        $reset = $success;
        //
        break;
    case STATUS_CMD:
        //
        # Update status=0
        //
        @include_once (COMPONENTS_DATA . "data.RepositoryModelResource.php");
        //
        $resource = new RepositoryModelResource($userRequest->Parameter("dkey"));
        $success = $resource->selectRow($database);
        if (true == $success) $success = $resource->updateRow($database, DISPLAYKEY);
        $reset = $success;
        //
        break;
    }
    //
    //-------------------------------------------------------------------------
    //	View Handler
    //
    @include_once (COMPONENTS_VIEW . "view.RepositoryModelResourceExplorer.php");
    //
    $resourceView = new RepositoryModelResourceExplorer();
    //
    //	Mode Handler
    //
    $viewMode = NULLVIEW;
    $failMode = NULLVIEW;
    $viewHTML = "";
    $failHTML = "";
    $formMark = "";
    //
    switch ($__mode) {
        case "form":
            //
            $failMode = FORMVIEW;
            $failHTML = "$__ctrl-$__fail.html";
            //
            $resourceView->set_field_value("REPOSITPKID", $userRequest->Parameter("root"));
            $resourceView->set_field_value("DIRRESOPKID", $userRequest->Parameter("fkid"));
            $resourceView->set_field_value("REPMRESLINK", $userRequest->Parameter("link"));
            $resourceView->set_field_value("REPMRETPKID", $userRequest->Parameter("type"));
            //
            $resourceView->set_field_value("REPMRESFLAG", $userRequest->Parameter("flag"));
            $resourceView->set_field_value("REPMRESTEXT", $userRequest->Parameter("text"));
            //
            if (($__verb == INSERT_CMD) && (true == $success)) $resourceView->set_field_value("REPMRESPKID", $resource->get_field_value("REPMRESPKID"));
            //
            break;
        case "view":
            //
            $failMode = ITEMVIEW;
            $failHTML = "$__ctrl-$__fail.html";
            //
            $resourceView->set_field_value("REPMRESPKID", $userRequest->Parameter("pkid"));
            //
            break;
        }
        //
        //	Error test
        if (false == $success) {
            //---------------------------------------------------------------------
            //	Error Handler:
            //
            $resourceView->set_error_value("DBERRNUMBER", $database->errorNumber());
            $resourceView->set_error_value("DBERRSTRING", $database->errorString());
            //
            if (is_object($resource)) {
                // Check critical errors
                if (($resource->get_error_value("REPMRETPKID") == true) || ($resource->get_error_value("DIRRESOPKID") == true)) {
                    //
                    $viewMode = FAILVIEW;
                    $viewHTML = "system.errors.html";
                } else {
                    //
                    // get input errors
                    //
                    $resourceView->set_error_value("DATAERRORSET", $resource->errorSet());
                    $resourceView->set_error_value("DATAFIELDSET", $resource->fieldSet());
                    //
                    $formMark = "error";
                    $viewMode = $failMode;
                    $viewHTML = $failHTML;
                }
            }
            //
            //---------------------------------------------------------------------
            
        } else {
            //---------------------------------------------------------------------
            //	Reset handler
            if (($postback == true) && ($reset == true)) $template->assign_global("_POSTVERB_", "$__verb");
            //
            //	View Handler
            switch ($__view) {
                case "form":
                    //
                    $viewMode = FORMVIEW;
                    $viewHTML = "$__ctrl-$__view.html";
                    //
                    
                break;
                case "view":
                    //
                    $viewMode = ITEMVIEW;
                    $viewHTML = "$__ctrl-$__view.html";
                    //
                    
                break;
                case "tree":
                    //
                    $viewMode = TREEVIEW;
                    $viewHTML = "$__ctrl-$__view.html";
                    $formMark = "tree";
                    //
                    $resourceView->set_field_value("DIRRESOPKID", $userRequest->Parameter("pkid"));
                    $resourceView->set_field_value("REPOSITPKID", $userRequest->Parameter("root"));
                    //
                    
                break;
                case "frame":
                    //
                    $viewMode = FRAMEVIEW;
                    $viewHTML = "$__ctrl-$__view.html";
                    //
                    $resourceView->set_field_value("DIRRESOPKID", $userRequest->Parameter("pkid"));
                    $resourceView->set_field_value("REPOSITPKID", $userRequest->Parameter("root"));
                    //
                    
                break;
            }
            //
            //---------------------------------------------------------------------
            
        }
        //-------------------------------------------------------------------------
        //	Display View
        //
        if ($resourceView->renderView($database, $template, $viewHTML, $formMark, $viewMode) == false) {
            if ($resourceView->get_error_count() > 0) {
                //
                $viewMode = FAILVIEW;
                $viewHTML = "action.errors.html";
                //
                @include_once (FRAMEWORK_VIEW . "view.ExceptionView.php");
                //
                $exceptionView = new ExceptionView($resourceView->errorSet());
                $exceptionView->renderView($viewHTML, $formMark, $viewMode);
            }
        }
        //
        //-------------------------------------------------------------------------
        
?>
Return current item: Entier Studio