Example #1
0
File: Radio.php Project: JBZoo/Html
 /**
  * Check selected option in list.
  *
  * @param array $options
  * @param string $selectedVal
  * @return array
  */
 protected function _checkSelected(array $options, $selectedVal)
 {
     if (!empty($selectedVal) && !Arr::key($selectedVal, $options)) {
         $selectedVal = self::KEY_NO_EXITS_VAL;
         $options = array_merge(array(self::KEY_NO_EXITS_VAL => $this->_translate('No exits')), $options);
     }
     return array($options, $selectedVal);
 }
Example #2
0
 /**
  * Removes assets from collection.
  *
  * @param string|array $name
  * @return void
  */
 public function remove($name)
 {
     $names = (array) $name;
     foreach ($names as $name) {
         if (Arr::key($name, $this->_assets)) {
             unset($this->_assets[$name]);
         }
     }
 }
Example #3
0
 /**
  * Crete button.
  *
  * @param string $name
  * @param string $content
  * @param array $attrs
  * @param string $type
  * @return string
  */
 public function render($name = '', $content = '', array $attrs = array(), $type = 'submit')
 {
     $attrs = array_merge(array('text' => null, 'name' => $name), $attrs);
     $attrs = $this->_getBtnClasses($attrs);
     $attrs['type'] = $type;
     if (Arr::key('icon', $attrs)) {
         $content = '<i class="' . $this->_icon . '-' . $attrs['icon'] . '"></i> ' . $this->_translate($content);
         unset($attrs['icon']);
     }
     return '<button ' . $this->buildAttrs($attrs) . '>' . $content . '</button>';
 }
Example #4
0
 /**
  * Create button classes.
  *
  * @param array $attrs
  * @return array
  */
 protected function _getBtnClasses(array $attrs = array())
 {
     if (Arr::key('button', $attrs)) {
         $classes = array($this->_btm);
         foreach ((array) $attrs['button'] as $btn) {
             $classes[] = $this->_btm . '-' . $btn;
         }
         $attrs = $this->_mergeAttr($attrs, implode(' ', $classes));
         unset($attrs['button']);
     }
     return $attrs;
 }
Example #5
0
File: Tag.php Project: JBZoo/Html
 /**
  * Create html tag.
  *
  * @param string $content
  * @param array|string $class
  * @param string $id
  * @param array $attrs
  * @return string
  */
 public function render($content, $class = '', $id = '', array $attrs = array())
 {
     $attrs = $this->_setId($attrs, $id);
     $attrs = $this->_cleanAttrs($attrs);
     if (!empty($class)) {
         $attrs = $this->_normalizeClassAttr($attrs, $class);
     }
     $tag = $this->_tag;
     if (Arr::key('tag', $attrs)) {
         $tag = $attrs['tag'];
         unset($attrs['tag']);
     }
     $format = !empty($attrs) ? '<%s %s>%s</%s>' : '<%s%s>%s</%s>';
     $output = sprintf($format, $tag, $this->buildAttrs($attrs), $content, $tag);
     return $output;
 }
Example #6
0
 /**
  * Initialize hook method.
  *
  * @param array $config
  * @return void
  */
 public function initialize(array $config)
 {
     $this->config('loginRedirect', $this->_loginRedirect());
     $controller = $this->_registry->getController();
     $this->_controller = $controller;
     $permissions = new Permissions($this->user(), $this->request);
     $allowedActions = $permissions->getAllowed();
     $isAllowed = Arr::in($this->request->param('action'), $allowedActions);
     $this->request->offsetSet('isAllowed', $isAllowed);
     if ($this->user('role_id') == Role::ADMIN_ID) {
         $this->allow();
     } else {
         $this->allow($allowedActions);
     }
     $controller->set('loggedUser', $this->_getLoggedUserForView());
     parent::initialize($config);
 }
Example #7
0
 /**
  * Find main/required attributes in data array.
  *
  * @param array $data
  * @return array
  */
 protected function _findMainAttr(array $data)
 {
     $id = $class = $value = '';
     if (Arr::key('value', $data)) {
         $value = $data['value'];
         unset($data['value']);
     }
     if (Arr::key('class', $data)) {
         $class = $data['class'];
         unset($data['class']);
     }
     if (Arr::key('id', $data)) {
         $id = $data['id'];
         unset($data['id']);
     }
     return array($data, $value, $class, $id);
 }
Example #8
0
 public function testFunctionOverhead()
 {
     runBench(array('Clean' => function () {
         return pathinfo(__FILE__, PATHINFO_BASENAME);
     }, 'Wrapper' => function () {
         return FS::base(__FILE__);
     }), array('name' => 'Pathinfo overhead', 'count' => 10000));
     runBench(array('Vars::get' => function () {
         return Vars::get($GLOBALS['somevar']);
     }, 'isset' => function () {
         return isset($GLOBALS['somevar']);
     }), array('name' => 'Isset overhead', 'count' => 10000));
     $randArr = array_fill(0, 100, null);
     for ($i = 0; $i < 100; $i += 1) {
         $randArr[$i] = mt_rand(0, 9);
     }
     runBench(array('array_keys(+flip)' => function () use($randArr) {
         return Arr::unique($randArr, false);
     }, 'array_unique' => function () use($randArr) {
         return Arr::unique($randArr, true);
     }), array('name' => 'Isset overhead', 'count' => 1000));
 }
