Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lillem4n/larvconomy
Browse files Browse the repository at this point in the history
  • Loading branch information
rockymontana committed Nov 9, 2011
2 parents 1a5c73f + 0f51cb3 commit 34ed002
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
13 changes: 6 additions & 7 deletions classes/controller/admin/employees.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ public function action_employee()
);
xml::to_XML($employee->get(), $this->xml_content->appendChild($this->dom->createElement('employee')), NULL, 'id');
}
else
elseif (count($_POST))
{

$post = new Validation($_POST);
$post->filter('trim');
$employee = new Employee();
$post = new Validation($_POST);
$post->filter('trim');
$employee_id = Employee::new_employee($post->as_array());

$employee->set($post->as_array());
$this->add_message($post->get('firstname') . ' was added as employee');
$this->add_message($post->get('firstname').' (ID: '.$employee_id.') was added as employee');
}
else $this->redirect();
}

}
52 changes: 27 additions & 25 deletions classes/model/employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Model_Employee extends Model
private $id;
private $employee;

public function __construct($id = FALSE)
public function __construct($id)
{
parent::__construct();
if($id) {

$this->prepared_select = $this->pdo->prepare('SELECT * FROM employees WHERE id = ?');
$this->prepared_select->execute(array($id));
$this->id = $id;
Expand All @@ -18,7 +18,7 @@ public function __construct($id = FALSE)
throw new Kohana_Exception('Invalid employee ID');
}
}
}

public function get($detail = FALSE)
{
$current_year = date('Y', time());
Expand All @@ -42,14 +42,33 @@ public function get($detail = FALSE)
else return $this->employee;
}

public function set($array)
{
if(isset($array['id'])) {
public static function new_employee($data)
{
$pdo = Kohana_pdo::instance();

$columns = array();
foreach ($pdo->query('DESCRIBE employees')->fetchAll(PDO::FETCH_ASSOC) as $row)
if ($row['Field'] != 'id') $columns[] = $row['Field'];

foreach ($data as $field => $value)
if ( ! in_array($field, $columns)) unset($data[$field]);

$sql = 'INSERT INTO employees (`'.implode('`,`', array_keys($data)).'`) VALUES(';
foreach ($data as $field => $value) $sql .= $pdo->quote($value).',';
$sql = substr($sql, 0, strlen($sql) - 1).')';

$pdo->query($sql);

return $pdo->lastInsertId();
}

public function set($data)
{
$columns = array_keys($this->employee);
unset($columns[0]); // Remove ID from the index
$sql = 'UPDATE employees SET ';
$counter = 0;
foreach ($array as $key => $value)
foreach ($data as $key => $value)
{
if (in_array($key, $columns))
{
Expand All @@ -68,22 +87,5 @@ public function set($array)

return TRUE;
}
else
{
$attr = "";
$values = "";
unset($array['create_employee']);
foreach($array AS $key => $value)
{
$attr .='`'. $key . "`,";
$values .= $this->pdo->quote($value) . ",";
}
$attr = substr($attr, 0, strlen($attr) - 1);
$values = substr($values, 0, strlen($values) - 1);
$query = "INSERT INTO employees ($attr) VALUES ($values)";
$this->pdo->query($query);
return TRUE;
}
return FALSE;
}

}
4 changes: 2 additions & 2 deletions xsl/admin/bills.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
<xsl:value-of select="format-number(number(sum), '###,###,###.00')" />
<xsl:text> SEK</xsl:text>
</td>
<td><a href="{concat('/bills/pdf/bill_',@id,'.pdf')}">Link</a></td>
<td><a href="http://{root/meta/domain}{/root/meta/base}{concat('user_content/pdf/bill_',@id,'.pdf')}">Link</a></td>
<td>
<xsl:text>[</xsl:text><a href="/bills/bill?billnr={@id}">Details</a><xsl:text>]</xsl:text>
<xsl:text>[</xsl:text><a href="http://{root/meta/domain}{/root/meta/base}bill?billnr={@id}">Details</a><xsl:text>]</xsl:text>
<xsl:if test="paid_date = ''">
<xsl:text>[</xsl:text><a href="bills/mark_as_paid/{@id}/{/root/meta/current_date}" class="paylink" id="paylink_{@id}">Mark as paid</a><xsl:text>]</xsl:text>
</xsl:if>
Expand Down

0 comments on commit 34ed002

Please sign in to comment.