<?php /** * Passbook Online Password Keeper by Paul Chiu * Copyright (c) 2009. All rights reserved. * * This file is part of Passbook. * * Passbook is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Foobar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see http://www.gnu.org/licenses/ * * Created using: * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) */ class ModelComponent extends Object{ // Vars var $controller = null; // Add data for parent, params can have keys: // 'success' : Success message to flash // 'successUrl' : Where to redirect to on success // 'fail' : Fail message to flash // 'failUrl' : Where to redirect on fail function add($modelName, $params = array()) { // Make sure model is loaded $this->loadModel($modelName); // Check for data if ($this->controller->data) { // Create and save record $this->controller->{$modelName}->create(); if ($this->controller->{$modelName}->save($this->controller->data)) { if (isset($params['success']) and $params['success']) $this->controller->Session->setFlash($params['success']); if (isset($params['successUrl']) and $params['successUrl']) $this->controller->redirect($params['successUrl']); return true; } else { if (isset($params['fail']) and $params['fail']) $this->controller->Session->setFlash($params['fail']); if (isset($params['failUrl']) and $params['failUrl']) $this->controller->redirect($params['failUrl']); return false; } } } // Edit data for parent, params can have keys: // 'success' : Success message to flash // 'successUrl' : Where to redirect to on success // 'fail' : Fail message to flash // 'failUrl' : Where to redirect on fail function edit($modelName, $params = array()) { // Make sure model is loaded $this->loadModel($modelName); // Check for data if ($this->controller->data and dot($this->controller->data, "{$modelName}.{$this->controller->$modelName->primaryKey}")) { // Save record if ($this->controller->{$modelName}->save($this->controller->data)) { if (isset($params['success']) and $params['success']) $this->controller->Session->setFlash($params['success']); if (isset($params['successUrl']) and $params['successUrl']) $this->controller->redirect($params['successUrl']); return true; } else { if (isset($params['fail']) and $params['fail']) $this->controller->Session->setFlash($params['fail']); if (isset($params['failUrl']) and $params['failUrl']) $this->controller->redirect($params['failUrl']); return false; } } } // Load any given model name(s), place strings in array to load multiple function loadModel($modelNames) { // Make sure working with an array if (!is_array($modelNames)) $modelNames = array($modelNames); // Process array foreach ($modelNames as $mn) { if (!isset($this->{$mn})) { App::import('model', $mn); eval("\$this->controller->{$mn} = new {$mn}();"); } } } // Start up function startup(&$controller) { $this->controller =& $controller; } } |
CakePHP model component by Paul Chiu