Example #9
0
 /**
  * Get user avatar by email
  *
  * @param int    $avatarSize
  * @param string $defaultPic
  * @return string
  */
 public function getAvatar($avatarSize = 64, $defaultPic = 'identicon')
 {
     $md5 = md5(trim($this->_email));
     $avatarSize = (int) $avatarSize;
     $defaultPic = trim($defaultPic);
     $validList = array('404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank');
     if (strpos($defaultPic, 'http') === 0) {
         $default = urlencode($defaultPic);
     } elseif (Arr::in((string) $defaultPic, $validList)) {
         $default = $defaultPic;
     } else {
         $default = 'identicon';
     }
     if (Url::isHttps()) {
         $avatarUrl = 'https://secure.gravatar.com/avatar/' . $md5 . '.jpg?s=' . $avatarSize . '&d=' . $default;
     } else {
         $avatarUrl = 'http://www.gravatar.com/avatar/' . $md5 . '.jpg?s=' . $avatarSize . '&d=' . $default;
     }
     return $avatarUrl;
 }
Example #10
0
 /**
  * Build action.
  *
  * @param string $type
  * @trows BadRequestException
  * @return \Cake\Network\Response|null
  */
 public function build($type = 'synchronize')
 {
     if (!Arr::in($type, $this->_buildTypes)) {
         throw new BadRequestException(__d('community', 'Unable to find build type "{0}"', $type));
     }
     $AclExtras = new AclExtras();
     $AclExtras->startup($this);
     if ($type == 'synchronize') {
         $AclExtras->acoSync();
     } else {
         $AclExtras->acoUpdate();
     }
     return $this->redirect(['action' => 'permissions']);
 }
Example #11
0
 /**
  * Merge attributes by given key.
  *
  * @param array $attrs
  * @param null $val
  * @param string $key
  * @return array
  */
 protected function _mergeAttr(array $attrs = array(), $val = null, $key = 'class')
 {
     if (Arr::key($key, $attrs)) {
         $attrs[$key] .= ' ' . $val;
     } else {
         $attrs[$key] = $val;
     }
     return $attrs;
 }
Example #12
0
 /**
  * Rotates and/or flips an image automatically so the orientation will be correct (based on exif 'Orientation')
  *
  * @return $this
  * @codeCoverageIgnore
  * @throws Exception
  */
 public function autoOrient()
 {
     if (!Arr::key('Orientation', $this->_exif)) {
         return $this;
     }
     $orient = (int) $this->_exif['Orientation'];
     if ($orient === 2) {
         // Flip horizontal
         $this->addFilter('flip', 'x');
     } elseif ($orient === 3) {
         // Rotate 180 counterclockwise
         $this->addFilter('rotate', -180);
     } elseif ($orient === 4) {
         // Vertical flip
         $this->addFilter('flip', 'y');
     } elseif ($orient === 5) {
         // Rotate 90 clockwise and flip vertically
         $this->addFilter('flip', 'y');
         $this->addFilter('rotate', 90);
     } elseif ($orient === 6) {
         // Rotate 90 clockwise
         $this->addFilter('rotate', 90);
     } elseif ($orient === 7) {
         // Rotate 90 clockwise and flip horizontally
         $this->addFilter('flip', 'x');
         $this->addFilter('rotate', 90);
     } elseif ($orient === 8) {
         // Rotate 90 counterclockwise
         $this->addFilter('rotate', -90);
     }
     return $this;
 }
Example #13
0
 /**
  * Cleanup array
  *
  * @param mixed           $value
  * @param string|\Closure $filter
  * @return string
  */
 public static function arr($value, $filter = null)
 {
     $array = (array) $value;
     if ($filter === 'noempty') {
         $array = Arr::clean($array);
     } elseif ($filter instanceof \Closure) {
         $array = array_filter($array, $filter);
         // TODO add support both - key + value
     }
     return $array;
 }
Example #14
0
 /**
  * Controller success process.
  *
  * @param string $action
  * @param array $messages
  * @param array $redirect
  * @param array $ids
  * @return \Cake\Network\Response|null
  */
 protected function _process($action, array $messages, array $redirect, array $ids)
 {
     if (Arr::key($action, $messages)) {
         $message = $messages[$action];
     } else {
         $message = __d('union', '{0} processed.', Inflector::humanize($this->_controller->name));
     }
     Event::dispatch($this->_getEventName($action, self::EVEN_POS_AFTER), $this->_controller, ['ids' => $ids]);
     $this->Flash->success($message);
     return $this->_controller->redirect($redirect);
 }
