setZoneParams(array('year', 'month')); } function makePath($year, $month) { return "/expenses/$year/$month"; } function initZone($params) { //do something } function initPages($params) { //do something $this->guiAssign('zoneUrl', $this->getZoneUrl()); } function pageDefault($params) { //do something /* $month = (int)date('m'); $year = date('Y'); $this->zoneRedirect("list/$year/$month"); */ $this->zoneRedirect('list'); } function pageList($params) { $year = $this->getZoneParam('year'); $month = $this->getZoneParam('month'); $expenses = Expense::getExpenseList($month, $year); $categories = Category::getBudgetList($month, $year); $categoryOptions = Category::getCategoryOptions($month, $year); $totals['total'] = 0; $totals['amount'] = 0; $totals['left'] = 0; foreach($categories as $id => $data) { $totals['total'] += $data['total'];//how much have we spent $totals['amount'] += $data['amount'];//how much did we allocate $totals['left'] += empty($data['left']) ? $data['amount'] : $data['left'];//how much is left } $this->guiAssign('total', $totals); $this->guiAssign('categories', $categories);//for the budget list $this->guiAssign('categoryOptions', $categoryOptions);//for the drop down category menus $this->guiAssign('month', $month); $prevmonth = $month -1; $prevyear = $year; if($prevmonth == 0) { $prevmonth = 12; $prevyear = $year - 1; } $nextmonth = $month + 1; $nextyear = $year; if($nextmonth == 13) { $nextmonth = 1; $nextyear = $year + 1; } $this->guiAssign('prevPeriod', SCRIPT_URL . zone_expenses::makePath($prevyear, $prevmonth)); $this->guiAssign('nextPeriod', SCRIPT_URL . zone_expenses::makePath($nextyear, $nextmonth)); $this->guiAssign('year', $year); $this->guiAssign('expenses', $expenses); $this->guiDisplay('list.tpl'); } function postList($params) { $year = $this->getZoneParam('year'); $month = $this->getZoneParam('month'); $action = getPostText('actionField'); if($action == 'editCat') { $catId = getPostInt('catId'); BaseRedirect("/budget/editCat/$catId/$year/$month"); } else if ($action == 'editExpense') { $expenseId = getPostInt('expenseId'); $this->zoneRedirect("edit/$expenseId"); } else if ($action == 'deleteExpense') { $expenseId = getPostInt('expenseId'); $expense = &new Expense($expenseId); $expense->delete(); $this->zoneRedirect(VIRTUAL_URL); } } function pageFormEdit($params) { global $gui; $defaults = $this->session('defaults'); $expenseId = $params[1]; $form = new form2("expense", "record", $expenseId); $form->setTitle("Editing Expense"); $form->setValidationOptions("amount", array("type" => "money")); $form->required(array("amount", "category_id")); $form->setFieldParam("id", "formshow", false); if(empty($defaults)) $form->setFieldParam('date', 'default', date('Y-m-d')); else { $form->setFieldParam('date', 'default', $defaults['date']); $form->setFieldParam('category_id', 'default', $defaults['category_id']); $form->setFieldParam('entered_by', 'default', $defaults['entered_by']); } $form->setFieldName('category_id', 'Category'); $form->setFieldIndexTable("category_id", 'category', 'id', 'name', 'deleted = 0 order by name'); $form->setFieldIndexTable("entered_by", 'person', 'id', 'name'); $form->hideField('deleted'); $form->hideField('unique_id'); $form->setHTMLoption('category_id', 'type', 'select'); $form->setHTMLoption('comment', 'type', 'textarea'); $form->setHTMLoption('date', 'type', 'date'); $form->guiAssign(); $this->session("form", $form); $this->guiDisplay('formEdit.tpl'); } function postFormEdit($params) { //gather expenseInfo into an array $action = getPostText('actionField'); if($action != 'cancel') { $form = $this->session("form"); $POST = getRawPost(); $form->saveRecord($POST); $defaults['date'] = $form->getValue('date'); $defaults['category_id'] = $form->getValue('category_id'); $defaults['entered_by'] = $form->getValue('entered_by'); $this->session('defaults', $defaults); $date = explode('-', $defaults['date']); $year = $date[0]; $month = (int)$date[1]; } else { $defaults = $this->session('defaults'); $date = explode('-', $defaults['date']); $year = $date[0]; $month = (int)$date[1]; } switch($action) { case 'save': case 'cancel': $this->zoneRedirect("list/$year/$month"); case 'continue': case 'default': $this->zoneRedirect('edit/new'); default: { $this->setParam('year', $year); $this->setParam('month', $month); $this->zoneRedirect("list"); } } } function pageEdit($params) { global $gui; $defaults = $this->session('defaults'); if($defaults === null) $defaults = array(); $expenseId = $params[1]; if($expenseId == 'new') $expenseInfo = array_merge(Expense::getBlankInfo(), $defaults); else { $expense = &new Expense($expenseId); $expenseInfo = $expense->getInfo(); } //TODO: only show categories that are valid for the date selected by the user. Make user choose date first, then choose category? //TODO: then use Category::getCategoryOptions() $categories = sql_fetch_simple_map("select id, name from category where deleted = 0 order by name", 'id', 'name'); $this->guiAssign('categories', $categories); //TODO: use a person object... $users = sql_fetch_simple_map("select id, name from person", 'id', 'name'); //make guicontrols... $date = &getGuiControl('date', 'date'); $date->setValue($expenseInfo['date']); $date->setRequired(); $date->setValidationType('date'); $this->guiAssign("date", $date); //TODO: create a custom guicontainer that encapsulates the choosing of a date and a category... $amount = &getGuiControl('text', 'amount'); $amount->setValidationType('money'); $amount->setRequired(); $amount->setValue($expenseInfo['amount']); $this->guiAssign('amount', $amount); $comment = &getGuiControl('textarea', 'comment'); $comment->setValue($expenseInfo['comment']); $this->guiAssign('comment', $comment); $store = &getGuiControl('text', 'store'); $store->setValue($expenseInfo['store']); $this->guiAssign('store', $store); $enteredBy = &getGuiControl('select', 'entered_by'); $enteredBy->setParam('displayName', 'Entered By'); $enteredBy->setValue($expenseInfo['entered_by']); $enteredBy->setParam('index', $users); $this->guiAssign('enteredBy', $enteredBy); $this->guiAssign('expenseInfo', $expenseInfo); $this->guiDisplay('edit.tpl'); } function postEdit($params) { //gather expenseInfo into an array $action = getPostText('actionField'); if($action != 'cancel') { $defaults['date'] = $expenseInfo['date'] = getPostText('date'); $defaults['category_id'] = $expenseInfo['category_id'] = getPostInt('category_id'); $defaults['entered_by'] = $expenseInfo['entered_by'] = getPostInt('entered_by'); $this->session('defaults', $defaults); $expenseInfo['store'] = getPostText('store'); $expenseInfo['amount'] = getPostText('amount'); if(getPostIsset('repeat')) $expenseInfo['span_months'] = getPostInt('span_months'); else $expenseInfo['span_months'] = 1; if($params[1] === 'new') { Expense::insert($expenseInfo); } $date = explode('-', $defaults['date']); $year = $date[0]; $month = (int)$date[1]; } else { $defaults = $this->session('defaults'); $date = explode('-', $defaults['date']); $year = $date[0]; $month = (int)$date[1]; } switch($action) { case 'save': case 'cancel': $this->zoneRedirect("list"); case 'continue': case 'default': $this->zoneRedirect('edit/new'); default: $this->zoneRedirect("list"); } } function pageImport($params) { //display a form to upload a file $year = $params[1]; $month = $params[2]; $this->guiAssign('year', $year); $this->guiAssign('month', $month); $this->guiDisplay("import.tpl"); } function postImport($params) { //read a csv file $csvfile = &new CsvExpenseParser(); $csvfile->open($_FILES['importFile']['tmp_name']); sql_begin_transaction(); while(($transaction = $csvfile->getTransaction()) !== false) { $expense = &Expense::ImportTransaction($transaction); } sql_commit_transaction(); $this->zoneRedirect('list'); /* $qif = &new QifParser(); $qif->parse($file); foreach($qif->getTransactions() as $transact) { $expense = &Expense::ImportTransaction($transact); } */ /* $info = $expense->getInfo(); $date = $info['date']; $date = explode('-', $date); $year = $date[0]; $month = $date[1]; die("list/$year/$month"); $this->zoneRedirect("list/$year/$month"); */ } function pageChangeExpenseCategory($params) { $year = $params[1]; $month = $params[2]; $expenseId = $params[3]; $categoryId = $params[4]; $expense = &new Expense($expenseId); $expense->setBasicItem('category_id', $categoryId); $category = &new Category($categoryId); $catInfo = $category->getInfo($month, $year); echo(json_encode($catInfo)); } }