Beispiel #1
0
 public function hasChildren()
 {
     $fn = $this->fn;
     $r = $fn($this->current(), $this->key(), $this->depth);
     if (empty($r)) {
         $this->children = null;
         return false;
     }
     $this->children = new RecursiveIterator(iterator($r), $fn, $this->depth + 1);
     return true;
 }
Beispiel #2
0
 /**
  * Advance the inner iterator until we get a non-empty outer iterator.
  */
 function nextOuter()
 {
     while ($this->inner->valid()) {
         $v = $this->inner->current();
         if (is_iterable($v)) {
             $this->outer = iterator($v);
             $this->outer->rewind();
             if ($this->outer->valid()) {
                 return;
             }
             $this->inner->next();
         } else {
             break;
         }
     }
     $this->outer = null;
 }
Beispiel #3
0
/**
 * Converts the argument into an iterator, even if it is not iterable.
 *
 * @param mixed $t Any value type. If it is not iterable, an empty iterator is returned.
 * @return Iterator
 */
function iterator($t)
{
    if (is_array($t)) {
        return new ArrayIterator($t);
    }
    if (is_object($t)) {
        if ($t instanceof IteratorAggregate) {
            return iterator($t->getIterator());
        }
        if ($t instanceof Iterator) {
            return $t;
        }
    }
    if (is_callable($t)) {
        return new FunctionIterator($t);
    }
    return NOIT();
}
function should_enqueue($id_base, $class)
{
    $ret = false;
    iterator($id_base, $class, function ($settings) use(&$ret) {
        if (!(isset($settings['disable_css']) && $settings['disable_css'])) {
            // checks if css disable is not set
            $ret = true;
            return false;
            // stop iterator
        } else {
            return true;
        }
        // continue iteration to next widget
    });
    return $ret;
}
    protected function render()
    {
        $prop = $this->props;
        $context = $this->context;
        $viewModel = $this->getViewModel();
        $context->getAssetsService()->addInlineScript(<<<JS
function check(ev,id,action) {
  action = action || 'check';
  ev.stopPropagation();
  \$.post(location.href, { _action: action, id: id });
}
\$.extend(true, \$.fn.dataTable.Buttons.defaults, {
  dom: {
    button: {
      className: 'btn'
    },
  }
});
function dataGridMultiSearch (ev) {
  var table = \$(\$(ev.target).parents('table')[0]).DataTable()
    , value = ev.target.value
    , idx = ev.target.getAttribute('data-col');
  table.column(idx).search(value).draw();
}
JS
, 'datagridInit');
        $id = $prop->id;
        $minPagItems = self::$MIN_PAGE_ITEMS[$prop->pagingType];
        $PUBLIC_URI = self::PUBLIC_URI;
        $language = $prop->lang != 'en-US' ? "language:     { url: '{$PUBLIC_URI}/js/datatables/{$prop->lang}.json' }," : '';
        $this->setupColumns($prop->column);
        $this->enableRowClick = $this->isPropertySet('onClick') || $this->isPropertySet('onClickGoTo');
        $paging = boolToStr($prop->paging);
        $searching = boolToStr($prop->searching);
        $ordering = boolToStr($prop->ordering);
        $info = boolToStr($prop->info);
        $responsive = $prop->responsive;
        if ($responsive) {
            $responsive = "{\n  details: {\n    type: 'inline'\n  }\n}";
        }
        $lengthChange = boolToStr($prop->lengthChange);
        ob_start();
        $this->runChildren('plugins');
        $plugins = ob_get_clean();
        $this->beginContent();
        $layout = "<'row'<'col-xs-4'f><'col-xs-8'<'dataTables_buttons'B>>><'row'<'col-xs-12'tr>><'row'<'col-xs-7'li><'col-xs-5'p>>";
        $buttons = '';
        if ($prop->actions) {
            $btns = [];
            $prop->actions->preRun();
            foreach ($prop->actions->getChildren() as $btn) {
                if (!$btn instanceof Button) {
                    if ($btn instanceof CompositeComponent) {
                        $btn->preRun();
                        $b = $btn->provideShadowDOM()->getFirstChild();
                        if ($b instanceof Button) {
                            $b->preRun();
                            $btn = $b;
                            goto addBtn;
                        }
                    }
                    throw new ComponentException($this, "Invalid content for the <kbd>actions</kbd> property.\n<p>You can only use Button instances or components whose skin contains a button component as the first child", true);
                }
                addBtn:
                $bp = $btn->props;
                if ($bp->action) {
                    $action = "selenia.doAction('{$bp->action}')";
                } elseif ($bp->script) {
                    $action = $bp->script;
                } elseif ($v = $btn->getComputedPropValue('url')) {
                    $action = "location.href='{$v}'";
                } else {
                    $action = '';
                }
                $class = enum(' ', $bp->class, $bp->icon ? 'with-icon' : '');
                $bLabel = $btn->getComputedPropValue('label');
                $label = $bp->icon ? "<i class=\"{$bp->icon}\"></i>{$bLabel}" : $bLabel;
                $btns[] = sprintf("{className:'%s',text:'%s',action:function(e,dt,node,config){%s}}", $class, $label, $action);
            }
            $buttons = 'buttons:[' . implode(',', $btns) . '],';
        }
        $initScript = '';
        if ($prop->multiSearch) {
            $initScript = <<<JS
var tfoot = this.find('tfoot')
, r = tfoot.find ('tr');
this.find('thead').append(r);
JS;
        }
        $notSortable = [];
        foreach ($prop->column as $i => $col) {
            if ($col->props->notSortable) {
                $notSortable[] = $i + ($this->props->rowSelector ? 1 : 0);
            }
        }
        $columns = "columnDefs: [";
        if ($notSortable) {
            $columns .= sprintf('{ "orderable": false, targets: [%s] }', implode(',', $notSortable));
        }
        $columns .= "],";
        if ($prop->initScript) {
            $initScript .= $prop->initScript->getRendering();
        }
        // AJAX MODE
        if ($prop->ajax) {
            $url = $_SERVER['REQUEST_URI'];
            $action = $prop->action;
            $detailUrl = $prop->detailUrl;
            $this->enableRowClick = $prop->clickable;
            $context->getAssetsService()->addInlineScript(<<<JS
\$('#{$id} table').dataTable({
  serverSide:   true,
  paging:       {$paging},
  lengthChange: {$lengthChange},
  searching:    {$searching},
  ordering:     {$ordering},
  info:         {$info},
  autoWidth:    false,
  responsive:   {$responsive},
  pageLength:   {$prop->pageLength},
  lengthMenu:   {$prop->lengthMenu},
  pagingType:   '{$prop->pagingType}',
  dom:          "{$layout}",
  {$columns}
  {$language}
  {$plugins}
  {$buttons}
  ajax: {
     url: '{$url}',
     type: 'POST',
     data: {
        _action: '{$action}'
    }
   },
  initComplete: function() {
    {$initScript}
    \$('#{$id}').show();
  }
}).on ('length.dt', function (e,cfg,len) {
  {$prop->lengthChangeScript}
}).on ('click', 'tbody tr', function () {
    location.href = '{$detailUrl}' + \$(this).attr('rowid');
});
JS
);
        } else {
            // IMMEDIATE MODE
            $context->getAssetsService()->addInlineScript(<<<JS
\$('#{$id} table').dataTable({
  paging:       {$paging},
  lengthChange: {$lengthChange},
  searching:    {$searching},
  ordering:     {$ordering},
  info:         {$info},
  autoWidth:    false,
  responsive:   {$responsive},
  pageLength:   {$prop->pageLength},
  lengthMenu:   {$prop->lengthMenu},
  pagingType:   '{$prop->pagingType}',
  dom:          "{$layout}",
  {$columns}
  {$language}
  {$plugins}
  {$buttons}
  initComplete: function() {
    {$initScript}
    \$('#{$id}').show();
  },
  drawCallback: function() {
    var p = \$('#{$id} .pagination');
    p.css ('display', p.children().length <= {$minPagItems} ? 'none' : 'block');
  }
}).on ('length.dt', function (e,cfg,len) {
  {$prop->lengthChangeScript}
});
JS
);
            if (isset($prop->data)) {
                /** @var \Iterator $dataIter */
                $dataIter = iterator($prop->data);
                $dataIter->rewind();
                $valid = $dataIter->valid();
            } else {
                $valid = false;
            }
            if ($valid) {
                $this->parseIteratorExp($prop->as, $idxVar, $itVar);
                $columnsCfg = $prop->column;
                foreach ($columnsCfg as &$col) {
                    $col->databind();
                    $col->applyPresetsOnSelf();
                }
                $this->begin('table', ['class' => enum(' ', $prop->tableClass, $this->enableRowClick ? 'table-clickable' : '')]);
                $this->beginContent();
                $this->renderHeader($columnsCfg);
                if (!$prop->ajax) {
                    $idx = 0;
                    /** @noinspection PhpUndefinedVariableInspection */
                    foreach ($dataIter as $i => $v) {
                        if ($idxVar) {
                            $viewModel->{$idxVar} = $i;
                        }
                        $viewModel->{$itVar} = $v;
                        $this->renderRow($idx++, $columnsCfg);
                    }
                }
                $this->end();
            } else {
                $this->renderSet($this->getChildren('noData'));
                $this->context->getAssetsService()->addInlineScript(<<<JS
        \$('#{$id}').show();
JS
);
            }
        }
    }