Example #15
0
 /**
  * Clean and validate HTTP-method type
  *
  * @param string $method
  * @return string
  * @throws Exception
  */
 protected function _getMethod($method)
 {
     $method = trim(Str::up($method));
     $validList = array(self::METHOD_GET, self::METHOD_POST, self::METHOD_HEAD, self::METHOD_PUT, self::METHOD_DELETE, self::METHOD_OPTIONS, self::METHOD_PATCH);
     if (Arr::in($method, $validList)) {
         return $method;
     } else {
         throw new Exception('Unsupported Request Method: ' . $method);
     }
 }
Example #16
0
 /**
  * Process action.
  *
  * @return \Cake\Network\Response|null
  */
 public function process()
 {
     list($action, $ids) = $this->Process->getRequestVars($this->name);
     $loggedId = $this->Auth->user('id');
     if (Arr::key($loggedId, $ids) && $action == 'delete') {
         $this->Flash->warning(__d('community', 'It is impossible to delete your entry.'));
         unset($ids[$loggedId]);
     }
     return $this->Process->start($this->Users, $action, $ids);
 }
Example #17
0
 public function testImplodeNested()
 {
     isSame('1,2,3', Arr::implode(',', array(1, 2, 3)));
     isSame('123', Arr::implode('', array(1, 2, 3)));
     isSame('1,2,3,4,5,6', Arr::implode(',', array(1, 2, 3, array(4, 5, 6))));
     isSame('123456', Arr::implode('', array(1, 2, 3, array(4, 5, 6))));
     isSame('1|||||||2|||||||3|||||||4|||||||5|||||||6|||||||7|||||||8|||||||9', Arr::implode('|||||||', array(1, 2, 3, array(4, 5, 6, array(7, 8, 9)))));
     isSame('1,2,3', Arr::implode(',', array('key1' => 1, 'key2' => 2, 'key3' => 3)));
 }
Example #18
0
 /**
  * Find theme path.
  *
  * @param string $name
  * @return null|string
  */
 protected static function _find($name)
 {
     $paths = App::path('Plugin');
     foreach ($paths as $path) {
         $path = FS::clean($path . '/', DS);
         $details = explode(DS, rtrim($path, DS));
         $folder = Str::trim(array_pop($details));
         $themeFolder = $path . $name;
         if (Arr::in($folder, self::$_skipFolder) || !FS::isDir($themeFolder)) {
             $themeFolder .= self::POSTFIX;
         }
         if (FS::isDir($themeFolder)) {
             return $themeFolder;
         }
     }
     return null;
 }
Example #19
0
 /**
  * @return bool
  * @throws \Exception
  */
 public function send()
 {
     // Simple cleanup
     $this->_headers = Arr::clean($this->_headers);
     $this->_atachments = Arr::clean($this->_atachments);
     if ($this->_validateMode !== self::VALIDATE_NO_CHECK) {
         if (!$this->_subject) {
             $this->_error('Subject is empty');
         }
         if (!$this->_body) {
             $this->_error('Body is empty');
         }
         if (count($this->_recipient) !== 2) {
             $this->_error('Recipient is empty');
         }
     }
     $result = false;
     try {
         if ($result = $this->_send()) {
             $this->clean();
         }
     } catch (\Exception $e) {
         $this->_error($e->getMessage());
     }
     return $result;
 }
Example #20
0
 /**
  * Check skip files.
  *
  * @param SplFileInfo $file
  * @return bool
  */
 protected function _isSkip(SplFileInfo $file)
 {
     $name = $file->getFilename();
     $ext = $file->getExtension();
     return Arr::in($ext, $this->_skipFileExt) || Arr::in($name, $this->_skipFileNames);
 }
Example #21
0
 /**
  * Resolves asset dependencies.
  *
  * @param Asset|null $asset
  * @param Asset[]    $resolved
  * @param Asset[]    $unresolved
  * @return Asset[]
  * @throws Exception
  */
 protected function _resolveDependencies(Asset $asset, &$resolved = [], &$unresolved = [])
 {
     $unresolved[$asset->getAlias()] = $asset;
     foreach ($asset->getDependencies() as $dependency) {
         if (!Arr::key($dependency, $resolved)) {
             if (isset($unresolved[$dependency])) {
                 throw new Exception(sprintf('Circular asset dependency "%s > %s" detected.', $asset->getAlias(), $dependency));
             }
             if ($dep = $this->_collection->get($dependency)) {
                 $this->_resolveDependencies($dep, $resolved, $unresolved);
             }
         }
     }
     $resolved[$asset->getAlias()] = $asset;
     unset($unresolved[$asset->getAlias()]);
     return $resolved;
 }
Example #22
0
 /**
  * Check serialize data for response types.
  *
  * @return bool
  */
 protected function _needSerialize()
 {
     return !Arr::key('_serialize', $this->viewVars) && Arr::in($this->response->type(), $this->_serializeTypes);
 }
Example #23
0
 /**
  * Check selected option.
  *
  * @param string $value
  * @param array $selected
  * @return bool
  */
 protected function _isSelected($value, array $selected = array())
 {
     $return = false;
     if (Arr::in($value, $selected)) {
         $return = true;
     }
     return $return;
 }