<commit_before><?php

abstract class dmWidgetBaseView
{
  protected
  $context,
  $widgetType,
  $widget,
  $requiredVars = array(),
  $isIndexable = true,
  $vars,
  $javascripts = array(),
  $stylesheets = array();

  public function __construct(dmContext $context, dmWidgetType $type, array $data)
  {
    $this->context        = $context;
    
    $this->widgetType     = $type;
    $this->widget         = $data;

    $this->configure();
  }

  protected function configure()
  {

  }
  
  protected function addJavascript($keys)
  {
    $this->javascripts = array_merge($this->javascripts, (array) $keys);

    return $this;
  }
  
  public function getJavascripts()
  {
    return $this->javascripts;
  }
  
  protected function addStylesheet($keys)
  {
    $this->stylesheets = array_merge($this->stylesheets, (array) $keys);

    return $this;
  }
  
  public function getStylesheets()
  {
    return $this->stylesheets;
  }

  public function getRequiredVars()
  {
    return $this->requiredVars;
  }
  
  public function isRequiredVar($var)
  {
    return in_array($var, $this->getRequiredVars());
  }

  public function addRequiredVar($var)
  {
    if (is_array($var))
    {
      $this->requiredVars = array_merge($this->requiredVars, $var);
    }
    else
    {
      $this->requiredVars[] = $var;
    }
    
    $this->requiredVars = array_unique($this->requiredVars);
  }
  
  public function removeRequiredVar($var)
  {
    if (is_array($var))
    {
      foreach($var as $v)
      {
        $this->removeRequiredVar($v);
      }
    }
    elseif (false !== ($varIndex = array_search($var, $this->requiredVars)))
    {
      unset($this->requiredVars[$varIndex]);
    }
  }
  
  protected function compileVars(array $vars = array())
  {
    $this->compiledVars = array_merge(
      array('cssClass' => isset($this->widget['css_class']) ? $this->widget['css_class'] : null),
      (array) json_decode((string) $this->widget['value'], true),
      dmString::toArray($vars)
    );
  }

  public function render(array $vars = array())
  {
    $this->compileVars($vars);
    
    if ($this->isValid())
    {
      $html = $this->doRender();
    }
    else
    {
      $html = $this->renderDefault();
    }
    
    return $html;
  }
  
  protected function doRender()
  {
    return $this->renderPartial($this->filterViewVars($this->compiledVars));
  }
  
  protected function renderPartial(array $vars)
  {
    if ($this->isCachable() && $this->context->getViewCacheManager())
    {
      list($partialModule, $partialAction) = $this->getPartialModuleAction();

      $this->context->getViewCacheManager()->addCache($partialModule, '_'.$partialAction, array(
        'withLayout'      => false,
        'lifeTime'        => 86400,
        'clientLifeTime'  => 86400,
        'contextual'      => !$this->isStatic(),
        'vary'            => array($this->widget['id'], $this->getService('user')->getUserId(), $this->getService('user')->getCulture())
      ));
    }

    // add dm_widget to the component/partial vars
    $vars['dm_widget'] = $this->widget;
    
    return $this->doRenderPartial($vars);
  }
  
  protected function doRenderPartial(array $vars)
  {
    list($partialModule, $partialAction) = $this->getPartialModuleAction();

    if ($this->widgetType->useComponent())
    {
      $html = $this->getHelper()->renderComponent($partialModule, $partialAction, $vars);
    }
    else
    {
      $html = $this->getHelper()->renderPartial($partialModule, $partialAction, $vars);
    }

    return $html;
  }<commit_msg>Fix possible notice when caching widgets - Close #264<commit_after><?php

abstract class dmWidgetBaseView
{
  protected
  $context,
  $widgetType,
  $widget,
  $requiredVars = array(),
  $isIndexable = true,
  $vars,
  $javascripts = array(),
  $stylesheets = array();

  public function __construct(dmContext $context, dmWidgetType $type, array $data)
  {
    $this->context        = $context;
    
    $this->widgetType     = $type;
    $this->widget         = $data;

    $this->configure();
  }

  protected function configure()
  {

  }
  
  protected function addJavascript($keys)
  {
    $this->javascripts = array_merge($this->javascripts, (array) $keys);

    return $this;
  }
  
  public function getJavascripts()
  {
    return $this->javascripts;
  }
  
  protected function addStylesheet($keys)
  {
    $this->stylesheets = array_merge($this->stylesheets, (array) $keys);

    return $this;
  }
  
  public function getStylesheets()
  {
    return $this->stylesheets;
  }

  public function getRequiredVars()
  {
    return $this->requiredVars;
  }
  
  public function isRequiredVar($var)
  {
    return in_array($var, $this->getRequiredVars());
  }

  public function addRequiredVar($var)
  {
    if (is_array($var))
    {
      $this->requiredVars = array_merge($this->requiredVars, $var);
    }
    else
    {
      $this->requiredVars[] = $var;
    }
    
    $this->requiredVars = array_unique($this->requiredVars);
  }
  
  public function removeRequiredVar($var)
  {
    if (is_array($var))
    {
      foreach($var as $v)
      {
        $this->removeRequiredVar($v);
      }
    }
    elseif (false !== ($varIndex = array_search($var, $this->requiredVars)))
    {
      unset($this->requiredVars[$varIndex]);
    }
  }
  
  protected function compileVars(array $vars = array())
  {
    $this->compiledVars = array_merge(
      array('cssClass' => isset($this->widget['css_class']) ? $this->widget['css_class'] : null),
      (array) json_decode((string) $this->widget['value'], true),
      dmString::toArray($vars)
    );
  }

  public function render(array $vars = array())
  {
    $this->compileVars($vars);
    
    if ($this->isValid())
    {
      $html = $this->doRender();
    }
    else
    {
      $html = $this->renderDefault();
    }
    
    return $html;
  }
  
  protected function doRender()
  {
    return $this->renderPartial($this->filterViewVars($this->compiledVars));
  }
  
  protected function renderPartial(array $vars)
  {
    if ($this->isCachable() && $this->context->getViewCacheManager())
    {
      list($partialModule, $partialAction) = $this->getPartialModuleAction();

      $this->context->getViewCacheManager()->addCache($partialModule, '_'.$partialAction, array(
        'withLayout'      => false,
        'lifeTime'        => 86400,
        'clientLifeTime'  => 86400,
        'contextual'      => !$this->isStatic(),
        'vary'            => array(dmArray::get($this->widget, 'id'), $this->getService('user')->getUserId(), $this->getService('user')->getCulture())
      ));
    }

    // add dm_widget to the component/partial vars
    $vars['dm_widget'] = $this->widget;
    
    return $this->doRenderPartial($vars);
  }
  
  protected function doRenderPartial(array $vars)
  {
    list($partialModule, $partialAction) = $this->getPartialModuleAction();

    if ($this->widgetType->useComponent())
    {
      $html = $this->getHelper()->renderComponent($partialModule, $partialAction, $vars);
    }
    else
    {
      $html = $this->getHelper()->renderPartial($partialModule, $partialAction, $vars);
    }

    return $html;
  }
{'fraction_non_alphanumeric': 0.1610240626748741, 'fraction_numerical': 0.0032176832680470063, 'mean_word_length': 2.925864909390445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525243', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2291, 'n_words': 394}