1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
*
*
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $LastChangedDate$
*
* $Id$
*/
/**
* Compact find result
*
* in model var actAs = array("CompactResult" => array('model to esclude'));
*
*/
class CompactResultBehavior extends ModelBehavior {
var $config = array();
/**
* bviorCompactResults, default: true
* Set to false if you want data formatted in classic CakePHP way
*/
var $bviorCompactResults = true ;
/**
* Unset fields from data result recursively
*/
var $bviorHideFields = array() ;
function setup(&$model, $config = array()) {
$this->config[$model->name] = $config ;
if (!isset($model->bviorCompactResults)) {
$model->bviorCompactResults = $this->bviorCompactResults ;
}
if (!isset($model->bviorHideFields)) {
$model->bviorHideFields = $this->bviorHideFields ;
}
}
function afterFind(&$model, $results) {
if (isset($model->bviorCompactResults) && $model->bviorCompactResults === true)
$this->_compactStart($model, $results);
// remove fields specified
$this->_removeFields($model, $results) ;
return $results ;
}
/**
* Remove from result fields specified
*
* @param unknown_type $model
* @param unknown_type $results
*/
private function _removeFields(&$model, &$results) {
if(empty($model->bviorHideFields) || empty($results)) return ;
// remove field
for($i=0; $i < count($model->bviorHideFields) ; $i++) {
if(array_key_exists($model->bviorHideFields[$i], $results)) {
unset($results[$model->bviorHideFields[$i]]) ;
}
}
// verify among children
foreach ($results as $k => $v) {
if(!is_array($results[$k])) continue ;
$this->_removeFields($model, $results[$k]) ;
}
}
/**
* Check if an array is numerically indexed in a standard manner.
* [0..(n-1)], with no other keys
*
* @param array $array Array to check
* @return boolean
*/
private function _isNumericArray($array) {
if (!is_array($array)) {
return null;
}
return (array_sum(array_keys($array)) === (sizeof($array) * (sizeof($array)-1))>>1) ;
}
private function _compactStart(&$model, &$results) {
// Skip empty arrays
if (empty($results)) return;
if($this->_isNumericArray($results)) {
for($i=0; $i < count($results); $i++) {
if (!isset($results[$i][$model->name])) continue ;
$this->_compact($results[$i], $model) ;
}
} else {
if (!isset($results[$model->name])) return ;
$this->_compact($results, $model) ;
}
}
private function _compact(&$result, &$model) {
$result = $model->am($result, $this->config[$model->name]); // After improvements to BEAppModel::am() brought by #639.
}
}