<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Core_Widget_ThemeChooseController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    // Get themes
    $themesTable = Engine_Api::_()->getDbtable('themes', 'core');
    $this->view->themes = $themes = $themesTable->select()->query()->fetchAll();

    // Detect active theme
    $activeTheme = null;

    if( !$activeTheme && !empty($_COOKIE['theme']) && is_numeric($_COOKIE['theme']) ) {
      foreach( $themes as $theme ) {
        if( $theme['theme_id'] == $_COOKIE['theme'] ) {
          $activeTheme = $theme;
        }
      }
    }

    if( !$activeTheme ) {
      foreach( $themes as $theme ) {
        if( $theme['active'] ) {
          $activeTheme = $theme;
        }
      }
    }

    $this->view->activeTheme = $activeTheme;
  }
}