Beispiel #6
0
 /**
  * Sets the internal iterator.
  *
  * Not recommended for external use. This is used internally to update the first iterator on the chain.
  *
  * @param mixed $it An iterable.
  * @return $this
  */
 function setIterator($it)
 {
     $this->it = iterator($it);
     return $this;
 }
Beispiel #7
0
 /**
  * Generate the OPTION tags based on a resultset
  *
  * @param WpPepVN\Mvc\Model\Resultset resultset
  * @param array using
  * @param mixed value
  * @param string closeOption
  */
 private static function _optionsFromResultset($resultset, $using, $value, $closeOption)
 {
     $code = '';
     $params = null;
     $usingIsArray = false;
     $usingIsObject = false;
     if (is_array($using)) {
         $usingIsArray = true;
     } else {
         if (is_object($using)) {
             $usingIsObject = true;
         }
     }
     $valueIsArray = false;
     if (is_array($value)) {
         $valueIsArray = true;
     }
     if ($usingIsArray) {
         if (count($using) != 2) {
             throw new Exception('Parameter \'using\' requires two values');
         }
         $usingZero = $using[0];
         $usingOne = $using[1];
     }
     foreach (iterator($resultset) as $option) {
         if ($usingIsArray) {
             if (is_object($option)) {
                 if (method_exists($option, 'readAttribute')) {
                     $optionValue = $option->readAttribute($usingZero);
                     $optionText = $option->readAttribute($usingOne);
                 } else {
                     $optionValue = $option->usingZero;
                     $optionText = $option->usingOne;
                 }
             } else {
                 if (is_array($option)) {
                     $optionValue = $option[$usingZero];
                     $optionText = $option[$usingOne];
                 } else {
                     throw new Exception('Resultset returned an invalid value');
                 }
             }
             /**
              * If the value is equal to the option's value we mark it as selected
              */
             if ($valueIsArray) {
                 if (in_array($optionValue, $value)) {
                     $code .= '<option selected="selected" value="' . $optionValue . '">' . $optionText . $closeOption;
                 } else {
                     $code .= '<option value="' . $optionValue . '">' . $optionText . $closeOption;
                 }
             } else {
                 $strOptionValue = (string) $optionValue;
                 $strValue = (string) $value;
                 if ($strOptionValue === $strValue) {
                     $code .= '<option selected="selected" value="' . $strOptionValue . '">' . $optionText . $closeOption;
                 } else {
                     $code .= '<option value="' . $strOptionValue . '">' . $optionText . $closeOption;
                 }
             }
         } else {
             /**
              * Check if using is a closure
              */
             if ($usingIsObject) {
                 if ($params === null) {
                     $params = array();
                 }
                 $params[0] = $option;
                 $code .= call_user_func_array($using, $params);
             }
         }
     }
     return $code;
 }
