/** * view method * * @param string $id * @return void */ public function view($id = null) { $this->Upload->recursive = 0; $this->Upload->id = $id; if (!$this->Upload->exists()) { throw new NotFoundException(__('Invalid upload')); } $upload = $this->Upload->read(null, $id); $auth = $this->Auth->user(); if (empty($auth)) { $this->Session->setFlash(__('You must be logged in to view this page.')); $this->redirect(array('controller' => 'users', 'action' => 'login')); } //Check to see if the user should be able to view the file if ($auth['id'] != $upload['Upload']['user_id']) { $this->Session->setFlash(__('You must be logged in to view this page.')); $this->redirect(array('controller' => 'users', 'action' => 'login')); } $this->paginate = array('Code' => array('limit' => 10)); $upload['Code'] = $this->paginate('Code'); //Paginate the code results $active_codes = $this->Upload->Code->find('count', array('conditions' => array('Code.upload_id' => $id, 'Code.active' => 1))); $all_codes = $this->Upload->Code->find('all', array('conditions' => array('Code.upload_id' => $id), 'fields' => array('Code.download_count'))); $all_codes = Set::format($all_codes, '{0}', array('{n}.Code.download_count')); $total_downloads = array_sum($all_codes); $this->set(compact('upload', 'active_codes', 'total_downloads')); }
/** * testFormattingNullValues method * * @return void */ public function testFormattingNullValues() { $data = array(array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')), array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => NULL)), array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => NULL))); $result = Set::format($data, '%s', array('{n}.Person.something')); $expected = array('42', '', ''); $this->assertEquals($expected, $result); $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something')); $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, '); $this->assertEquals($expected, $result); }
/** * Creates an associative array using a $path1 as the path to build its keys, and optionally * $path2 as path to get the values. If $path2 is not specified, all values will be initialized * to null (useful for Set::merge). You can optionally group the values by what is obtained when * following the path specified in $groupPath. * * @param array $data Array from where to extract keys and values * @param mixed $path1 As an array, or as a dot-separated string. * @param mixed $path2 As an array, or as a dot-separated string. * @param string $groupPath As an array, or as a dot-separated string. * @return array Combined array * @access public */ function combine($data, $path1 = null, $path2 = null, $groupPath = null) { if (is_a($this, 'set') && is_string($data) && is_string($path1) && is_string($path2)) { $groupPath = $path2; $path2 = $path1; $path1 = $data; $data = $this->get(); } elseif (is_a($this, 'set') && is_string($data) && empty($path2)) { $path2 = $path1; $path1 = $data; $data = $this->get(); } if (is_object($data)) { $data = get_object_vars($data); } if (is_array($path1)) { $format = array_shift($path2); $keys = Set::format($data, $format, $path1); } else { $keys = Set::extract($data, $path1); } if (!empty($path2) && is_array($path2)) { $format = array_shift($path2); $vals = Set::format($data, $format, $path2); } elseif (!empty($path2)) { $vals = Set::extract($data, $path2); } else { $count = count($keys); for ($i = 0; $i < $count; $i++) { $vals[$i] = null; } } if ($groupPath != null) { $group = Set::extract($data, $groupPath); if (!empty($group)) { $c = count($keys); for ($i = 0; $i < $c; $i++) { if (!isset($group[$i])) { $group[$i] = 0; } if (!isset($out[$group[$i]])) { $out[$group[$i]] = array(); } $out[$group[$i]][$keys[$i]] = $vals[$i]; } return $out; } } return array_combine($keys, $vals); }
/** * Creates an associative array using a $path1 as the path to build its keys, and optionally * $path2 as path to get the values. If $path2 is not specified, all values will be initialized * to null (useful for Set::merge). You can optionally group the values by what is obtained when * following the path specified in $groupPath. * * @param array|object $data Array or object from where to extract keys and values * @param string|array $path1 As an array, or as a dot-separated string. * @param string|array $path2 As an array, or as a dot-separated string. * @param string $groupPath As an array, or as a dot-separated string. * @return array Combined array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine */ public static function combine($data, $path1 = null, $path2 = null, $groupPath = null) { if (empty($data)) { return array(); } if (is_object($data)) { if (!($data instanceof ArrayAccess || $data instanceof Traversable)) { $data = get_object_vars($data); } } if (is_array($path1)) { $format = array_shift($path1); $keys = Set::format($data, $format, $path1); } else { $keys = Set::extract($data, $path1); } if (empty($keys)) { return array(); } if (!empty($path2) && is_array($path2)) { $format = array_shift($path2); $vals = Set::format($data, $format, $path2); } elseif (!empty($path2)) { $vals = Set::extract($data, $path2); } else { $count = count($keys); for ($i = 0; $i < $count; $i++) { $vals[$i] = null; } } if ($groupPath != null) { $group = Set::extract($data, $groupPath); if (!empty($group)) { $c = count($keys); for ($i = 0; $i < $c; $i++) { if (!isset($group[$i])) { $group[$i] = 0; } if (!isset($out[$group[$i]])) { $out[$group[$i]] = array(); } $out[$group[$i]][$keys[$i]] = $vals[$i]; } return $out; } } if (empty($vals)) { return array(); } return array_combine($keys, $vals); }
/** * testFormatting method * * @access public * @return void */ function testFormatting() { $data = array(array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')), array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')), array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}'))); $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name')); $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett'); $this->assertEqual($result, $expected); $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name')); $this->assertEqual($result, $expected); $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state')); $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA'); $this->assertEqual($result, $expected); $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state')); $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}'); $this->assertEqual($result, $expected); $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something')); $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}'); $this->assertEqual($result, $expected); $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something')); $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}'); $this->assertEqual($result, $expected); $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something')); $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}'); $this->assertEqual($result, $expected); $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something')); $expected = array('42, Nate', '0, Larry', '0, Garrett'); $this->assertEqual($result, $expected); $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something')); $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0'); $this->assertEqual($result, $expected); }