Skip to content
Extraits de code Groupes Projets
DataTable.php 30,9 ko
Newer Older
  • Learn to ignore specific revisions
  • 	}
    
    	/**
    	 * Loads the data from a simple php array.
    	 * Basically maps a simple multidimensional php array to a DataTable.
    	 * Not recursive (if a row contains a php array itself, it won't be loaded)
    	 * 
    	 * @param array Array with the simple structure:
    	 * 		array(
    	 * 			array( col1_name => valueA, col2_name => valueC, ...),
    	 * 			array( col1_name => valueB, col2_name => valueD, ...), 
    	 *		)
    	 */
    
    	public function addRowsFromSimpleArray( $array )
    
    	{
    		if(count($array) === 0)
    		{
    			return;
    		}
    		
    		// we define an exception we may throw if at one point we notice that we cannot handle the data structure
    		$e = new Exception(" Data structure returned is not convertible in the requested format.".
    						" Try to call this method with the parameters '&format=original&serialize=1'".
    						"; you will get the original php data structure serialized.".
    						" The data structure looks like this: \n \$data = " . var_export($array, true) . "; ");
    				
    		
    		// first pass to see if the array has the structure
    		// array(col1_name => val1, col2_name => val2, etc.)
    		// with val* that are never arrays (only strings/numbers/bool/etc.)
    		// if we detect such a "simple" data structure we convert it to a row with the correct columns' names
    		$thisIsNotThatSimple = false;
    		
    		foreach($array as $columnName => $columnValue )
    		{
    			if(is_array($columnValue) || is_object($columnValue)) 
    			{
    				$thisIsNotThatSimple = true;
    				break;
    			}
    		}
    		if($thisIsNotThatSimple === false)
    		{
    			// case when the array is indexed by the default numeric index
    			if( array_keys($array) == array_keys(array_fill(0, count($array), true)) )
    			{
    				foreach($array as $row)
    				{
    					$this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => array($row) ) ) );					
    				}
    			}
    			else
    			{
    				$this->addRow( new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $array ) ) );
    			}
    			// we have converted our simple array to one single row
    			// => we exit the method as the job is now finished 
    			return;
    		}
    		
    		foreach($array as $key => $row)
    		{
    			// stuff that looks like a line
    			if(is_array($row))
    			{
    				/**
    				 * We make sure we can convert this PHP array without losing information.
    				 * We are able to convert only simple php array (no strings keys, no sub arrays, etc.)
    				 * 
    				 */
    				
    				// if the key is a string it means that some information was contained in this key. 
    				// it cannot be lost during the conversion. Because we are not able to handle properly
    				// this key, we throw an explicit exception.
    				if(is_string($key))
    				{
    					throw $e;
    				}
    				// if any of the sub elements of row is an array we cannot handle this data structure...
    				foreach($row as $subRow)
    				{
    					if(is_array($subRow))
    					{
    						throw $e;						
    					}
    				}
    				$row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => $row ) );		
    			}
    			// other (string, numbers...) => we build a line from this value
    			else
    			{
    				$row = new Piwik_DataTable_Row( array( Piwik_DataTable_Row::COLUMNS => array($key => $row)) );
    			}				
    			$this->addRow($row);
    		}
    	}
    
    	/**
    	 * Rewrites the input $array 
    	 * array (
    	 * 	 LABEL => array(col1 => X, col2 => Y),
    	 * 	 LABEL2 => array(col1 => X, col2 => Y),
    	 * )
    	 * to the structure 
    	 * array (
    	 * 	 array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y)),
    	 * 	 array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)),
    	 * )
    	 * 
    
    	 * It also works with array having only one value per row, eg.
    	 * array (
    	 * 	 LABEL => X,
    	 * 	 LABEL2 => Y,
    	 * )
    	 * would be converted to the structure 
    	 * array (
    	 * 	 array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, 'value' => X)),
    	 * 	 array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, 'value' => Y)),
    	 * )
    	 * 
    
    	 * The optional parameter $subtablePerLabel is an array of subTable associated to the rows of the $array
    	 * For example if $subtablePerLabel is given
    	 * array(
    	 * 		LABEL => #Piwik_DataTable_ForLABEL,
    	 * 		LABEL2 => #Piwik_DataTable_ForLABEL2,
    	 * )
    	 * 
    	 * the $array would become 
    	 * array (
    	 * 	 array( 	Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y),
    	 * 				Piwik_DataTable_Row::DATATABLE_ASSOCIATED => #ID DataTable For LABEL
    	 * 		),
    	 * 	 array( 	Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)
    	 * 				Piwik_DataTable_Row::DATATABLE_ASSOCIATED => #ID2 DataTable For LABEL2
    	 * 		),
    	 * )
    	 * 
    	 * @param array $array See method description
    	 * @param array|null $subtablePerLabel see method description
    	 * 
    	 * @return void
    	 */
    
    	public function addRowsFromArrayWithIndexLabel( $array, $subtablePerLabel = null)
    
    	{
    		$cleanRow = array();
    		foreach($array as $label => $row)
    		{
    
    			if(!is_array($row))
    			{
    				$row = array('value' => $row);
    			}
    
    			$cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = null;
    
    			// we put the 'label' column first as it looks prettier in API results
    
    			$cleanRow[Piwik_DataTable_Row::COLUMNS] = array('label' => $label) + $row;
    			if(!is_null($subtablePerLabel)
    				// some rows of this table don't have subtables 
    
    				// (for example case of campaigns without keywords)
    
    				&& isset($subtablePerLabel[$label]) 
    			)
    			{
    				$cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = $subtablePerLabel[$label];
    			}
    			$this->addRow( new Piwik_DataTable_Row($cleanRow) );
    		}
    	}
    
    }