Beispiel #8
0
 /**
  * Immediately invalidates all existing items.
  */
 public function flush()
 {
     foreach (iterator(new \APCIterator('user')) as $item) {
         apc_delete($item['key']);
     }
     return true;
 }
Beispiel #9
0
/**
 * Converts the argument into an iterator, if possible, otherwise it throws an exception.
 *
 * @param mixed $t An iterable value or null. If null, an empty iterator is returned.
 * @param bool  $throwOnError If false, the function returns false.
 * @return Iterator|false
 */
function iteratorOf($t, $throwOnError = true)
{
    if (is_array($t)) {
        return new ArrayIterator($t);
    }
    if (is_object($t)) {
        if ($t instanceof IteratorAggregate) {
            return iterator($t->getIterator());
        }
        if ($t instanceof Iterator) {
            return $t;
        }
    }
    if (is_null($t)) {
        return new EmptyIterator();
    }
    if ($throwOnError) {
        throw new InvalidArgumentException("Value is not iterable");
    }
    return false;
}
 function merge($navigationMap, $prepend = false)
 {
     self::validateNavMap($navigationMap);
     /**
      * @var string                  $key
      * @var NavigationLinkInterface $link
      */
     foreach (iterator($navigationMap) as $key => $link) {
         $link->parent($this);
         if (is_string($key) && !exists($link->rawUrl())) {
             $link->url($key);
         }
     }
     if ($prepend) {
         $this->links = array_merge($navigationMap, $this->links);
     } else {
         $this->links = array_merge($this->links, $navigationMap);
     }
     return $this;
 }